我正在尝试构建一个以pdf格式发送电子邮件的功能,我需要从其他服务器读取文件,然后将其附加到电子邮件中。
我已经用txt测试了它,并且可以正常工作,但是当我使用pdf时,它会附加一个无法打开的文件。
到目前为止,这是我的代码:
let dados = {
"para": "::EMAIL::",
"body": "Olá",
"assunto": "Teste",
"from": "::EMAIL::",
"anexo": "teste.pdf" // Name of the file I want to read from server
};
request.get("::URL_SERVER::" + dados.anexo, function (error, response, body) {
let anexo;
if (!error && response.statusCode == 200) {
anexo = body;
}
let ses_mail =
`From: 'AWS SES Attchament Configuration' <${dados.from}>
To: <${dados.para}>
Subject: ${dados.assunto}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart"
--NextPart
Content-Type: text/html
${dados.body}
--NextPart
Content-Type: application/pdf; name="${dados.anexo}"
Content-Transfer-Encoding: base64
Content-Disposition:attachment
${anexo.toString("base64").replace(/([^\0]{76})/g, "$1\n")}
--NextPart`;
let params = {
RawMessage: {Data: ses_mail},
Source: `'AWS SES Attchament Configuration' <${dados.from}>`
};
let sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendRawEmail(params).promise();
return sendPromise.then(
data => {
console.log(data);
return data;
}).catch(
err => {
console.error(err.message);
throw err;
});
});
可以用axios做到吗?我只在我的研究中找到了如何下载文件
答案 0 :(得分:0)
我可以做到,但是我需要更改用于同步请求的库。 我的最终代码:
let anexo = null;
try {
anexo = request( "GET", "::URL::" + dados.anexo );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 404, 'Anexo não encontrado' );
}
anexo = anexo.getBody();
//return criarResposta( 200, anexo.toString("base64") );
let ses_mail =
`From: 'AWS SES Attchament Configuration' <${dados.from}>
To: <${dados.para}>
Subject: ${dados.assunto}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="NextPart"
--NextPart
Content-Type: text/html
${dados.body}
--NextPart
Content-Type: application/octet; name="arquivo.pdf"
Content-Transfer-Encoding: base64
Content-Disposition:attachment
${anexo.toString("base64")}
--NextPart`;
let params = {
RawMessage: {Data: ses_mail},
Source: `'AWS SES Attchament Configuration' <${dados.from}>`
};
sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendRawEmail(params).promise();
try {
const data = await sendPromise;
console.log(data.MessageId);
return criarResposta( 200, 'OK' );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 500, 'Erro interno' );
}