Search code examples
node.jsubuntunodemailer

How to send a gzip file using node_mailer


I've created a backup of my mongoDb database and saved it to a gzip file. located in /public/myfile.gzip

Now I want to send that file using node_mailer using the following script :

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth:{
        user: 'xyz@gmail.com',
        pass: 'xxxxxxx'
    }
});

let mailContent={
    from: 'Sender Name <xyz@gmail.com>',
    to: 'Receiver Name <receivername@gmail.com>',
    subject: 'First Node.js email',
    text: 'Hi,This is a test mail sent using Nodemailer',
    html: '<h1>You can send html formatted content using Nodemailer with attachments</h1>',
    attachments: [
        {
            filename: 'mygzip.gzip',
            path: __dirname + '/public/mygzip.gzip'
        }
    ]
};

transporter.sendMail(mailContent, function(error, data){
    if(err){
        console.log('Unable to send mail');
    }else{
        console.log('Email send successfully');
    }
});

The script is running in an ubuntu linux machine. The email sends perfectly fine but instead of a folder in my attachments i get a huge encrypted like string. Any idea how i can actually send a folder as an attachment? Thanks in advance.


Solution

  • As per the Nodemailer Documentation for Attachments you should specify the contentType of what you are sending. In your case I believe gzip falls under application/javascript.

    attachments: [
        {
            filename: 'mygzip.gzip',
            path: __dirname + '/public/mygzip.gzip',
            contentType: 'application/javascript'
        }
    ]