通过NodeJS发送带附件的邮件

时间:2011-01-12 19:06:39

标签: email node.js attachment

是否有任何用于发送附件邮件的NodeJS库?

13 个答案:

答案 0 :(得分:48)

是的,很简单, 我使用nodemailer:npm install nodemailer --save

var mailer = require('nodemailer');
mailer.SMTP = {
    host: 'host.com', 
    port:587,
    use_authentication: true, 
    user: 'you@example.com', 
    pass: 'xxxxxx'
};

然后阅读文件并发送电子邮件:

fs.readFile("./attachment.txt", function (err, data) {

    mailer.send_mail({       
        sender: 'sender@sender.com',
        to: 'dest@dest.com',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{'filename': 'attachment.txt', 'content': data}]
    }), function(err, success) {
        if (err) {
            // Handle error
        }

    }
});

答案 1 :(得分:4)

尝试使用nodemailer,例如尝试:

  var nodemailer = require('nodemailer');
  nodemailer.SMTP = {
     host: 'mail.yourmail.com',
     port: 25,
     use_authentication: true,
     user: 'info@youdomain.com',
     pass: 'somepasswd'
   };

  var message = {   
        sender: "sender@domain.com",    
        to:'somemail@somedomain.com',   
        subject: '',    
        html: '<h1>test</h1>',  
        attachments: [  
        {   
            filename: "somepicture.jpg",    
            contents: new Buffer(data, 'base64'),   
            cid: cid    
        }   
        ]   
    };

最后,发送消息

    nodemailer.send_mail(message,   
      function(err) {   
        if (!err) { 
            console.log('Email send ...');
        } else console.log(sys.inspect(err));       
    });

答案 2 :(得分:3)

您是否尝试过Nodemailer

  

Nodemailer支持

     
      
  • Unicode使用任何字符
  •   
  • HTML内容以及纯文本替代
  •   
  • 附件
  •   
  • 以HTML格式嵌入图片
  •   
  • SSL(但不是STARTTLS)
  •   

答案 3 :(得分:3)

我个人使用Amazon SES rest API或Sendgrid rest API,这是最一致的方法。

如果您需要低级别方法,请使用https://github.com/Marak/node_mailer并设置您自己的smtp服务器(或您也可以访问的服务器)

http://blog.nodejitsu.com/sending-emails-in-node

答案 4 :(得分:1)

您可以使用nodejs-phpmailer

答案 5 :(得分:1)

您也可以使用AwsSum的Amazon SES库:

在那里,有一个名为SendEmail和SendRawEmail的操作,后者可以通过服务发送附件。

答案 6 :(得分:1)

使用邮件包它非常灵活和容易。

答案 7 :(得分:1)

另一个尝试的替代库是emailjs

我在这里尝试了一些建议但是运行代码抱怨send_mail()和sendMail()未定义(即使我只是通过微调来复制和粘贴代码)。我使用节点0.12.4和npm 2.10.1。我对emailjs没有任何问题,这对我来说是现成的。它有很好的附件包装,所以你可以根据自己喜欢的方式附加各种方式,与此处的nodemailer示例相比。

答案 8 :(得分:0)

任何nodejs邮件需求的Nodemailer。这是目前最好的:D

答案 9 :(得分:0)

我没有使用过它,但是nodemailer(npm install nodemailer)看起来就像你想要的那样。

答案 10 :(得分:0)

发送快递邮件(https://www.npmjs.com/package/express-mailer

发送PDF - &gt;

var pdf="data:application/pdf;base64,JVBERi0xLjM..etc"

attachments: [  {  filename: 'archive.pdf',
                  contents: new Buffer(pdf.replace(/^data:application\/(pdf);base64,/,''), 'base64')
                 }   
             ]

发送图片 - &gt;

var img = 'data:image/jpeg;base64,/9j/4AAQ...etc'
attachments: [  
             {  
               filename: 'myImage.jpg',
               contents: new Buffer(img.replace(/^data:image\/(png|gif|jpeg);base64,/,''), 'base64')
               }   
             ]

发送txt - &gt;

attachments: [  
             {  
               filename: 'Hello.txt',
               contents: 'hello world!'
               }   
             ]

答案 11 :(得分:0)

你可以使用谷歌的官方api。 他们为此提供了节点包。 google official api

我附加了部分代码,为我做了附件事

&#13;
&#13;
function makeBody(subject, message) {
var boundary = "__myapp__";
var nl = "\n";
var attach = new Buffer(fs.readFileSync(__dirname + "/../"+fileName)) .toString("base64");
// console.dir(attach);
var str = [

        "MIME-Version: 1.0",
        "Content-Transfer-Encoding: 7bit",
        "to: " + receiverId,
        "subject: " + subject,
        "Content-Type: multipart/alternate; boundary=" + boundary + nl,
        "--" + boundary,
        "Content-Type: text/plain; charset=UTF-8",
        "Content-Transfer-Encoding: 7bit" + nl,
        message+ nl,
        "--" + boundary,
        "--" + boundary,
        "Content-Type: Application/pdf; name=myPdf.pdf",
        'Content-Disposition: attachment; filename=myPdf.pdf',
        "Content-Transfer-Encoding: base64" + nl,
        attach,
        "--" + boundary + "--"

    ].join("\n");

    var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
    return encodedMail;
}
&#13;
&#13;
&#13;

感谢himanshu对他的深入研究

答案 12 :(得分:0)

答案未更新为nodemailer@6.x的最新版本

这里有一个更新的示例:

const fs = require('fs')
const path = require('path')

const nodemailer = require('nodemailer')
const transport = nodemailer.createTransport({
  host: 'smtp.libero.it',
  port: 465,
  secure: true,
  auth: {
    user: 'email@libero.it',
    pass: 'HelloWorld'
  }
})


fs.readFile(path.join(__dirname, 'test22.csv'), function (err, data) {
  transport.sendMail({
    from: 'email_from@libero.it',
    to: 'email_to@libero.it',
    subject: 'Attachment',
    text: 'mail content...', // or body: field
    attachments: [{ filename: 'attachment.txt', content: data }]
  }, function (err, success) {    
    if (err) {
      // Handle error
      console.log(err)
      return
    }
    console.log({ success })
  })
})