以下是来自https://github.com/andrewpuch/aws-ses-node-js-examples的代码,其中有一个示例要发送并附带附件,
我已修改代码以从aws s3获取图像文件并将其作为附件以邮件方式发送,当我为文本文件执行它时它完美地工作,但是当我发送图像时,我在邮件中因为它已损坏而无法看到图像。
当我尝试打开苹果照片应用程序时,它显示缺少元数据,我在邮件标题中添加了Content-Transfer-Encoding:base64,当我尝试使用utf8,utf-8和UTF-时标题中的 Content-Transfer-Encoding 中的8我从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);
});
答案 0 :(得分:3)
首先,您的MIME邮件格式不正确。最后一行应为--NextPart--
,而不仅仅是--NextPart
。
您还应该使用data.Body
将new Buffer(data.Body).toString('base64')
数组转换为其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) }
。
答案 1 :(得分:1)
解决此问题的另一种方法是将您的 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
技术来最小化答案的复杂性。随意将构建调用包装在一个 promise 中,并async/wait您的缓冲区数据,然后您可以将这些数据分别传递给您的 ses.sendRawEmail
方法。