gpt4 book ai didi

javascript - 使用带有附件的 SendGrid 的 Azure Function (JS)

转载 作者:行者123 更新时间:2023-11-30 20:33:53 24 4
gpt4 key购买 nike

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

  1. 为 SendGrid API key 创建了新的 AppSettings
  2. Azure Function 的 SendGrid 输出绑定(bind)集
  3. 以下是我的 Azure 函数

    module.exports = function (context, myQueueItem) {
    var message = {
    "personalizations": [ { "to": [ { "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4a0b1a7a0a0bb94a0b1a7a0fab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a>" } ] } ],
    from: { email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1e0f191e0c1805072a1e0f191e44090507" rel="noreferrer noopener nofollow">[email protected]</a>" },
    subject: "Azure news",
    content: [{
    type: 'text/plain',
    value: myQueueItem
    }]
    };
    context.done(null, {message});
    };

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

最佳答案

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

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="077562646e776e62697347627f666a776b6229687560" rel="noreferrer noopener nofollow">[email protected]</a>',
from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b5a3a8a2a3b486a3bea7abb6aaa3e8a9b4a1" rel="noreferrer noopener nofollow">[email protected]</a>',
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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aeeffe9eeeef5daeeffe9eeb4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>" } ] } ],
from: { email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f4e5f3f4e6f2efedc0f4e5f3f4aee3efed" rel="noreferrer noopener nofollow">[email protected]</a>" },
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});
};

关于javascript - 使用带有附件的 SendGrid 的 Azure Function (JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50041375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com