Reputation: 97
"code for sending email above is the code .iam tired of scratching my head but still not able to see the image in the mail. I am converting the image into base 64 encoded string and also following the sendgrid syntax still not able to send the image .i dunno wats going wrong here.:"
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: "[email protected]",
from: "[email protected]",
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";
});
Upvotes: 2
Views: 9190
Reputation: 748
This code will work for sure
//imageData= "data:image/png;base64,ine793nfdsf......."
imageb64 = imageData.replace('data:image/png;base64,' , '');
//remove data:image/png;base64,
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: "image attached",
html :'<img src="cid:myimagecid"/>',
attachments: [
{
filename: "imageattachment.png",
content: imageb64,
content_id: "myimagecid",
}
]
};
sgMail.send(msg);
Upvotes: 2
Reputation: 124
May be it is because of your mail client doesn't support for base 64 encoded images see this question
Upvotes: 0
Reputation: 23049
If I look to the https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
I see it is attachments not attachment
Also there is content_id instead of ContentId
Also make sure, you are using right version (I am pointing to v3 but I think you can choose to use v2)
Upvotes: 1