2011-01-12 129 views

回答

42

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

var mailer = require('nodemailer'); 
mailer.SMTP = { 
    host: 'host.com', 
    port:587, 
    use_authentication: true, 
    user: '[email protected]', 
    pass: 'xxxxxx' 
}; 

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

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

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

    } 
}); 
+1

在最后一行缺少'})'。我不能直接编辑,因为它少于6个字符... – 2015-06-20 22:12:20

+0

附件属性有一个类型。 “内容”不正确。应该是“内容”。 – PyroJoke 2016-01-19 17:11:03

+0

@PyroJoke修复它,谢谢你把它带起来 – Philippe 2016-01-19 19:33:09

1

您可以使用nodejs-phpmailer

+0

他使用Node.js的一部分,为什么建议他使用php解决方案? – zobi8225 2012-09-26 11:16:50

+0

我认为使用node.js,但是基于PHP,有点慢和坏..但我认为这个工作。 – 2014-04-14 15:19:20

3

你试过Nodemailer

Nodemailer supports

  • Unicode to use any characters
  • HTML contents as well as plain text alternative
  • Attachments
  • Embedded images in HTML
  • SSL (but not STARTTLS)
4

尝试nodemailer,例如试试这个:

var nodemailer = require('nodemailer'); 
    nodemailer.SMTP = { 
    host: 'mail.yourmail.com', 
    port: 25, 
    use_authentication: true, 
    user: '[email protected]', 
    pass: 'somepasswd' 
    }; 

    var message = { 
     sender: "[email protected]",  
     to:'[email protected]', 
     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));  
    }); 
0

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

1

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

0

我还没有使用它,但nodemailer(npm install nodemailer)看起来像你想要的。

1

另一个可供选择的库是emailjs

我在这里尝试了一些自己的建议,但运行代码抱怨说send_mail()和sendMail()是未定义的(尽管我只是简单地拷贝了&粘贴的代码并做了小小的调整)。我使用节点0.12.4和npm 2.10.1。我对电子邮件服务没有任何问题,这对我来说是非常有用的。而且它具有很好的附件包装,所以你可以根据自己的喜好轻松地附上各种方式,与此处的nodemailer示例相比。

0

发送使用Express-邮寄者(https://www.npmjs.com/package/express-mailer

发送PDF - >

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

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

发送图像 - >

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

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

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

伊夫连接我的代码,做了附件事情对我来说

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

PS感谢himanshu,他深入研究这个

相关问题