javascript - 使用 Meteor.js 发送电子邮件附件(电子邮件包和/或 nodemailer 或其他)

标签 javascript email meteor email-attachments nodemailer

Meteor 官方似乎还没有实现发送邮件附件email package .我尝试了 nodemailer 建议(参见 here )但收到错误“无法读取未定义的属性‘createTransport’”。

我正在尝试在数据 URI 中创建一个 CSV 文件,然后发送该附件。这是我使用官方电子邮件包时的代码片段:

csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

var options = {
          from: "xxx@gmail.com",
          to: "xxx@gmail.com",
          subject: "xxx",
          html: html,
          attachment: {
            fileName: fileName, 
            path: csvData
            }
      };

Meteor.call('sendEmail', options);

编辑:

这基本上是我的 nodemailer 代码的样子:

var nodemailer = Nodemailer;
var transporter = nodemailer.createTransport();
transporter.sendMail({
    from: 'sender@address',
    to: 'receiver@address',
    subject: 'hello',
    text: 'hello world!',
    attachments: [
        {   
            path: csvData
        }
    ]
});

最佳答案

没有足够的代表发表评论。

我最终使用 Sendgrids NPM 包解决了附件问题。

npm install sendgrid

如果你的 meteor 应用程序中没有 npm,你可以阅读这篇文章。 https://meteorhacks.com/complete-npm-integration-for-meteor

在你的 packages.json 中

{
  "sendgrid": "1.4.0"
}

然后在服务器上运行的文件中:

Meteor.startup(function(){
    process.env.MAIL_URL = 'smtp://<username>:<password>@smtp.sendgrid.net:587';
});

这是一个示例 meteor 方法,它从附件集合中获取附件的 url(我们使用的是 S3)。此特定方法可以将任意数量的附件发送给任意数量的收件人。这里有一些特定于上下文的逻辑,但它应该足以让您启动并运行发送附件。

重要部分:

var email = new sendgrid.Email();
email.setFrom("email@email.com");
email.setSubject("subject");
email.addFile({
    filename: attachment_name,
    url: attachment_url
});
sendgrid.send(email, function (err, json) {
    if (err) {
        console.error(err); 
    } 
    if (json) { 
        console.log(json.message);                   
    }
});

一个完整的方法示例:

Meteor.methods({
SendEmail: function (subject, message, templateNumber) {

    //console.log(subject, message, templateNumber);

    var user_id = Meteor.userId();
    var list = UserList.find({user_id: user_id}).fetch();
    var sentTemplate = sentTemplate + templateNumber;
    var counter = 0;
    console.log(list.length);
    // Track is the 'No Response' from the list.
    for (var i = 0; i < list.length; i++) {    
            var email = new sendgrid.Email();
            if (list[i].track == null || list[i].track == "1") {
                //email.addTo(list[0].list[i].Email);
                //console.log(list[0].list[i].Email);
                email.to = list[i].email;
            }
            email.setFrom(Meteor.user().email);
            email.replyto = Meteor.user().email;

            email.setSubject(subject);

            var firstName = list[i].name.split(" ")[0];

            var companyReplace = message.replace("{{Company}}", list[i].company).replace("{{Company}}", list[i].company).replace("{{Company}}", list[i].company).replace("{{Company}}", list[i].company).replace("{{Company}}", list[i].company);
            var nameReplace = companyReplace.replace("{{Name}}",list[i].name).replace("{{Name}}",list[i].name).replace("{{Name}}",list[i].name).replace("{{Name}}",list[i].name).replace("{{Name}}",list[i].name)
            var firstNameReplace = companyReplace.replace("{{FirstName}}",firstName).replace("{{FirstName}}",firstName).replace("{{FirstName}}",firstName).replace("{{FirstName}}",firstName).replace("{{FirstName}}",firstName);

            email.setHtml(firstNameReplace);

            var numAttachments = Attachments.find({user_id: Meteor.userId()}).fetch().length;
            var attachments = Attachments.find({user_id: Meteor.userId()}).fetch();
            console.log("**********Attachments****************");
            console.log(attachments);
            console.log("**********Attachments****************");
            for (var t = 0; t < numAttachments; t++) {
                email.addFile({
                    filename: attachments[t].attachment_name,
                    url: attachments[t].attachment_url
                });
            }
            sendgrid.send(email, function (err, json) {
                if (err) {
                    console.error(err);
                } 
                if (json) {
                    console.log(json.message);

                }
            });
            //console.log(email);

    } // end for loop

    if (templateNumber == 1) {
        Meteor.users.update({_id:Meteor.userId()}, {$set: {"sentTemplate1": true}});
    }
    if (templateNumber == 2) {
        Meteor.users.update({_id:Meteor.userId()}, {$set: {"sentTemplate2": true}});
    }
    if (templateNumber == 3) {
        Meteor.users.update({_id:Meteor.userId()}, {$set: {"sentTemplate3": true}});
    }
    if (templateNumber == 4) {
        Meteor.users.update({_id:Meteor.userId()}, {$set: {"sentTemplate4": true}});
    }
    // for each email. replace all html

   return list.length;
}
});

关于javascript - 使用 Meteor.js 发送电子邮件附件(电子邮件包和/或 nodemailer 或其他),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308994/

相关文章:

javascript - 在Safari中拦截CMD+Z、CMD+SHIFT+Z和CMD+Y

javascript - 如何从当前 URL 获取路由名称

r - 在 Linux 上用 R 发送电子邮件

java - 使用 hibernate 的点 "."不敏感搜索?

javascript - 如何在 Cordova-plugin-smtp-client Meteor 移动应用程序上查找错误

javascript - 无法从本地主机向远程服务器发出 ajax 请求

javascript - Durandal路由IF语句

angular - 为什么 Firebase 电子邮件验证在 Web 上创建用户后需要很长时间才能交付?

javascript - 模板助手中出现异常 : quickFormContext with aldeed:autoform

android - Meteor:尝试在我的设备中运行我的应用程序会出现依赖性错误