How to attach file to an email with nodemailer

前端 未结 8 2124
南笙
南笙 2020-12-04 19:49

I have code that send email with nodemailer in nodejs but I want to attach file to an email but I can\'t find way to do that I search on ne

8条回答
  •  日久生厌
    2020-12-04 20:26

    I've tested each of these attachments methods and none is ok for me. Here's my mailer function code without the smtp transport config :

    function mailer(from, to, subject, attachments, body) {
    
        // Setup email
        var mailOptions = {
            from: from,
            to: to,
            subject: subject,
            attachments: attachments,
            html: body
        };
    
        // send mail with defined transport object
        smtpTransport.sendMail(mailOptions, function(error, response){
            if(error) console.log(error);
            else console.log("Message sent: " + response.message);
            // shut down the connection pool, no more messages
            smtpTransport.close();
        });
    }
    

    And then the call :

    var attachments = [{ filename: 'test.pdf', path: __dirname + '/pdf/test.pdf', contentType: 'application/pdf' }];
    mailer("exped@gmail.com", "mymail@gmail.com", "Test", attachments, "

    Hello

    ");

    The mail comes successfully but with no attachment. Even if I set a string or buffer attachment it's the same result.

提交回复
热议问题