使用带附件的SendGrid的Azure功能(JS)

时间:2018-04-26 11:05:26

标签: javascript azure azure-functions sendgrid

我想使用SendGrid从Azure功能(Javascript)发送带附件的电子邮件。我做了以下

  1. 为SendGrid API密钥创建了新的AppSettings
  2. Azure功能的SendGrid输出绑定集
  3. 以下是我的Azure功能

    module.exports = function (context, myQueueItem) {
    var message = {
     "personalizations": [ { "to": [ { "email": "testto@test.com" } ] } ],
    from: { email: "testfrom@test.com" },        
    subject: "Azure news",
    content: [{
        type: 'text/plain',
        value: myQueueItem
    }]
    };
    context.done(null, {message});
    };
    
  4. 电子邮件正在正确发送。但是我如何添加附件?

1 个答案:

答案 0 :(得分:1)

您可以尝试从Sendgrid API获取以下代码段:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.org',
  from: 'sender@example.org',
  subject: 'Hello attachment',
  html: '<p>Here’s an attachment for you!</p>',
  attachments: [
    {
      content: 'Some base 64 encoded attachment content',
      filename: 'some-attachment.txt',
      type: 'plain/text',
      disposition: 'attachment',
      contentId: 'mytext'
    },
  ],
};

所以在你的背景下:

module.exports = function (context, myQueueItem) {
var message = {
 "personalizations": [ { "to": [ { "email": "testto@test.com" } ] } ],
from: { email: "testfrom@test.com" },        
subject: "Azure news",
content: [{
    type: 'text/plain',
    value: myQueueItem
}],
attachments: [
    {
      content: 'Some base 64 encoded attachment content',
      filename: 'some-attachment.txt',
      type: 'plain/text',
      disposition: 'attachment',
      contentId: 'mytext'
    },
  ]
};
context.done(null, {message});
};