Cloud Code Functions
Integrations
使用SendGrid邮件API集成Cloud Code函数指南
9 分
使用 sendgrid 邮件 api 介绍 本节解释了如何将 sendgrid 与 cloud code 函数集成。在完成本指南的逐步说明后,您将能够在您的应用程序中使用该函数,并在您的 ios 或 android 应用程序中调用它。 先决条件 要完成本教程,您需要: 在 back4app 创建的应用程序。 请遵循 创建新应用程序教程 以了解如何在 back4app 上创建应用程序。 与项目配置的 back4app 命令行工具。 请遵循 设置 cloud code 教程 以了解如何为项目设置云代码。 在 twilio 创建的帐户。 让我们开始吧! 我们将使用 sendgrid 编写一个函数,您将能够利用许多可能性,例如向我们的客户发送消息和配置参数以使用 sendgrid v3 rest api。 要了解如何在 sendgrid 中创建或访问帐户,请查看以下链接: 创建新帐户 登录到您的帐户 1 创建 sendgrid api 密钥 在开始编码之前,最重要的一步是创建正确的密钥以设置您的环境。在访问您的帐户后,在设置下拉菜单中找到 api 密钥 api 密钥 选项,如下图所示 之后,在右上角找到并选择一个标识以为 api 密钥名称 api 密钥名称 , 如下所示 正如您在上面的图像中所看到的,选择一个选项以允许 完全访问 api 密钥是必要的。 点击 创建并查看 创建并查看 以继续创建密钥后,您将能够看到如下屏幕 提示 请小心记录,因为无法找回。点击文本以复制它。 2 向云代码添加一个函数 使用 sendgrid api 的主要策略是创建一个名为 sendgridemail sendgridemail 的云代码函数,并从应用程序中调用它。 2 1 从 sendgrid 安装模块 创建一个名为 package json package json , 在这个文件中,您需要安装 twilio 模块,如下所示: 1 { 2 "dependencies" { 3 "@sendgrid/mail" " " 4 } 5 } 2 2 实现云代码 您应该注意,每个电子邮件字段都必须由应用程序发送——从主题到内容——作为参数。代码如下: parse server 3 x 1 parse cloud define("sendgridemail", async(request) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 try{ 16 await sgmail send(msg); 17 return 'ok' 18 } catch (e){ 19 return `error ${e message}` 20 } 21 22 }); parse server 2 x 1 parse cloud define("sendgridemail", (request, response) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 sgmail send(msg) then(() => { 16 response success("the message was sent!"); 17 }) 18 catch(error => { 19 //log friendly error 20 response error(error tostring()); 21 }); 22 }); 提示 请记得更改字段 从 从 和 reply to reply to 到您的个人信息。 然后有必要在应用程序中实现对云代码函数的调用。 3 调用云代码函数 在当前步骤中,我们可以使用两种可能性来调用我们的函数,它们是:android 和 ios(swift 和 objective c)。 android 1 map\<string, string> params = new hashmap<>(); 2 3 // create the fields "emailaddress", "emailsubject" and "emailbody" 4 // as strings and use this piece of code to add it to the request 5 params put("toemail", emailaddress); 6 params put("subject", emailsubject); 7 params put("body", emailbody); 8 9 parsecloud callfunctioninbackground("sendgridemail", params, new functioncallback\<object>() { 10 @override 11 public void done(object response, parseexception exc) { 12 if(exc == null) { 13 // the function executed, but still has to check the response 14 } 15 else { 16 // something went wrong 17 } 18 } 19 }); ios(swift) 1 pfcloud callfunctioninbackground("sendgridemail", withparameters \[ 2 // these fields have to be defined earlier 3 "toemail" toemail, 4 "subject" subject, 5 "body" body 6 ]) { (response, error) in 7 if error == nil { 8 // the function executed, but still has to check the response 9 } else { 10 // the function returned an error 11 } 12 } ios(objective c) 1 \[pfcloud callfunctioninbackground @"sendgridemail" 2 withparameters @{@"toemail" toemail, 3 @"subject" subject, 4 @"body" body} 5 block ^(nsstring myalertmsg, nserror error){ 6 if(!error) { 7 // the function executed, but still has to check the response 8 } 9 else { 10 // the function returned an error 11 } 12 } 13 ]; 4 完成! 这就是 sendgrid 的使用。请注意,您可能希望在允许任何人使用您的 sendgrid api 发送电子邮件之前使用某种身份验证。 如果您需要任何帮助或某个功能/链接无法正常工作,请通过聊天与我们的团队联系!