gpt4 book ai didi

node.js - 使用来自 node.js 的 AWS SES 在邮件中上传 .jpg 图像附件

转载 作者:搜寻专家 更新时间:2023-10-31 23:16:03 25 4
gpt4 key购买 nike

下面是来自 https://github.com/andrewpuch/aws-ses-node-js-examples 的代码其中有一个发送和电子邮件附件的例子,

我修改了代码以从 aws s3 获取图像文件并将其作为附件作为邮件发送,当我为文本文件执行此操作时效果很好,但是当我发送图像时,在邮件中我是无法看到图像,因为它已损坏。

当我尝试使用 apple photo 应用程序打开时,它显示缺少元数据,而且当我尝试使用 utf8、utf-8 和 UTF- 时,我还在邮件的标题中添加了 Content-Transfer-Encoding: base64- 8 在标题中的 Content-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

您还应该使用 Buffer.from(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 += Buffer.from(data.Body).toString('base64');
ses_mail += "--NextPart--";

然后,您可以将 ses_mail 字符串作为原始消息数据传递为 RawMessage: { Data: ses_mail } 而不是 RawMessage: { Data: new Buffer( ses_mail) }.

关于node.js - 使用来自 node.js 的 AWS SES 在邮件中上传 .jpg 图像附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42781486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com