2017-12-19 21 views
0

각도 및 SendGrid를 사용하여 전자 메일을 보내려고합니다. NPM 패키지를 올바르게 설치했지만 코드를 구현하는 데 문제가 있습니다. 나는 API 키를 생성 타이프 스크립트는각도 및 노드로 SendGrid 사용 시도

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env 
echo "sendgrid.env" >> .gitignore 
source ./sendgrid.env 

으로 디렉토리에 저장 :

sgemail(){ 
    const sgMail = require('@sendgrid/mail'); //ERROR: Cannot find name 'require'. 
    sgMail.setApiKey(process.env.SENDGRID_API_KEY); //ERROR: Cannot find name 'process'. 
    const msg = { 
    to: '[email protected]', 
    from: '[email protected]', 
    subject: 'Sending with SendGrid is Fun', 
    text: 'and easy to do anywhere, even with Node.js', 
    html: '<strong>and easy to do anywhere, even with Node.js</strong>', 
    }; 
    console.log(msg); 
    sgMail.send(msg); 
} 

내가 버튼 클릭에 발사하고있다.

Sendgrid는 import { Vibration } from '@ionic-native/vibration';을 사용하여 Ionic의 진동 패키지를 사용하는 것과 같은 패키지 가져 오기에 대한 정보가 없습니다.

답변

1

Send Mail APIfetch을 사용하여 수동으로 POST 요청을 보내보십시오. 그리고 Authorization Headers을 잊지 마세요. 아래는 시도 할 동일한 테스트되지 않은 JavaScript 코드 스 니펫입니다. YOUR_API_KEY을 작성하고 이메일을 귀하의 이메일 중 하나로 업데이트하십시오.

var payload = { 
    "personalizations": [ 
     { 
     "to": [ 
      { 
      "email": "[email protected]" 
      } 
     ], 
     "subject": "Hello, World!" 
     } 
    ], 
    "from": { 
     "email": "[email protected]" 
    }, 
    "content": [ 
     { 
     "type": "text/plain", 
     "value": "Hello, World!" 
     } 
    ] 
    }; 
    var myHeaders = new Headers({ 
    "Content-Type": "application/json", 
    "Authorization": "Bearer YOUR_API_KEY", 
    }); 
    var data = new FormData(); 
    data.append("json", JSON.stringify(payload)); 
    fetch("https://api.sendgrid.com/v3/mail/send", 
    { 
     method: "POST", 
     headers: myHeaders, 
     body: data 
    }) 
    .then(function(res){ return res.json(); }) 
    .then(function(data){ console.log(JSON.stringify(data)) })