Twilio - 从功能发送电子邮件

aru*_*mar 5 twilio twilio-functions

有没有办法从 Twilio 功能发送电子邮件?我知道我们可以使用 sendgrid。我正在寻找一个更简单的解决方案。

ste*_*dis 5

Twilio 传道者在这里。

截至目前,您可以在Twilio Function中使用SendGrid。下面的代码为我完成了这项工作,我刚刚通过函数发送了一封电子邮件

exports.handler = function(context, event, callback) {
    const sgMail = require('@sendgrid/mail');
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    const msg = {
      to: 'sjudis@twilio.com',
      from: 'test@example.com',
      subject: 'Sending with SendGrid is Fun',
      text: 'and easy to do anywhere, even with Node.js',
      html: '<strong>and easy to do anywhere, even with Node.js</strong>',
    };
    sgMail.send(msg)
    .then(() => {
        callback(null, 'Email sent...');
    })
    .catch((e) => {
        console.log(e);
    })
};
Run Code Online (Sandbox Code Playgroud)

上述电子邮件很可能会成为垃圾邮件,因为test@example.com它不是一个非常值得信任的电子邮件地址。如果您想从自己的域发送电子邮件,则需要进行其他配置。

要运行函数内部的代码,您必须确保安装邮件依赖项并在函数配置sendgrid/mail中提供 sendgrid 令牌。

Twilio功能配置

如果您想使用此函数来支持消息等,您必须确保返回有效的TwiML。:) 当您创建新函数时,您将获得有关如何执行此操作的示例。

希望有帮助。:)