本文介绍了如何将文件附加到电子邮件与nodemailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有代码可以在 nodejs 中使用 nodemailer 发送电子邮件,但是我想将文件附加到电子邮件中,但是找不到我在网上搜索的方法但是我找不到有用的东西。有什么办法可以用这个文件附加文件,还是可以帮助我用nodemailer附加文件?

I have code that send email with nodemailer in nodejs but I want to attach file to an email but I can't find way to do that I search on net but I could't find something useful.Is there any way that I can attach files to with that or any resource that can help me to attach file with nodemailer?

var nodemailer = require('nodemailer');
var events = require('events');
var check =1;
var events = new events.EventEmitter();
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "gmail",
    auth: {
        user: "[email protected]",
        pass: "pass"
    }
});
function inputmail(){
    ///////Email
    const from = 'example<[email protected]>';
    const to  = '[email protected]';
    const subject  = 'example';
    const text = 'example email';
    const html = '<b>example email</b>';
    var mailOption = {
        from: from,
        to:  to,
        subject: subject,
        text: text,
        html: html
    }
    return mailOption;
}
function send(){
        smtpTransport.sendMail(inputmail(),function(err,success){
        if(err){
            events.emit('error', err);
        }
        if(success){
            events.emit('success', success);
        }
    });
}
///////////////////////////////////
send();
events.on("error", function(err){
    console.log("Mail not send");
    if(check<10)
        send();
    check++;
});
events.on("success", function(success){
    console.log("Mail send");
});


推荐答案

包含在var mailOption中的关键附件,如下:

Include in the var mailOption the key attachments, as follow:

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='
    }
]

}

选择适合您需求的选项。

Choose the option that adjust to your needs.

链接:

祝你好运!

这篇关于如何将文件附加到电子邮件与nodemailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:05