Функция Azure (JS) с использованием SendGrid с вложением - PullRequest
0 голосов
/ 26 апреля 2018

Я хочу отправлять электронные письма с вложениями из функции Azure (Javascript), используя SendGrid. Я сделал следующее

  1. создал новый AppSettings для SendGrid API Key
  2. Набор выходных привязок SendGrid функции Azure
  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});
    };
    

Электронная почта отправляется правильно. Но как мне добавить вложение?

1 Ответ

0 голосов
/ 27 апреля 2018

Вы можете попробовать следующий фрагмент из 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});
};
...