Hierarchy

  • EmailPowers

Index

Methods

Methods

Static send

  • Sends an email with HTML content and options.

    example

    The following example sends an email with an inline HTML template and an inline text attachment content.

    
    import { ISkillActivity, ISkillContext } from "andiskills";
    import { DataPowers, EmailBody, EmailOptions, EmailPowers } from "@andi/powers";
    
    // Send an email using EmailPowers with templated HTML data.
    export async function run(skillContext: ISkillContext): Promise<ISkillActivity> {
    
      // Handlebars HTML template - this can be either inline or could be read from storage using DataPowers.
      const htmlBody = `
        <table cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td style="padding-bottom: 15px">
                <!-- This image src refers to one of the email attachments. See below for more information. -->
                <img
                  alt="Image of books"
                  src="cid:books.png"
                  width="240"
                  height="240"
                >
              </td>
            </tr>
            <tr>
              <td style="font-size: 24px; padding: 5px">4 Novels Considered the “Greatest Book Ever Written”</td>
            </tr>
            <tr>
              <td>
                <table cellspacing="0" cellpadding="0">
                  <thead>
                      <tr>
                        <th style="border-style: solid; border-width: 1px; border-color: #000; padding: 5px">Title</th>
                        <th style="border-style: solid; border-width: 1px; border-color: #000; padding: 5px">Details</th>
                      </tr>
                    </thead>
                  <tbody>
                  {{#each rows}}
                    <tr>
                      <td style="border-style: solid; border-width: 1px; border-color: #000; padding: 5px">{{ title }}</td>
                      <td style="border-style: solid; border-width: 1px; border-color: #000; padding: 5px">{{ details }}</td>
                    </tr>
                  {{/each}}
                  </tbody>
                </table>
              </td>
            </tr>
          </tbody>
        </table>
      `;
      // Above template expects this data shape.
      // This data is set inline here but can also come from other sources like API call or DataPowers call.
      const htmlData = {
        rows: [
          {
            title: "THE GREAT GATSBY",
            details: "Author: F. Scott Fitzgerald",
          },
          {
            title: "BRAVE NEW WORLD",
            details: "Author: Aldous Huxley",
          },
          {
            title: "THE SOUND AND THE FURY",
            details: "Author: William Faulkner",
          },
          {
            title: "CATCH-22",
            details: "Author: Arthur Koestler",
          }
        ]
      };
    
      // Compose EmailBody using above data, it requires htmlBody & htmlData
      const emailBody: EmailBody = {
        htmlBody,
        htmlData,
        template: "default" // Default Email wrapper template that includes header/footer will be used. You can use 'none' if your own HTML is sufficient.
      };
    
      // Compose EmailOptions with a text attachment.
      // Attachment in this case is an inline text content. You can use csv, json, excel, word, txt etc. type of file data. You can load such files using DataPowers.
      const emailOptions: EmailOptions = {
        subject: "Test email - “Greatest Book Ever Written”",
        // If your organization is using email whitelist, the email address must belong to one of the whitelisted domains.
        to: ["youremail@yourdomain.com"],
        cc: ["youremail2@yourdomain.com"],
        attachments: [
          {
            name: "notes.txt",
            content: "Literary critics, historians, avid readers, and even casual readers will all have different opinions on which novel is truly the “greatest book ever written.” Is it a novel with beautiful, captivating figurative language? Or one with gritty realism? A novel that has had an immense social impact? Or one that has more subtly affected the world? Here is a list of 4 novels that, for various reasons, have been considered some of the greatest works of literature ever written."
          },
          // A storage file can also be used as an attachment. You can embed an image by attaching it and referencing it by its contentId within the HTML.
          // {
          //   name: "books.png",
          //   content: await DataPowers.get("books.png", { type: DataType.File, parseOptions: "buffer" }),
          //   contentId: "books.png"
          // }
        ]
      };
      // Send email using EmailPowers. This email will be put on a queue and sent as soon as possible.
      await EmailPowers.send(emailBody, emailOptions);
    
      return skillContext.powers.andi.batchJob.returnRunComplete();
    }
    

    Parameters

    Returns Promise<void>

Generated using TypeDoc