回答
是的,這是很簡單的, 我用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
}
}
});
您可以使用nodejs-phpmailer
他使用Node.js的一部分,爲什麼建議他使用php解決方案? – zobi8225 2012-09-26 11:16:50
我認爲使用node.js,但是基於PHP,有點慢和壞..但我認爲這個工作。 – 2014-04-14 15:19:20
你試過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)
我個人使用Amazon SES休息API或Sendgrid其餘API是最一致的方式來做到這一點。
如果你需要一個低級別的方法使用https://github.com/Marak/node_mailer和建立自己的SMTP服務器(或一個你訪問過)
舊的答案,但是API如何比本地sendmail服務器更加一致......? – 2014-02-21 23:47:04
您還可以使用AwsSum亞馬遜SES庫:
在那裏,有一個名爲SendEmail和SendRawEmail的操作,其中後者可以通過該服務發送附件。
嘗試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));
});
Nodemailer任何郵件的NodeJS需求。這是目前最好的:D
使用郵件包,它非常靈活和容易。
我還沒有使用它,但nodemailer(npm install nodemailer
)看起來像你想要的。
另一個可供選擇的庫是emailjs。
我在這裏嘗試了一些自己的建議,但運行代碼抱怨說send_mail()和sendMail()是未定義的(儘管我只是簡單地拷貝了&粘貼的代碼並做了小小的調整)。我使用節點0.12.4和npm 2.10.1。我對電子郵件服務沒有任何問題,這對我來說是非常有用的。而且它具有很好的附件包裝,所以你可以根據自己的喜好輕鬆地附上各種方式,與此處的nodemailer示例相比。
發送使用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!'
}
]
你可以使用官方這個谷歌的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,他深入研究這個
- 1. 通過VBA發送帶有附件的Outlook電子郵件
- 2. 通過AppleScript發送帶有附件的電子郵件
- 3. 如何通過Unix發送帶有附件的HTML郵件Shellscripting
- 4. 通過PHPMailer發送電子郵件(附帶上傳附件)
- 5. 發送郵件帶有附件
- 6. 發送郵件附件通過PHP PEAR
- 7. 通過附件從unix發送郵件
- 8. 通過phpmailer發送郵件附件
- 9. 通過GMailSender發送帶附件的電子郵件?
- 10. Python:僅通過stdlib發送帶附件的電子郵件?
- 11. 通過郵件發送帶有多個附件的郵件問題()php。只發送文本文件,否pdf
- 12. 通過帶有java郵件API的struts發送電子郵件
- 13. Mailgun發送帶附件的郵件
- 14. 嘗試發送帶附件的郵件
- 15. 帶附件的發送郵件消息
- 16. 發送帶附件的電子郵件
- 17. 無法發送帶附件的郵件
- 18. 發送帶附件的電子郵件
- 19. 如何使用iphone通過SMTP發送帶有附件的郵件
- 20. 通過SendGrid使用Ruby on Rails發送帶有附件的電子郵件
- 21. 如何通過SMTP從C++發送帶有附件的電子郵件?
- 22. 通過多封郵件發送大郵件附件
- 23. 無法通過CakePHP郵件發送電子郵件附件PDF
- 24. 通過PHPMailer通過URL附件發送電子郵件
- 25. 附加文件通過電子郵件發送通過EXCEL
- 26. 通過電子郵件發送帶有圖片的html文件
- 27. PDF附件未通過郵件接收,而通過IPad發送郵件
- 28. 帶附件的郵件不能發送HTML郵件
- 29. 發送帶有附件的電子郵件,發送一個空白文件
- 30. 沒有發送郵件(phpmailer),在窗口上,帶有附件
在最後一行缺少'})'。我不能直接編輯,因爲它少於6個字符... – 2015-06-20 22:12:20
附件屬性有一個類型。 「內容」不正確。應該是「內容」。 – PyroJoke 2016-01-19 17:11:03
@PyroJoke修復它,謝謝你把它帶起來 – Philippe 2016-01-19 19:33:09