使用Node.js的SparkPost指南

使用Node.js的SparkPost指南

使用Node.js的SparkPost指南

Sep 1, 2017

出版商

出版商

Bird

Bird

-

类别

类别

电子邮件

电子邮件

Ready to see Bird
in action?

Ready to see Bird
in action?

A Guide to Using SparkPost with Node.js

Node.js介绍

As a Developer Advocate for SparkPost, I write a lot of sample applications. My background is mostly front-end development, therefore my strongest language is JavaScript. Thanks to Node.js, I’m a decent backend developer as well. Does this mean I’m a full stack developer now? Anyway, it was important for me that we had an awesome SparkPost client library for Node.js. So, I dove right in and became a contributor (甚至在我被录用之前).

请允许我帮助您在您的Node.js项目上开始使用SparkPost发送电子邮件。


Installing & Setup

I’m going to assume that you have 安装了Node.js. Because we follow the Node.js长期支持(LTS)时间表, you’ll need to be running version 4 or higher. You can see which version you’re running using the `node –version` command in your terminal window.

让我们创建一个新的npm项目。如果你已经有一个,你可以跳过这一部分。

> mkdir sparkpost-test > cd sparkpost-test > npm init --yes

这将创建一个新的项目并接受所有的默认值。你也可以运行`npm init`并回答所有的提示。

Now we can install node-sparkpost:

> npm install sparkpost --save

一旦安装完毕,你就可以导入并创建一个SparkPost类的实例。

const SparkPost = require('sparkpost') const client = new SparkPost('YOUR API KEY')

It’s good practice to avoid putting your API key in code. We highly recommend storing it outside your code, so we set up the client library to detect the SPARKPOST_API_KEY  environment variable.


发送电子邮件

现在您有了一个SparkPost实例,您就可以进行发送了。有相当多的选项可用于发送,但让我们从一个简单的例子开始。下面是您如何向单个收件人发送电子邮件,指定内联内容。

client.transmissions.send({ content: { from: 'test@your-sending-domain.com', subject: 'Hello from node-sparkpost', html: '<p>Hello world</p>' }, recipients: [ {address: 'someone@somedomain.com'} ] }) .then(data => { console.log('Woohoo! You just sent your first mailing!') console.log(data) }) .catch(err => { console.log('Whoops! Something went wrong') console.log(err) })

Note: This example uses Promises, but don’t worry. We also 支持回调函数.

There are more options available with transmissions, including specifying stored templates or recipient lists, cc and bcc, adding attachments, specifying a campaign, using substitution data, and much more. Check out the examples, 传输资源的文档, and the 传输API文档 for more info.


奖金:用Nodemailer发送邮件

联网邮件系统 is a popular library for Node.js that make sending email “easy as cake.” For those of you choosing to use this library, we created a 用于Nodemailer的SparkPost传输. You’ll need to install the nodemailer  and nodemailer-sparkpost-transport packages in your project.

> npm install nodemailer nodemailer-sparkpost-transport --save

现在你可以创建一个nodemailer传输实例。

const nodemailer = require('nodemailer') const sparkPostTransport = require('nodemailer-sparkpost-transport') const transporter = nodemailer.createTransport(sparkPostTransport({ 'sparkPostApiKey': process.env.SPARKPOST_API_KEY }))

 

注意我是如何从环境变量中读取API密钥的。这仍然是一个最好的做法,不要把它直接放在你的代码中。

There are several options that can passed into the SparkPost transport, like campaign id and whether or not the message is transactional. Take a look 在 README.md for all the options.

下面是你如何使用Nodemailer发送上述同样的信息。

transporter.sendMail({ from: 'test@your-sending-domain.com', to: 'someone@somedomain.com', subject: 'Hello from nodemailer-sparkpost-transport', html: '<p>Hello world</p>' }, (err, info) => { if (err) { console.error(err); } else { console.log(info); } })


双重奖励:用通知我发送电子邮件

We all know that email is king of communication but sometimes you want to be able to reach people via multiple channels. notif.me is a Node.js library for sending all kinds of transactional messages. Whether you want to send an email, sms, push, or webpushes, you can do it with ease. It also has built in fall and round robin strategies for multiple providers. We recently worked with the creators to build a SparkPost provider. You’ll need to install the `notifme-sdk` package in your project.

> npm install notifme-sdk --save

现在你可以用SparkPost提供者创建一个notifme实例。

const NotifmeSdk = require('notifme-sdk').default const notifmeSdk = new NotifmeSdk({ channels: { email: { providers: [{ type: 'sparkpost', apiKey: process.env.SPARKPOST_API_KEY, }] } } })

同样,我们从环境变量中提取API密钥。我们已经说过三次了--它就是这么重要。 🙂

现在让我们重复这个相同的例子,这次使用notif.me。

notifmeSdk.send({ email: { from: 'test@your-sending-domain.com', to: 'someone@somedomain.com', subject: 'Hello from the SparkPost notif.me provider', html: '<p>Hello world</p>' } }).then(console.log)

It’s really easy to use and I recommend looking 在 其他特点。

构建节点的方式没有错

当谈到使用Node.js发送电子邮件时,你有很多选择。我们已经努力地帮助你尽可能地减少麻烦。

Your new standard in Marketing, Pay & Sales. It's Bird

ǞǞǞ right message ->right person -> at the right time.

By clicking "See Bird" you agree to Bird's 隐私声明.

Your new standard in Marketing, Pay & Sales. It's Bird

ǞǞǞ right message ->right person -> at the right time.

By clicking "See Bird" you agree to Bird's 隐私声明.