How to attach file to an email with nodemailer

2019-01-17 05:21发布

问题:

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 net but I could't find something useful.Is there any way that I can attach files to with that or any resource that can help me to attach file with nodemailer?

var nodemailer = require('nodemailer');
var events = require('events');
var check =1;
var events = new events.EventEmitter();
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "gmail",
    auth: {
        user: "example@gmail.com",
        pass: "pass"
    }
});
function inputmail(){
    ///////Email
    const from = 'example<example@gmail.com>';
    const to  = 'example@yahoo.com';
    const subject  = 'example';
    const text = 'example email';
    const html = '<b>example email</b>';
    var mailOption = {
        from: from,
        to:  to,
        subject: subject,
        text: text,
        html: html
    }
    return mailOption;
}
function send(){
        smtpTransport.sendMail(inputmail(),function(err,success){
        if(err){
            events.emit('error', err);
        }
        if(success){
            events.emit('success', success);
        }
    });
}
///////////////////////////////////
send();
events.on("error", function(err){
    console.log("Mail not send");
    if(check<10)
        send();
    check++;
});
events.on("success", function(success){
    console.log("Mail send");
});

回答1:

Include in the var mailOption the key attachments, as follow:

var mailOptions = {
...
attachments: [
    {   // utf-8 string as an attachment
        filename: 'text1.txt',
        content: 'hello world!'
    },
    {   // binary buffer as an attachment
        filename: 'text2.txt',
        content: new Buffer('hello world!','utf-8')
    },
    {   // file on disk as an attachment
        filename: 'text3.txt',
        path: '/path/to/file.txt' // stream this file
    },
    {   // filename and content type is derived from path
        path: '/path/to/file.txt'
    },
    {   // stream as an attachment
        filename: 'text4.txt',
        content: fs.createReadStream('file.txt')
    },
    {   // define custom content type for the attachment
        filename: 'text.bin',
        content: 'hello world!',
        contentType: 'text/plain'
    },
    {   // use URL as an attachment
        filename: 'license.txt',
        path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE'
    },
    {   // encoded string as an attachment
        filename: 'text1.txt',
        content: 'aGVsbG8gd29ybGQh',
        encoding: 'base64'
    },
    {   // data uri as an attachment
        path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
    }
]

}

Choose the option that adjust to your needs.

Link:Nodemailer Repository GitHub

Good Luck!!



回答2:

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, "<h1>Hello</h1>");

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



回答3:

If you are passing options object in mail composer constructor and attachment is on http server then it should look like:

const options = {
    attachments = [
      { // use URL as an attachment
        filename: 'xxx.jpg',
        path: 'http:something.com/xxx.jpg'
      }
    ]
}


回答4:

The alternative solution is to host your images online using a CDN and link to the online image source in your HTML, eg. <img src="list_image_url_here">.

(I had problems with nodemailer's image embedding using nodemailer version 2.6.0, which is why I figured out this workaround.)

An added benefit of this solution is that you're sending no attachments to nodemailer, so the sending process is more streamlined.



回答5:

var express = require('express');
var router = express(),
multer = require('multer'),
upload = multer(),
fs = require('fs'),
path = require('path');
nodemailer = require('nodemailer'),

directory = path.dirname("");
var parent = path.resolve(directory, '..');
// your path to store the files
var uploaddir = parent + (path.sep) + 'emailprj' + (path.sep) + 'public' + (path.sep) + 'images' + (path.sep);
/* GET home page. */
router.get('/', function(req, res) {
res.render('index.ejs', {
    title: 'Express'
});
});

router.post('/sendemail', upload.any(), function(req, res) {

var file = req.files;
console.log(file[0].originalname)
fs.writeFile(uploaddir + file[0].originalname, file[0].buffer,     function(err) {
    //console.log("filewrited")
    //console.log(err)
})
var filepath = path.join(uploaddir, file[0].originalname);
console.log(filepath)
    //return false;
nodemailer.mail({
    from: "yourgmail.com",
    to: req.body.emailId, // list of receivers
    subject: req.body.subject + " ✔", // Subject line
    html: "<b>" + req.body.description + "</b>", // html body
    attachments: [{
        filename: file[0].originalname,
        streamSource: fs.createReadStream(filepath)
    }]
});
res.send("Email has been sent successfully");
})
module.exports = router;


回答6:

var mailer = require('nodemailer');
mailer.SMTP = {
    host: 'host.com', 
    port:587,
    use_authentication: true, 
    user: 'you@example.com', 
    pass: 'xxxxxx'
};

Then read a file and send an email :

fs.readFile("./attachment.txt", function (err, data) {

    mailer.send_mail({       
        sender: 'sender@sender.com',
        to: 'dest@dest.com',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{'filename': 'attachment.txt', 'content': data}]
    }), function(err, success) {
        if (err) {
            // Handle error
        }

    }
});