是否可以通过SendGrid API发送批量预渲染电子邮件?

我检查了SendGrid API文档,并没有发现发送批量预渲染电子邮件的可能性。 这可能吗?

我想要做的是使用我自己的模板引擎来渲染单个电子邮件内容,然后将这些内容(包括收件人,主题和邮件正文)作为批量上传到SendGrid系统。

我可以发送交易邮件,但没有问题,但是对于大量的邮件来说,它现在太慢了。

我目前正在使用Node.js库https://github.com/sendgrid/sendgrid-nodejs

有几种可能性。 一般来说,快速发送一堆电子邮件,我build议SMTPAPI ,它允许您从一个请求发送多达10,000个电子邮件。 但是,这需要使用SendGrid的replace系统来自定义邮件。 既然你想使用自己的模板系统,这可能不是你最好的select。

有一件事我只想简单地进行代码优化,因为并行发送一堆电子邮件可能不会花太多时间。 我们的一些最大的发送者已经知道通过个人连接立即转储数百万封电子邮件。 尽pipe从长远来看,这可能不是最好的解决scheme。


如果这两个答案都不起作用,可以通过一个连接发送一串电子邮件到SendGrid:SMTP。 但是,这将需要删除SendGrid节点库并将其replace为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库并将其结果插入到邮件的标题中。

 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) }