1

It looks like I am doing something probably most obviously wrong, but I couldn't find an answer suiting my needs. I want to send emails via SES with attachment via nodemailer on Lambda. The emails are getting send, but the attachment does not work. (There is some code inside the attachment but it's not as it should). For example, a PDF with a simple text on it just gets displayed as a blank PDF page.

In the future, I would like to send excel & csv files.

async function sendEmail() {
   
    const nodemailer = require('nodemailer');
    const fs = require('fs');
    
    const transporter = await nodemailer.createTransport({
        SES: new aws.SES()
    });

    await new Promise((resolve, reject) => {
        
        const mailOptions = {
            from: 'fromemail',
            to: 'toemail',
            subject: 'Message',
            text: 'I hope this message gets sent!',
            attachments: [
                {   // file on Lambda as an attachment
                    filename: 'text5.pdf',
                    path: __dirname + '/pdf_dummy.pdf',
                    //content: fs.createReadStream( __dirname + '/pdf_dummy.pdf'), // also tried this
                    //contentType: 'application/pdf' // also tried this
                }
            ]
        };
       
        transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                console.log("ERROR | err: " + err);
                return reject(err);
            } else {
                console.log("INFO | info: " + JSON.stringify(info));
                return resolve(info);
            }
        });
   });

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MJey
  • 345
  • 3
  • 16

0 Answers0