亚马逊 ses.sendEmail - 如何附加 pdf 文件?
Amazon ses.sendEmail - how to attach pdf file?
我正在尝试使用 Amazon ses.sendEmail 在电子邮件中附加 pdf。但我不知道执行此操作的参数键。没有附件它工作完美。这是我试过的。
` var ses = new AWS.SES()
var params = {
Destination: {
ToAddresses: [
'xxx',
]
},
Message: {
Body: {
Html: {
Data: msg,
Charset: 'UTF-8'
}
},
Subject: { /* required */
Data: 'Test Mail',
Charset: 'UTF-8'
}
},
Attachment:{
},
Source: 'yyy'
};
ses.sendEmail(params, function(err, data) {
if (err) {// an error occurred}
oDialog.close();
MessageToast.show("Email not sent. Some problem occurred!");
}
else {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
});`
目前,您只能使用原始电子邮件发送附件 API,例如:
var ses_mail = "From: 'Your friendly UI5 developer' <" + email + ">\n"
+ "To: " + email + "\n"
+ "Subject: AWS SES Attachment Example\n"
+ "MIME-Version: 1.0\n"
+ "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
+ "--NextPart\n"
+ "Content-Type: text/html; charset=us-ascii\n\n"
+ "This is the body of the email.\n\n"
+ "--NextPart\n"
+ "Content-Type: text/plain;\n"
+ "Content-Disposition: attachment; filename=\"attachment.txt\"\n\n"
+ "Awesome attachment" + "\n\n"
+ "--NextPart";
var params = {
RawMessage: { Data: new Buffer(ses_mail) },
Destinations: [ email ],
Source: "'Your friendly UI5 developer' <" + email + ">'"
};
var ses = new AWS.SES();
ses.sendRawEmail(params, function(err, data) {
if(err) {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
else {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
});
对于任何其他想要向 SES 电子邮件添加附件的人,这是我在 NodeJS 中为 lambda 所做的:将 Nodemailer 与 SES 传输一起使用。
npm install --save nodemailer
在代码中:
// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
SES: new AWS.SES({
apiVersion: '2010-12-01',
region: "eu-west-1", // SES is not available in eu-central-1
})
});
const emailTransportAttachments = [];
if (attachments && attachments.length !== 0) {
emailTransportAttachments = attachments.map(attachment => ({
filename: attachment.fileName,
content: attachment.data,
contentType: attachment.contentType,
}));
}
const emailParams = {
from,
to,
bcc,
subject,
html,
attachments: emailTransportAttachments,
};
return new Promise((resolve, reject) => {
transporter.sendMail(emailParams, (error, info) => {
if (error) {
console.error(error);
return reject(error);
}
console.log('transporter.sendMail result', info);
resolve(info);
});
});
我正在尝试使用 Amazon ses.sendEmail 在电子邮件中附加 pdf。但我不知道执行此操作的参数键。没有附件它工作完美。这是我试过的。
` var ses = new AWS.SES()
var params = {
Destination: {
ToAddresses: [
'xxx',
]
},
Message: {
Body: {
Html: {
Data: msg,
Charset: 'UTF-8'
}
},
Subject: { /* required */
Data: 'Test Mail',
Charset: 'UTF-8'
}
},
Attachment:{
},
Source: 'yyy'
};
ses.sendEmail(params, function(err, data) {
if (err) {// an error occurred}
oDialog.close();
MessageToast.show("Email not sent. Some problem occurred!");
}
else {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
});`
目前,您只能使用原始电子邮件发送附件 API,例如:
var ses_mail = "From: 'Your friendly UI5 developer' <" + email + ">\n"
+ "To: " + email + "\n"
+ "Subject: AWS SES Attachment Example\n"
+ "MIME-Version: 1.0\n"
+ "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
+ "--NextPart\n"
+ "Content-Type: text/html; charset=us-ascii\n\n"
+ "This is the body of the email.\n\n"
+ "--NextPart\n"
+ "Content-Type: text/plain;\n"
+ "Content-Disposition: attachment; filename=\"attachment.txt\"\n\n"
+ "Awesome attachment" + "\n\n"
+ "--NextPart";
var params = {
RawMessage: { Data: new Buffer(ses_mail) },
Destinations: [ email ],
Source: "'Your friendly UI5 developer' <" + email + ">'"
};
var ses = new AWS.SES();
ses.sendRawEmail(params, function(err, data) {
if(err) {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
else {
oDialog.close();
MessageToast.show("Email sent successfully!");
}
});
对于任何其他想要向 SES 电子邮件添加附件的人,这是我在 NodeJS 中为 lambda 所做的:将 Nodemailer 与 SES 传输一起使用。
npm install --save nodemailer
在代码中:
// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
SES: new AWS.SES({
apiVersion: '2010-12-01',
region: "eu-west-1", // SES is not available in eu-central-1
})
});
const emailTransportAttachments = [];
if (attachments && attachments.length !== 0) {
emailTransportAttachments = attachments.map(attachment => ({
filename: attachment.fileName,
content: attachment.data,
contentType: attachment.contentType,
}));
}
const emailParams = {
from,
to,
bcc,
subject,
html,
attachments: emailTransportAttachments,
};
return new Promise((resolve, reject) => {
transporter.sendMail(emailParams, (error, info) => {
if (error) {
console.error(error);
return reject(error);
}
console.log('transporter.sendMail result', info);
resolve(info);
});
});