Twilio - 从函数发送电子邮件
Twilio - Send email from function
有没有办法从 Twilio 函数发送电子邮件?我知道我们可以使用 sendgrid。我正在寻找更简单的解决方案。
这里是 Twilio 布道者。
截至目前,您可以使用 SendGrid from within a Twilio Function。下面的代码为我完成了这项工作,我刚刚通过函数
发送了一封电子邮件
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);
})
};
由于 test@example.com
不是一个非常值得信赖的电子邮件地址,因此上述电子邮件很可能会成为垃圾邮件。如果您想从自己的域发送电子邮件,则需要额外的配置。
对于运行函数内的代码,您必须确保安装sendgrid/mail
邮件依赖项并在the function configuration.
中提供sendgrid令牌
如果您想使用此功能为例如您必须确保您的消息 return 有效 TwiML。 :) 当您创建一个新函数时,您将获得有关如何执行此操作的示例。
希望对您有所帮助。 :)
另一种方法是使用 SendGrid API
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
]
};
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
};
};
有没有办法从 Twilio 函数发送电子邮件?我知道我们可以使用 sendgrid。我正在寻找更简单的解决方案。
这里是 Twilio 布道者。
截至目前,您可以使用 SendGrid from within a Twilio Function。下面的代码为我完成了这项工作,我刚刚通过函数
发送了一封电子邮件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);
})
};
由于 test@example.com
不是一个非常值得信赖的电子邮件地址,因此上述电子邮件很可能会成为垃圾邮件。如果您想从自己的域发送电子邮件,则需要额外的配置。
对于运行函数内的代码,您必须确保安装sendgrid/mail
邮件依赖项并在the function configuration.
如果您想使用此功能为例如您必须确保您的消息 return 有效 TwiML。 :) 当您创建一个新函数时,您将获得有关如何执行此操作的示例。
希望对您有所帮助。 :)
另一种方法是使用 SendGrid API
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
]
};
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
};
};