【问题标题】:Sending email with firebase, sendgrid, angular使用 firebase、sendgrid、angular 发送电子邮件
【发布时间】:2018-08-24 09:09:07
【问题描述】:

我尝试使用我的 Angular 应用程序使用 firebase/sendgrid 配置电子邮件功能。按下按钮时调用函数

**--INDEX.JS**  
const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

const SENDGRID_API_KEY = functions.config().sendgrid.key

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);


exports.httpEmail = functions.https.onRequest((req, res) =>
{

  cors(req, res, () =>
  {

    const toName = req.body.toName;
    const toEmail = req.body.toEmail;

    const msg = {
      to: toEmail,
      from: 'hello@angularfirebase.com',
      subject: 'New Follower',
      // text: `Hey ${toName}. You have a new follower!!! `,
      // html: `<strong>Hey ${toName}. You have a new follower!!!</strong>`,

      // custom templates
      templateId: '300e1045-5b30-4f15-8c43-41754b73fe4f',
      substitutionWrappers: ['{{', '}}'],
      substitutions: {
        name: toName
        // and other custom properties here
      }
    };

    return sgMail.send(msg)

      .then(() => res.status(200).send('email sent!'))
      .catch(err => res.status(400).send(err))

  });

});


**--SendEmailComponent**
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions } from 'http';

@Component({
  selector: 'app-send-email',
  templateUrl: './send-email.component.html',
  styleUrls: ['./send-email.component.css']
})
export class SendEmailComponent implements OnInit
{
  //fields
  endpoint = 'https://**blankblank**.cloudfunctions.net/httpEmail';
  httpOptions =
    {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      })
    }

  //ctr
  constructor(private http: HttpClient) { }

  //lifehook
  ngOnInit()
  {
  }

  //methods
  sendEmail()
  {
    const data =
      {
        toEmail: 'someone@something.net',
        toName: 'Some One'
      }

    //post
    this.http.post(this.endpoint, data, this.httpOptions).subscribe(data => console.log('email sent: ', data));
  }

}

我尝试了不同的tutorials,但我总是收到此错误:

加载失败 https://blankblank.cloudfunctions.net/http电子邮件: 对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源

【问题讨论】:

    标签: firebase cors http-headers google-cloud-functions sendgrid


    【解决方案1】:

    您需要为 SendGrid 的 text 和 html 属性包含虚拟值。这也是我能够开始工作的 Cors 方法的外观

    index.js:

    const cors = require('cors')({ origin: true });
    const sendGrid = require('@sendgrid/mail');
    admin.initializeApp(functions.config().firebase);
    const db = admin.firestore();
    sendGrid.setApiKey(functions.config().sendgrid.apikey);
    sendGrid.setSubstitutionWrappers('{{', '}}');
    
    exports.sendBookingEmail = functions.https.onRequest((req, res) => {
    
        cors(req,res, () => {
    
    const message = {
      to: req.body.email,
      bcc: ['test@email.com'],
      from: 'test@email.com',
      text: 'email',
      html: '<strong>email</strong>',      
      substitutions: {
        contactName: 'test name',
        siteName: 'test site'
      }
    } 
    
            sendGrid.send(message);
            res.status(200).send('email sent new');
        }); 
    
    });
    

    【讨论】:

    • 嗨,本。感谢您的评论和建议。不幸的是,我没有受过足够的教育,无法将此 Angular 1 代码转换为 Angular 2。我必须进一步研究才能让邮件功能运行。
    • 嗨@Emiel 我已经更新了更具体的解决方案 - 角度 1 代码仅供参考,我现在已将其删除。您遇到错误的原因可能是 1. sgMail.send 方法中没有设置文本或 html 属性或 2. 您在函数中设置 cors 的方式 - 因为您收到与 CORS 相关的错误消息
    猜你喜欢
    • 2018-11-08
    • 2018-11-08
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多