Edits history of script submission #22452 for ' Send an email based on a mailgun template (mailgun)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import formData from "[email protected]";
    import MailgunClient from "[email protected]";
    
    interface Mailgun {
      api_key: string;
    }
    
    export async function main(
      mailgunConfig: Mailgun,
      domain: string,
      from: string,
      to: string[],
      subject: string,
      templateName: string,
      replyTo?: string,
      substitutions: Record<string, string>,
    ) {
      const mailgun = new MailgunClient(formData);
      const mg = mailgun.client({ username: "api", key: mailgunConfig.api_key });
    
      try {
        const result = await mg.messages.create(domain, {
          from,
          to,
          subject,
          template: templateName,
          "h:X-Mailgun-Variables": JSON.stringify(substitutions),
          ...(replyTo && { "h:Reply-To": replyTo }),
        });
    
        return result;
      } catch (error) {
        console.error(error);
        throw error;
      }
    }
    

    Submitted by hugo989 4 days ago