Search code examples
amazon-sesaws-sdk-js

I can't send email with pdf using Amazon SDK JS, but send txt works fine?


I'm trying to build a function to send email with pdf, I need to read the file from other server and then attach it to the email.

I have test it with txt and it works fine, but when I use pdf, it attach a file that cannot be open.

That's my code until now:

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

It is possible to do it with axios? I only found how to download file on my research


Solution

  • I could do it, but I needed to change the lib I was using to sync-request. My final code:

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