javascript - sending mail with image using sendgrid - Stack Overflow

"code for sending email above is the code .iam tired of scratching my head but still not able to s

"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";
  });

"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";
  });
Share Improve this question edited Dec 5, 2017 at 15:22 libik 23k10 gold badges51 silver badges92 bronze badges asked Dec 5, 2017 at 14:23 roshrosh 973 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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);

If I look to the https://sendgrid./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)

May be it is because of your mail client doesn't support for base 64 encoded images see this question

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750633a4591571.html

相关推荐

  • javascript - sending mail with image using sendgrid - Stack Overflow

    "code for sending email above is the code .iam tired of scratching my head but still not able to s

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信