Haeun Cho
Haeun Cho

Reputation: 11

nodemailer attachment is not working

i want to attach zip file. but it doesn't work any attachment.

here is my source code.

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var fs = require('fs');
var mailinfo = require('../config/mail_info').info;

var smtpTransport = nodemailer.createTransport({
    host: mailinfo.host,
    port: mailinfo.port,
    auth: mailinfo.auth,
    tls: mailinfo.tls,
    debug: true,
});

router.post('/',function(req,res){
    var emailsendee = req.body.emailAddress;
    console.log(emailsendee);
    var emailsubject = "Requested File";
    var emailText = "test";
    var emailFrom = '[email protected]';

    var mailOptions={
        from : "test <[email protected]>",
        to : emailsendee,
        subject : emailsubject,
        html : '<h1>' + emailText+ '</h1>';
        attachments : [
             {
                filename : '',//i just put black make you understand esaily
                path : ''//what i did is under this code
             }
         ]
     };

     console.log(mailOptions);
     smtpTransport.sendMail(mailOptions, function(error, response){
     if(error){
        console.log(error);
        res.end();
     }else{
         console.log(response);
         res.end();
     } 
 }); 
 });

 module.exports = router;

i tried these for attaching a file

enter code here
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]

it still sends a mail without attachment. when this code can't read a file there is a error. so i guess this isn't working because of reading a file. and i read some questions on stackoverflow which has similar error with me.

i fixed path -> filepath and fixed streamSource -> path my nodemailer version is 4.0.1. help me send a mail with zip file.

Upvotes: 1

Views: 6949

Answers (1)

codtex
codtex

Reputation: 6558

I'm using exactly the same version of nodemailer(4.0.1 at this moment) and I'm sending emails with attachments successfully.

Your first code snippet looks promising :)

But the second part

i tried these for attaching a file

attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]

doesn't look right at all ...

Please refer to nodemailer docs

fileName and streamSource are not a valid parameters of mailOptions object

EXAMPLE FROM DOCS

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/nodemailer/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='
        },
        {
            // use pregenerated MIME node
            raw: 'Content-Type: text/plain\r\n' +
                 'Content-Disposition: attachment;\r\n' +
                 '\r\n' +
                 'Hello world!'
        }
    ]
}

as you can see you should change fileName to filename and streamSource to content

// WRONG
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]

// RIGHT
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}]

Good Luck! I hope this was helpful for you :)

Upvotes: 4

Related Questions