[英]upload .jpg image attachment in mail using AWS SES from node.js
以下是來自https://github.com/andrewpuch/aws-ses-node-js-examples的代碼,其中有一個示例可以發送和通過電子郵件發送附件,
我修改了代碼以從 aws s3 獲取圖像文件並將其與郵件作為附件一起發送,當我為文本文件執行此操作時,它運行良好,但是當我發送圖像時,在郵件中我無法看到圖像,因為它已損壞。
當我嘗試使用蘋果照片應用程序打開時,它顯示缺少元數據,我還在郵件標題中添加了 Content-Transfer-Encoding: base64,當我在Content 中嘗試使用 utf8、utf-8 和 UTF-8 時-Transfer-Encoding在標題中我從 aws 得到以下響應
{
"message": "Unknown encoding: utf8",
"code": "InvalidParameterValue",
"time": "2017-03-14T08:42:43.571Z",
"requestId": "2e220c33-0892-11e7-8a5a-1114bbc28c3e",
"statusCode": 400,
"retryable": false,
"retryDelay": 29.798455792479217
}
我修改后的代碼用於發送帶有郵件的圖像附件,我什至嘗試將緩沖區編碼為 utf-8、base-64,在此上浪費了足夠的時間,不知道為什么它已損壞,如果有人以前這樣做過,請幫助我
// Require objects.
var express = require('express');
var app = express();
var aws = require('aws-sdk');
// Edit this with YOUR email address.
var email = "*******@gmail.com";
// Load your AWS credentials and try to instantiate the object.
aws.config.loadFromPath(__dirname + '/config.json');
// Instantiate SES.
var ses = new aws.SES();
var s3 = new aws.S3();
// Verify email addresses.
app.get('/verify', function (req, res) {
var params = {
EmailAddress: email
};
ses.verifyEmailAddress(params, function (err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});
});
// Listing the verified email addresses.
app.get('/list', function (req, res) {
ses.listVerifiedEmailAddresses(function (err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});
});
// Deleting verified email addresses.
app.get('/delete', function (req, res) {
var params = {
EmailAddress: email
};
ses.deleteVerifiedEmailAddress(params, function (err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});
});
// Sending RAW email including an attachment.
app.get('/send', function (req, res) {
var params = { Bucket: 's3mailattachments', Key: 'aadhar.jpg' };
var attachmentData;
s3.getObject(params, function (err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else {
console.log(data.ContentLength);
console.log(data.ContentType);
console.log(data.Body);
var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n";
ses_mail = ses_mail + "To: " + email + "\n";
ses_mail = ses_mail + "Subject: AWS SES Attachment Example\n";
ses_mail = ses_mail + "MIME-Version: 1.0\n";
ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n";
ses_mail = ses_mail + "This is the body of the email.\n\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: image/jpeg; \n";
ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"aadhar.jpg\"\n";
ses_mail = ses_mail + "Content-Transfer-Encoding: base64\n\n"
ses_mail = ses_mail + data.Body;
ses_mail = ses_mail + "--NextPart";
var params = {
RawMessage: { Data: new Buffer(ses_mail) },
Destinations: [email],
Source: "'AWS Tutorial Series' <" + email + ">'"
};
ses.sendRawEmail(params, function (err, data) {
if (err) {
res.send(err);
}
else {
res.send(data);
}
});
}
});
});
// Start server.
var server = app.listen(3003, function () {
var host = server.address().address;
var port = server.address().port;
console.log('AWS SES example app listening at http://%s:%s', host, port);
});
首先,您的 MIME 消息格式不正確。 最后一行應該是--NextPart--
而不僅僅是--NextPart
。
您還應該使用new Buffer(data.Body).toString('base64')
將data.Body
數組轉換為其 base64 字符串表示,如下所示:
var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n";
ses_mail += "To: " + email + "\n";
ses_mail += "Subject: AWS SES Attachment Example\n";
ses_mail += "MIME-Version: 1.0\n";
ses_mail += "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
ses_mail += "--NextPart\n";
ses_mail += "Content-Type: text/html; charset=us-ascii\n\n";
ses_mail += "This is the body of the email.\n\n";
ses_mail += "--NextPart\n";
ses_mail += "Content-Type: image/jpeg; \n";
ses_mail += "Content-Disposition: attachment; filename=\"aadhar.jpg\"\n";
ses_mail += "Content-Transfer-Encoding: base64\n\n"
ses_mail += new Buffer(data.Body).toString('base64');
ses_mail += "--NextPart--";
然后,您可以將ses_mail
字符串作為原始消息數據傳遞為RawMessage: { Data: ses_mail }
而不是RawMessage: { Data: new Buffer(ses_mail) }
。
解決此問題的另一種方法是將您的 nodemailer MailOptions 參數(內聯圖像附件cid:aadhar和所有)傳遞到 nodemailer composer 中,為您生成緩沖區數據,如下所示:
import MailComposer from 'nodemailer/lib/mail-composer';
new MailComposer( mailOptions )
.compile()
.build(( err, Data ) => {
if( err !== null ) {
process.stderr.write( err ); // for example
return;
}
ses.sendRawEmail({
RawMessage: {
Data
}
});
});
Ps:我使用笨重的callback
技術來最小化答案的復雜性。 隨意將構建調用包裝在一個承諾中並異步/等待您的緩沖區數據,然后您可以將這些數據分別傳遞給您的ses.sendRawEmail
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.