{"size": 3449, "ext": "js", "max_stars_repo_path": "components/mailgun/actions/send-email/send-email.js", "max_stars_repo_name": "wong2/pipedream", "max_stars_repo_stars_event_min_datetime": "2022-03-09T16:56:08.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-09T16:56:08.000Z", "max_issues_repo_path": "components/mailgun/actions/send-email/send-email.js", "max_issues_repo_name": "wong2/pipedream", "max_issues_repo_issues_event_min_datetime": "2021-12-18T00:58:00.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-28T06:14:27.000Z", "max_forks_repo_path": "components/mailgun/actions/send-email/send-email.js", "max_forks_repo_name": "wong2/pipedream", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 16, "max_forks_count": null, "avg_line_length": 24.8129496403, "max_line_length": 99, "alphanum_fraction": 0.5534937663}
const mailgun = require("../../mailgun.app.js");
const {
  props,
  methods,
} = require("../common");

module.exports = {
  key: "mailgun-send-email",
  name: "Send Email",
  description: "Send email with Mailgun.",
  version: "0.0.28",
  type: "action",
  props: {
    mailgun,
    domain: {
      propDefinition: [
        mailgun,
        "domain",
      ],
    },
    fromName: {
      propDefinition: [
        mailgun,
        "name",
      ],
      label: "From Name",
      description: "Sender name",
    },
    from: {
      propDefinition: [
        mailgun,
        "email",
      ],
      label: "From Email",
      description: "Sender email address",
    },
    /* eslint-disable pipedream/default-value-required-for-optional-props */
    replyTo: {
      propDefinition: [
        mailgun,
        "email",
      ],
      label: "Reply-To",
      description: "Sender reply email address",
      optional: true,
    },
    /* eslint-enable pipedream/default-value-required-for-optional-props */
    to: {
      propDefinition: [
        mailgun,
        "emails",
      ],
      label: "To",
      description: "Recipient email address(es)",
    },
    /* eslint-enable pipedream/default-value-required-for-optional-props */
    subject: {
      propDefinition: [
        mailgun,
        "subject",
      ],
      description: "Message subject",
    },
    text: {
      propDefinition: [
        mailgun,
        "body_text",
      ],
      optional: true,
    },
    /* eslint-disable pipedream/default-value-required-for-optional-props */
    html: {
      propDefinition: [
        mailgun,
        "body_html",
      ],
      optional: true,
    },
    /* eslint-enable pipedream/default-value-required-for-optional-props */
    testMode: {
      type: "boolean",
      label: "Send in test mode?",
      default: true,
      description: "Enables sending in test mode. For more information, see the [Mailgun API " +
        "documentation](https://documentation.mailgun.com/en/latest/api-sending.html#sending)",
    },
    dkim: {
      type: "boolean",
      label: "Use DKIM?",
      default: true,
      description: "Enables or disables DKIM signatures. For more information, see the [Mailgun " +
        "API documentation](https://documentation.mailgun.com/en/latest/api-sending.html#sending)",
      optional: true,
    },
    tracking: {
      type: "boolean",
      label: "Use Tracking?",
      default: true,
      description: "Enables or disables tracking. For more information, see the [Mailgun API " +
        "documentation](https://documentation.mailgun.com/en/latest/api-sending.html#sending)",
      optional: true,
    },
    ...props,
  },
  methods: {
    ...methods,
  },
  async run() {
    const msg = {
      "from": `${this.fromName} <${this.from}>`,
      "to": this.to,
      "subject": this.subject,
      "text": this.text,
      "html": this.html,
    };
    if (this.testMode) {
      msg["o:testmode"] = "yes";
    }
    if (this.replyTo) {
      msg["h:Reply-To"] = this.replyTo;
    }
    if (this.dkim !== null) {
      msg["o:dkim"] = this.dkim
        ? "yes"
        : "no";
    }
    if (this.tracking) {
      msg["o:tracking"] = "yes";
    }
    const sendMail = async function (mailgun, opts) {
      return await mailgun.api("messages").create(opts.domain, opts.msg);
    };
    return await this.withErrorHandler(sendMail, {
      domain: this.domain,
      msg,
    });
  },
};
