2015-02-03 25 views
9

Gửi tệp đính kèm email dường như chưa được triển khai trong chính thức email package của Meteor. Tôi đã thử đề xuất nodemailer (xem here) nhưng đã nhận được lỗi "Không thể đọc thuộc tính 'createTransport' không xác định".Gửi tệp đính kèm email bằng Meteor.js (gói email và/hoặc nodemailer hoặc cách khác)

Tôi đang cố gắng tạo tệp CSV trong URI dữ liệu và sau đó gửi tệp đính kèm đó. Dưới đây là một đoạn mã của tôi khi sử dụng gói email chính thức:

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

var options = { 
      from: "[email protected]", 
      to: "[email protected]", 
      subject: "xxx", 
      html: html, 
      attachment: { 
      fileName: fileName, 
      path: csvData 
      } 
     }; 

Meteor.call('sendEmail', options); 

EDIT:

đây về cơ bản là những gì mã nodemailer của tôi trông giống như:

var nodemailer = Nodemailer; 
var transporter = nodemailer.createTransport(); 
transporter.sendMail({ 
    from: '[email protected]', 
    to: '[email protected]', 
    subject: 'hello', 
    text: 'hello world!', 
    attachments: [ 
     { 
      path: csvData 
     } 
    ] 
}); 
+0

những gì mã nodemailer bạn đã cố gắng? Tôi không tin rằng file đính kèm được hỗ trợ trong gói chính thức thiên thạch. – rivarolle

+0

Tôi đã sử dụng [gói này] (https://atmospherejs.com/mrt/meteor-nodemailer) và làm theo hướng dẫn để chạy nó (có thiết lập khá giống với email của Meteor). Tôi sẽ cập nhật câu hỏi của mình bằng mã. –

+0

Bài viết này có một số ví dụ http://kukuruku.co/hub/javascript/meteor-how-to-build-a-todo-list –

Trả lời

5

Không đủ đại diện để bình luận.

Tôi đã kết thúc giải quyết sự cố tệp đính kèm bằng cách sử dụng gói Sendgrids NPM.

npm install sendgrid 

Nếu bạn không có npm trong ứng dụng sao băng, bạn có thể đọc nội dung này. https://meteorhacks.com/complete-npm-integration-for-meteor

Trong packages.json bạn

{ 
    "sendgrid": "1.4.0" 
} 

Sau đó, trong một tập tin mà chạy trên máy chủ:

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

Dưới đây là một phương pháp mẫu thiên thạch mà được url của một tập tin đính kèm (chúng tôi đang sử dụng S3) từ một tập hợp đính kèm. Phương pháp cụ thể này có thể gửi bất kỳ số lượng tệp đính kèm nào cho bất kỳ số người nhận nào. Có một số bối cảnh logic cụ thể ở đây nhưng nó sẽ là đủ để giúp bạn và chạy các file đính kèm.

Phần quan trọng:

var email = new sendgrid.Email(); 
email.setFrom("[email protected]"); 
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);     
    } 
}); 

Một phương pháp hoàn chỉnh ví dụ:

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; 
} 
}); 
Các vấn đề liên quan