typescript - 在 TypeScript 中为 Firebase Cloud Function 配置 mailgun

标签 typescript google-cloud-functions mailgun

我正在 Firebase 中执行云功能,以在 documentation 之后使用 mailgun 发送电子邮件.

我正在使用 TypeScript,但找不到有关如何设置 API key 、域以及如何在最后发送电子邮件的示例。我找到的所有示例都是用 JavaScript 编写的。

JavaScript 示例:

var API_KEY = 'YOUR_API_KEY';
var DOMAIN = 'YOUR_DOMAIN_NAME';
var mailgun = require('mailgun-js')({apiKey: API_KEY, domain: DOMAIN});

const data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, bar@example.com',
  subject: 'Hello',
  text: 'Testing some Mailgun awesomeness!'
};

mailgun.messages().send(data, (error, body) => {
  console.log(body);
});

typescript :

const API_KEY = 'YOUR_API_KEY';
const DOMAIN = 'YOUR_DOMAIN_NAME';

import * as mailgun from 'mailgun-js';
// How to set up ?

// How to send the email ?

我试过使用 ts-mailgun ,一个用于发送电子邮件的包装器,但由于部署该函数时出现错误而无法正常工作。

我的目标是使用 TypeScript 正确配置 mailgun 以向用户发送电子邮件。

最佳答案

npm i ts-mailgun

然后:


import { NodeMailgun } from 'ts-mailgun';

const mailer = new NodeMailgun();
mailer.apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Set your API key
mailer.domain = 'mail.my-sample-app.com'; // Set the domain you registered earlier
mailer.fromEmail = 'noreply@my-sample-app.com'; // Set your from email
mailer.fromTitle = 'My Sample App'; // Set the name you would like to send from

mailer.init();

// Send an email to test@example.com
mailer
    .send('test@example.com', 'Hello!', '<h1>hsdf</h1>')
    .then((result) => console.log('Done', result))
    .catch((error) => console.error('Error: ', error));

来自 ts-mailgun documentation

关于typescript - 在 TypeScript 中为 Firebase Cloud Function 配置 mailgun,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56992353/

相关文章:

javascript - 我怎样才能防止更漂亮的行为

firebase - 找不到模块 '.../lib/providers/https

php - Mailgun 内联图像,它是如何工作的?

javascript - Angular 2 : Rendering the same component with different ids

node.js - Typescript 没有重载与此调用匹配

javascript - 如何在 React TypeScript 中输出过滤后的待办事项列表

python-3.x - OpenCV 无法将 VideoCapture 与 url 一起使用

javascript - 如何在云函数中使用 indexOn 从实时数据库中检索数据

javascript - 使用 Mailgun 从 Base64 字符串发送 pdf 附件

c# - 为什么邮件主题在 MailGun 的 HTTP 帖子中重复?