1

我正在使用电子邮件发送电子邮件,nodemailer并且在EJS. 当我在我的机器上渲染这个模板时,我也可以看到,logo/image但是当我将此模板发送到电子邮件地址时,电子邮件中没有显示图像。

我使用静态托管图像middleware,这里是middleware

app.use('/static', express.static(path.join(__dirname, 'public')))

有没有办法logo/image在电子邮件中设置?

4

1 回答 1

2

发送到电子邮件时,您必须将图像的完整路径提供给 ejs 模板

 // common function used for send different emails 

 async sendMail(to:string, template:string, subject:string) {
    //create transport to send mail
    var transporter = nodemailer.createTransport({
        service: config.get('SMTP_CONSTANTS.SERVICE'),
        auth: {
            user: config.get('SMTP_CONSTANTS.USERNAME'),
            pass: config.get('SMTP_CONSTANTS.PASSWORD')
        }
    });
    //define mail options
    var mailOptions = {
        from: 'Your Domain <support@domain.com>',
        to: to, // receiver
        subject: subject, // Subject line
        html: template
    };
    // send mail with defined transport object
    let response = await transporter.sendMail(mailOptions);
    return response;
}


 app.get('/test',(req,res)=>{
    let image_path = path.join(__dirname+'public/','logo.png');
    console.log('image_path==',image_path);

    let subject = 'Welcome To Website';
    let to = 'to send mail';
    let parameters = {
        name: 'James',
        image_path:image_path
    };
    let html = ejs.render(fs.readFileSync(__dirname+`/../views/auth/welcome.ejs`).toString(), parameters);
    let response:any = await this.sendMail(to, html, subject).then((data:any)=>{return data}).catch(error => { return error });
    return response;

});
于 2020-05-22T04:24:21.693 回答