"上面发送电子邮件的代码是代码。我厌倦了挠头但仍然无法在邮件中看到图像。我正在将图像转换为base 64编码的字符串,并且还遵循sendgrid语法仍然无法发送图像.i dunno wats在这里出错了。:"
var app = require("../../../server/server");
var base64Img = require("base64-img");
let status = null;
let textBody,
htmBody = null;
var DataSource = require("loopback-datasource-juggler").DataSource;
var dsSendGrid = new DataSource("loopback-connector-sendgrid", {
api_key: app.customConfig.mail.sendgrid.api_key
});
var fs = require("fs");
function base64_encode(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString("base64");
}
function base64_decode(base64str, file) {
var bitmap = new Buffer(base64str, "base64");
fs.writeFileSync(file, bitmap);
console.log(
"******** File created from base64 encoded string ********",
base64str
);
}
var base64str = base64_encode("../../../images/Campaign-images/Christmas.png");
let message = {
to: "somebody@gmail.com",
from: "noreply@gmail.com",
subject: "test",
text: "hi",
html: '<img src="cid:myimagecid"/>',
attachment: [
{
filename: "Christmas2.png",
content: base64str,
ContentId: "myimagecid"
}
]
};
console.log(message);
app.models.Email.send(message)
.then(result => {
return "sent";
})
.catch(err => {
console.log(err);
return "failed";
});
答案 0 :(得分:0)
如果我查看https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
我看到它是附件 s 而非附件
还有content_id而不是ContentId
另外请确保您使用的是正确的版本(我指的是v3,但我认为您可以选择使用v2)
答案 1 :(得分:0)
可能是因为您的邮件客户端不支持base 64编码图像,请参阅此question
答案 2 :(得分:0)
此代码肯定会起作用
//imageData= "data:image/png;base64,ine793nfdsf......."
imageb64 = imageData.replace('data:image/png;base64,' , '');
//remove data:image/png;base64,
const msg = {
to: 'example@gmail.com',
from: 'test@gmail.com',
subject: "image attached",
html :'<img src="cid:myimagecid"/>',
attachments: [
{
filename: "imageattachment.png",
content: imageb64,
content_id: "myimagecid",
}
]
};
sgMail.send(msg);