Cloud Code Functions
Integrations
SendGrid API: 코드와 통합하여 이메일 전송 자동화하기
10 분
sendgrid 이메일 api 사용하기 소개 이 섹션에서는 sendgrid를 cloud code 함수와 통합하는 방법을 설명합니다 단계별 지침을 따라 이 가이드를 완료하면, 앱에서 함수를 사용하고 ios 또는 android 앱에서 호출할 준비가 됩니다 전제 조건 이 튜토리얼을 완료하려면 다음이 필요합니다 back4app에서 생성된 앱 새 앱 만들기 튜토리얼 을 따라 back4app에서 앱을 만드는 방법을 배우세요 프로젝트에 구성된 back4app 명령줄 클라우드 코드 설정 튜토리얼 을 따라 프로젝트에 대한 클라우드 코드를 설정하는 방법을 배우세요 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 }); 힌트 필드를 변경하는 것을 잊지 마세요 from from 및 reply to reply to 당신의 개인 정보로 그런 다음 앱에서 cloud code 함수를 호출하는 것이 필요합니다 3 cloud code 함수 호출 현재 단계에서는 두 가지 가능성을 가지고 함수를 호출할 수 있습니다 그것은 android와 ios(스위프트 및 오브젝티브 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를 사용하도록 허용하기 전에 어떤 형태의 인증을 사용하는 것이 좋습니다 도움이 필요하시거나 함수/링크가 작동하지 않는 경우, 채팅을 통해 저희 팀에 문의해 주세요!