来自base64的sendgrid multipart / alternative image

时间:2013-07-20 17:20:47

标签: sendgrid email-headers

我无法使用Sendgrid(使用node.js)在html中发送带有嵌入式base64图像的电子邮件。我想我很亲密......

var base64img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQgAAAFXCAYAAABeLtDdAAAgAElEQVR4Xu19CXhURdb2ISsJgSQ';//shortened version

sendgrid.send({
    to      : 'address1@gmail.com',
    from    : 'address2@gmail.com',
    subject : 'email with image from base64 data',
    headers:{
      'Content-Type':
      'multipart/alternative; boundary="imagebase64"'+
      '--imagebase64'+
      'Content-Type: image/png; name="base64.png"'+
      'Content-Disposition: inline; filename="base64.png"'+
      'Content-Transfer-Encoding: base64'+
      'Content-ID: <0123456789>'+
      'Content-Location: base64.png'+
      base64img+
      '--imagebase64'
      },
    html    : '<img src="cid:0123456789"/>'
    });

有人可以提供帮助,因为我没有在sendgrid docs或google上找到示例关闭

1 个答案:

答案 0 :(得分:8)

FIXED!

  var base64img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQgAAAFXCAYAAABeLtDdAAAgAElEQVR4Xu19CXhURdb2ISsJgSQ';//shortened version
  var data=base64img.replace(/^data:image\/\w+;base64,/,"");
  var buffer=new Buffer(data,'base64');
  fs.writeFile('/home/engine/public_html/img/base64img.png',buffer,function(err){
    fs.readFile('public_html/img/base64img.png',function(err,data){


    sendgrid.send({
      to      : 'address11@gmail.com',
      from    : 'address2@gmail.com',
      subject : 'email with image from base64 data',
      files   : [{filename:'base64.png',content:data,cid:'1234567890'}],
      html    : '<img src="cid:1234567890"/>'
      },
    function(success,message){
      if(!success){console.log(message);}
      else{
        console.log('sent');
        fs.unlink('public_html/img/base64img.png');
        }
    });});});