我有一个 azure 通信服务,每天发送大约数百封学生邀请电子邮件,我想使用 azure sdk 和 typescript 跟踪邮件分析数据,例如一天发送了多少封邮件、发送失败了多少封、已发送了多少封邮件以及打开了多少封邮件,知道如何开始吗?
我还没有在azure中尝试过任何sdk,即使它可以在其他云提供商中实现,也会很有帮助。
您可以使用 Azure 通信服务通过 Webhooks 触发针对聊天消息或来电等事件的 HTTP 请求。然后,您可以创建一个 TypeScript 函数来处理事件,例如通过 Twilio SendGrid 发送电子邮件。在 Azure 中设置 SendGrid,获取 API 密钥,并将其集成到您的 TypeScript 应用程序中。之后,您可以通过 Twilio SendGrid 仪表板或 API 访问跟踪数据以监控电子邮件指标。
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import * as sgMail from "@sendgrid/mail"; //SENDGRID LIB
// SET SENDGRID API KEY
const apiKey = process.env.SENDGRID_API_KEY;
sgMail.setApiKey(apiKey);
// AZURE()
const sendEmail: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
// Define email content
const emailContent = {
to: "[email protected]",
from: "[email protected]",
subject: "TEST EMAIL",
text: "WELCOME TO YOUR NEW COURSE: SENT VIA AZURE FUNCS AND SENDGRID.",
trackingSettings: {
clickTracking: { enable: true },
openTracking: { enable: true },
},
};
try {
// Send email
await sgMail.send(emailContent);
// Respond with success status
context.res = {
status: 200,
body: "EMAIL SENT SUCCESFULLY",
};
} catch (error) {
// Respond with error status
context.res = {
status: 500,
body: "EMAIL FAILED TO SEND",
};
}
};
export default sendEmail;