Cloud Code Functions
Integrations
การพัฒนาฟังก์ชั่นส่งอีเมลผ่าน SendGrid API
10 นาที
การใช้ sendgrid email api บทนำ ส่วนนี้อธิบายวิธีการที่คุณสามารถรวม sendgrid กับฟังก์ชัน cloud code หลังจากที่คุณทำตามคำแนะนำนี้อย่างละเอียด คุณจะพร้อมที่จะใช้ฟังก์ชันของคุณในแอปของคุณและเรียกใช้มันที่แอป ios หรือ android ของคุณ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้อง แอปที่สร้างขึ้นที่ back4app ติดตาม บทเรียนการสร้างแอปใหม่ เพื่อเรียนรู้วิธีการสร้างแอปที่ back4app back4app command line ที่กำหนดค่าไว้กับโปรเจกต์ ติดตาม บทเรียนการตั้งค่า cloud code เพื่อเรียนรู้วิธีการตั้งค่า cloud code สำหรับโปรเจกต์ บัญชีที่สร้างใน twilio เริ่มกันเลย! เราจะเขียนฟังก์ชันโดยใช้ sendgrid ที่คุณจะสามารถทำงานได้มากมายจากความสามารถในการส่งข้อความถึงลูกค้าของเราและกำหนดค่าพารามิเตอร์เพื่อใช้ sendgrid v3 rest api เพื่อเรียนรู้วิธีการสร้างหรือเข้าถึงบัญชีใน sendgrid โปรดตรวจสอบลิงก์ที่ให้ไว้ด้านล่าง สร้างบัญชีใหม่ เข้าสู่ระบบบัญชีของคุณ 1 สร้าง api key ของ sendgrid ขั้นตอนที่สำคัญที่สุดก่อนเริ่มเขียนโค้ดคือการสร้างคีย์ที่ถูกต้องเพื่อกำหนดค่าบริการของคุณ หลังจากเข้าสู่บัญชีของคุณ ให้ค้นหาในเมนูดรอปดาวน์การตั้งค่า api keys api keys ตามภาพด้านล่าง หลังจากนั้น ที่มุมขวาบนให้ค้นหาและเลือกการระบุชื่อสำหรับ api key name api key name , ตามที่แสดงด้านล่าง ตามที่คุณเห็นในภาพด้านบน จำเป็นต้องเลือกตัวเลือกหนึ่งเพื่ออนุญาตให้ full access สำหรับ api key หลังจากคลิกที่ create & view create & view เพื่อดำเนินการสร้างคีย์ คุณจะสามารถเห็นหน้าจอด้านล่าง คำแนะนำ โปรดระมัดระวังในการเขียนลงไป เพราะไม่มีวิธีการกู้คืนมัน คลิกที่ข้อความเพื่อคัดลอก 2 เพิ่มฟังก์ชันใน cloud code กลยุทธ์หลักในการใช้ api ของ sendgrid นี้คือการสร้างฟังก์ชันใน cloud code ชื่อ sendgridemail sendgridemail และเรียกใช้จากแอป 2 1 ติดตั้งโมดูลจาก sendgrid สร้างไฟล์ชื่อ package json package json , และภายในไฟล์นี้ คุณต้องติดตั้งโมดูล twilio เช่น 1 { 2 "dependencies" { 3 "@sendgrid/mail" " " 4 } 5 } 2 2 นำไปใช้ cloud code คุณควรทราบว่าทุกฟิลด์อีเมลต้องถูกส่งโดยแอป – ตั้งแต่หัวเรื่องไปจนถึงเนื้อหา – เป็นพารามิเตอร์ โค้ดมีดังนี้ 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 เป็นข้อมูลส่วนตัวของคุณ จากนั้นจำเป็นต้องเรียกใช้ฟังก์ชัน cloud code ในแอป 3 เรียกฟังก์ชัน cloud code ในขั้นตอนปัจจุบัน เราสามารถทำงานกับสองทางเลือกในการเรียกฟังก์ชันของเรา ได้แก่ 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 โปรดทราบว่าคุณอาจต้องการใช้การตรวจสอบสิทธิ์บางประเภทก่อนที่จะอนุญาตให้ใครก็ตามใช้ api ของ sendgrid ของคุณในการส่งอีเมล ในกรณีที่คุณต้องการความช่วยเหลือหรือฟังก์ชัน/ลิงก์ไม่ทำงาน โปรดติดต่อทีมงานของเราผ่านการแชท!