Send an email using mailgun

Send a simple HTML-based email using the mailgun API.

Script mailgun

by aurélien brabant550 · 6/22/2023

  • Submitted by aurélien brabant550 Deno
    Created 1061 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    2
    import formData from "npm:[email protected]";
    3
    import Mailgun from "npm:[email protected]";
    4
    
    
    5
    // fill the type, or use the +Resource type to get a type-safe reference to a resource
    6
    // type Postgresql = object
    7
    
    
    8
    export async function main(
    9
      mailgunConfig: wmill.Resource<"mailgun">,
    10
      domain: string,
    11
      from: string,
    12
      to: string[],
    13
      subject: string,
    14
      html: string,
    15
    ) {
    16
      const mailgun = new Mailgun(formData);
    17
      const mg = mailgun.client({ username: "api", key: mailgunConfig.api_key });
    18
    
    
    19
      try {
    20
        const result = await mg.messages.create(domain, {
    21
          from,
    22
          to,
    23
          subject,
    24
          html,
    25
        });
    26
    
    
    27
        return result;
    28
      } catch (error) {
    29
        console.error(error);
    30
        throw error;
    31
      }
    32
    }
    33