是否可以通过SendGrid API发送批量预先呈现的电子邮件? [英] Is it possible to to send bulk pre-rendered email via the SendGrid API?
本文介绍了是否可以通过SendGrid API发送批量预先呈现的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检查了SendGrid API文档,没有发现批量发送预先呈现的电子邮件的可能性。这可能吗?
我想做的是使用我自己的模板引擎呈现单独的电子邮件内容,然后将这些内容(包括收件人、主题和邮件正文)批量上传到SendGrid系统。
我可以毫无问题地发送事务性邮件,但目前它对于大量邮件来说太慢了。
我当前使用的是Node.js库https://github.com/sendgrid/sendgrid-nodejs
推荐答案
有多种可能性。一般来说,为了快速发送一堆电子邮件,我推荐SMTPAPI,它允许您在一个请求中发送多达10,000封电子邮件。但是,这需要使用SendGrid的substitution system来自定义消息。由于您希望使用自己的模板系统,因此这可能不是您的最佳选择。
我期待的一件事只是代码优化,因为并行发送一堆电子邮件可能不会花费太长时间。众所周知,我们的一些最大的发送者会通过个人连接一次性丢弃数百万封电子邮件。不过,从长远来看,这可能不是最佳解决方案。
如果这两个答案都不起作用,那么有一种方法可以通过一个连接向SendGrid发送大量电子邮件:SMTP。但是,这将需要删除SendGrid Node Library并将其替换为NodeMailer(或某些其他SMTP库)。
默认情况下,NodeMailer将使连接保持打开和活动状态,以便您可以在一个事务中发送多封电子邮件。要做到这一点,您可以这样做:var nodemailer = require("nodemailer");
// Create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "SendGrid",
auth: {
user: "your_username",
pass: "your_password"
}
});
// Let get the users you want to send email to
var users = getAllUsersAsArray();
// Loop through your users
users.forEach(function (user){
// Setup the message
var mailOptions = {
from: "You <you@example.com>",
to: user.email,
subject: subjectTemplate.render(user),
text: textTemplate.render(user),
html: htmlTemplate.render(user)
}
// Send mail
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
});
如果您仍需要利用SendGrid的SMTPAPI的部分内容,则可以使用SMTPAPI Node.js library并将其结果插入到邮件头中。
var mailOptions = {
from: "You <you@example.com>",
to: user.email,
// Assuming you've created an smtpapi header instance named header
headers: header.jsonString(),
subject: subjectTemplate.render(user),
text: textTemplate.render(user),
html: htmlTemplate.render(user)
}
这篇关于是否可以通过SendGrid API发送批量预先呈现的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文