【问题标题】:Azure Function (JS) using SendGrid with attachment使用带有附件的 SendGrid 的 Azure 函数 (JS)
【发布时间】:2018-10-07 01:23:40
【问题描述】:

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

  1. 为 SendGrid API Key 创建了一个新的 AppSettings
  2. Azure Function 的 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});
    };
    

电子邮件正在正确发送。但是如何添加附件呢?

【问题讨论】:

    标签: javascript azure azure-functions sendgrid


    【解决方案1】:

    您可以尝试使用 Sendgrid API 中的 sn-p:

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

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2016-10-23
      • 2022-06-30
      相关资源
      最近更新 更多