如何使用 node_mailer 发送 gzip 文件

How to send a gzip file using node_mailer

我已经创建了 mongoDb 数据库的备份并将其保存到一个 gzip 文件中。位于 /public/myfile.gzip

现在我想使用 node_mailer 使用以下脚本发送该文件:

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');
    }
});

脚本是 运行 在 ubuntu linux 机器上。电子邮件发送得很好,但我得到的不是附件中的文件夹,而是一个巨大的加密字符串。知道我如何实际发送文件夹作为附件吗?提前致谢。

根据 Nodemailer Documentation for Attachments,您应该指定要发送的内容的 contentType。在你的情况下,我相信 gzip 属于 application/javascript.

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