2011-01-12 131 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,他深入研究這個

相關問題