Sendgrid-nodejs: 如何添加附件?

创建于 2016-09-06  ·  10评论  ·  资料来源: sendgrid/sendgrid-nodejs

我们的电子邮件发送正常,现在我们要添加附件。 因此我们复制了此示例: https :

并像这样制作代码:

const sendgrid: any = require('sendgrid')(process.env.sendgrid_key);

sendEmail(data: IEmailData): void {
    const email: any = new sendgrid.Email();
    email.addTo(data.emailTo);
    for (let attachment in data.attachments)
      this.addAttachment(email, attachment);
    sendgrid.send(email, (error: any, json: string) => {
      if (error)
        console.log(error);
    });
  }

  addAttachment(email: any, attachmentData: any): void {
    const attachment = new sendgrid.Attachment();
    attachment.setContent(attachmentData.content);
    attachment.setType(attachmentData.type);
    attachment.setFilename(attachmentData.filename);
    attachment.setDisposition(attachmentData.disposition);
    email.addAttachment(attachment);
  }

但是,当调用sendEmail我们会被告知sendgrid.Attachment is not a function (第二个函数的第一行)。

我打印出sendgrid对象,它看起来像这样:

{ api_key: 'SG.DaV0L',
  api_user: null,
  options: 
   { uriParts: 
      { protocol: 'https',
        host: 'api.sendgrid.com',
        port: '',
        endpoint: '/api/mail.send.json' },
     uri: 'https://api.sendgrid.com/api/mail.send.json' },
  version: '1.9.2',
  Email: [Function: Email],
  smtpapi: [Function: smtpapi],
  send: [Function] }
  • 这个例子错了吗?
  • 我们是否应该以其他方式获得附件帮助器类?

谢谢,
理查德

最有用的评论

所有10条评论

阅读代码后想通了。 github中的示例是完全错误的。 应该是这样的:

addAttachment(email: any, attachmentData: any): void {    
  email.addFile(attachmentData);
}

谢谢@RichardJECooke

我们将进一步调查并修复示例。 感谢您抽出宝贵时间分享解决方案!

如果您通过[email protected]向我们发送电子邮件,

谢谢!

我们现在在这里有一个附件示例: https :

嗨,我知道这个问题已经解决,但这是我尝试向gmail用户发送嵌入图像时遇到的一个问题。

我发现了一些原因,为什么gmail由于它们强加了一些额外的安全性而无法渲染图像,所以我认为我将使用attachments参数添加content_id并查看其是否有效,而不是使用常规的src标记。

发送后,发送网格将返回错误的请求。 这是我的方法。

我这样编码JPG:

    let bitmap = fs.readFileSync(imageDir);
    imageBase64URL = new Buffer(bitmap).toString('base64');

然后,将编码后的JPG传递到附件参数内的content字段中:

mailer.send({
       to: '[email protected]',
       from: '[email protected]',
       subject: `You have been removed from ${house.name} by ${removerName}`,
       html: result,
       attachments: [
            {
               content: imageBase64URL,
               filename: 'cluttr-logo.jpg',
               contentId: 'cluttr-logo'
            },
       ]
});

最后,我将自己的CID嵌入到我的ejs文件中:

html <img alt="Logo" src="cid:cluttr-logo" width="60" height="60"

希望有人对我做错了什么有所了解。 谢谢。

你好@ lvh1g15

我认为您需要像这样添加disposition: inline

最诚挚的问候,

埃尔默

@thinkingserious :您好,您知道如何使用事务模板发送附件吗?

@thinkingserious有什么方法可以将URL附件添加到电子邮件中,还有什么其他方法可以将附件添加到电子邮件中。 找不到确切的文件说明附件在Sendgrid上的工作方式。

问候,
莫希特

@thinkingserious :您好,您知道如何使用事务模板发送附件吗?

如果您使用的是v3 API,只需添加一个attachments键。 content必须是Base64编码

https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

email = {
   "from":{  
      "email":"[email protected]"
   },
   "personalizations":[  
      {  
         "to":[  
            {  
               "email" : "[email protected]"
            }
         ],
         "dynamic_template_data": {
         }
      }
   ],
   "attachments": [
       {
           "content": "",
           "filename" ""
       }
   ]
   "template_id":"someid"
}

示例链接不起作用。
这是新的链接:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md

新的新链接: https :

此页面是否有帮助?
0 / 5 - 0 等级