本文介绍了使用带有附件的SendGrid的Azure Function(JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SendGrid从Azure函数(Javascript)发送带有附件的电子邮件.我已经完成了以下

I want to send emails with attachment from an Azure function (Javascript) using SendGrid. I have done the following

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

  1. created a new AppSettings for SendGrid API Key
  2. SendGrid output binding set of Azure Function
  3. Following is my Azure Function

module.exports = function (context, myQueueItem) {
var message = {
 "personalizations": [ { "to": [ { "email": "[email protected]" } ] } ],
from: { email: "[email protected]" },        
subject: "Azure news",
content: [{
    type: 'text/plain',
    value: myQueueItem
}]
};
context.done(null, {message});
};

电子邮件正在正确发送.但是我该如何添加附件?

Email is getting send correctly. But how do i add an attachment?

推荐答案

您可以尝试使用Sendgrid API中的以下代码段:

You can try following snippet from Sendgrid API:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: '[email protected]',
  from: '[email protected]',
  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": "[email protected]" } ] } ],
from: { email: "[email protected]" },        
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});
};

这篇关于使用带有附件的SendGrid的Azure Function(JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:20