6

I created the following function with NodeMailer which seems to be despatching emails without problems ("Message sent" notification in console and email received) except there are no attachments with any emails sent!

Tried it with a bunch of email addresses (gmail, google apps, hotmail) but all are doing the same thing. Please help!

var sendWithAttachment = function(userMail, subject, html, attachment_path, cb){
  var smtpTransport = nodemailer.createTransport("SMTP",{
      service: "Gmail",
      auth: {
          user: "labs@domain.com",
          pass: "password"
      }
  });

  var mailOptions = {
      from: "Labs <labs@domain.com>",
      to: userMail,
      subject: subject || "[blank]"
      html: html || "[none]"
      generateTextFromHTML: true,
      attachments: [
          {   // filename and content type is derived from path
              path: attachment_path
          },
          {   // utf-8 string as an attachment
              filename: 'check.txt',
              content: 'checking that some attachments work...'
          },
      ],
  };

  smtpTransport.sendMail(mailOptions, function(error, response){
      if(error){
          console.log(error);
          cb(error, null);
      }else{
          console.log("Message sent: " + response.message);
          cb(null, response);
      }
      smtpTransport.close();
  });
};
socratic_singh
  • 183
  • 1
  • 13
  • use single object in attachments, either `path` or else. As `Path` - `path to a file or an URL (data uris are allowed as well) if you want to stream the file instead of including it (better for larger attachments)`. So Remove this part and try.... – Ravi Oct 07 '14 at 07:16
  • The docs say you can include multiple attachments like this. I tried with just one attachment and the problem is the same... – socratic_singh Oct 07 '14 at 18:56
  • If you've made any progress with this, please post your answer as I'm facing a similar issue. Attachment just isn't sending, but I receive the email. – Clint Powell Oct 30 '14 at 15:29

4 Answers4

11

It an issue in nodemailer's docs. Changing 'path' with 'filePath' to define path and change 'content' to 'contents' for text. Worked for me.

var mailOptions = {
    ...
    attachments: [
        {   // utf-8 string as an attachment
            filename: 'text1.txt',
            contents: 'hello world!'
        },
        {   // utf-8 string as an attachment
            filename: 'text1.txt',
            filePath: 'text.txt'
        },
    ]
}
Kazim Homayee
  • 739
  • 6
  • 8
4

I resolved this issue by renaming content to contents. I was reading the most up-to-date docs for a newer version of nodemailer. You can read the docs for versions less than 1.0 here: https://github.com/andris9/Nodemailer/blob/0.7/README.md

Clint Powell
  • 2,368
  • 2
  • 16
  • 19
1
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='
        }
    ]
}
Jijo Paulose
  • 1,896
  • 18
  • 20
1

// simple code by nodemailer to send an email with attachment

var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
 auth: {
      user: "test@gmail.com",
      pass: "passport"
   }
 });

fs.readFile("path/logfile.txt", function (err, data) {
    //creating simple built-in templating
    var templateSender = {
        from: 'MK <test@gmail.com>', // sender address
        to: 'reciever@gmail.com', // list of receivers
        subject: "Attachment", // Subject line
        body: 'mail content...',
        attachments: [{ filepath: 'path/logfile.txt', filename: 'logfile.txt', contents: data}]
    };

    // send mail with defined transport object
    smtpTransport.sendMail(templateSender, function(error, success){
        if(error){
            return console.log(error);
        }           
        smtpTransport.close();
    });

});
Maryam Koulaei
  • 750
  • 2
  • 9
  • 17