我正在 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));
关于typescript - 在 TypeScript 中为 Firebase Cloud Function 配置 mailgun,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56992353/