以HTML/EJS格式将图片发送到电子邮件可以通过以下步骤完成:
// 读取图片文件
const fs = require('fs');
const imageFile = fs.readFileSync('path/to/image.jpg');
// 将图片转换为Base64编码
const base64Image = Buffer.from(imageFile).toString('base64');
<img>
标签来嵌入图片。以下是一个示例HTML模板:<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>邮件标题</title>
</head>
<body>
<h1>邮件正文</h1>
<img src="data:image/jpeg;base64, <%= base64Image %>" alt="图片">
</body>
</html>
在上面的示例中,<img>
标签的src
属性使用了Base64编码的图片数据。
const nodemailer = require('nodemailer');
// 创建邮件传输对象
const transporter = nodemailer.createTransport({
service: 'your_email_service_provider',
auth: {
user: 'your_email',
pass: 'your_password'
}
});
// 准备邮件内容
const mailOptions = {
from: 'sender_email',
to: 'recipient_email',
subject: '邮件主题',
html: '邮件内容'
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('邮件发送成功:' + info.response);
}
});
在上面的示例中,需要替换your_email_service_provider
、your_email
、your_password
、sender_email
和recipient_email
为相应的值。
这样,你就可以使用HTML/EJS格式将图片嵌入到电子邮件中并发送出去了。请注意,具体的实现方式可能因编程语言、邮件库和邮件服务提供商而异,上述示例仅供参考。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云