我无法使用 Amazon SDK JS 发送带有 pdf 的电子邮件,但发送 txt 工作正常?



我正在尝试构建一个与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(/([^]{76})/g, "$1n")}
--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进行吗?我只找到了如何在我的研究上下载文件

我可以做到这一点,但是我需要更改我使用的lib来同步。我的最终代码:

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' );
        }

最新更新