Wednesday, 24 May 2017

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 = 'test@test.com';

    var mailOptions={
        from : "test <test@test.com>",
        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.



via Haeun Cho

No comments:

Post a Comment