如何使用Nodemailer嵌入图像?

问题描述

我正在尝试使用nodemailer发送带有嵌入式图像的电子邮件

它可以正常工作并通过电子邮件发送电子邮件,但是图像没有出现?

我的文件夹结构

mailer-prototype
  |-- node_modules
  |-- hero.jpg
  |-- index.js

index.js文件

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  host: 'smtp.ethereal.email',port: 587,auth: {
    user: '[email protected]',pass: 'password123'
  }
});

var mailOptions = {
  from: '[email protected]',to: '[email protected]',subject: 'Sending Email using Node.js',text: 'That was easy!',html: '<h1>Welcome</h1><img src="cid:[email protected]" alt="Test" />',attachments: [
    {
      filename: 'hero.jpg',path: __dirname+'/hero.jpg',cid: '[email protected]' 
    }
  ]
};

transporter.sendMail(mailOptions,function(error,info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

有什么我想念的吗?
希望您能为您提供帮助。

谢谢, 萌

解决方法

问题似乎出在ethereal浏览器上的电子邮件平台上。

当我使用以下代码运行相同的代码时,我期望的工作按预期进行:

var transporter = nodemailer.createTransport({
  host: 'smtp-mail.outlook.com',secureConnection: false,port: 587,auth: {
    user: '[email protected]',pass: 'password123'
  }
});

我不确定为什么,但是你去了。

希望它对其他人有帮助。