En guide till att använda SparkPost med Node.js

En guide till att använda SparkPost med Node.js

En guide till att använda SparkPost med Node.js

Sep 1, 2017

Publicerad av

Publicerad av

Bird

Bird

Kategori:

Kategori:

E-post

E-post

Ready to see Bird
in action?

Ready to see Bird
in action?

A Guide to Using SparkPost with Node.js

Introduktion till 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 (redan innan jag anställdes).

Låt mig hjälpa dig att komma igång med att skicka e-post med SparkPost i ditt Node.js-projekt.


Installing & Setup

I’m going to assume that you have Node.js installerat. Because we follow the Node.js schema för Long Term Stöd (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.

Låt oss skapa ett nytt npm-projekt. Om du redan har ett kan du hoppa över den här delen.

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

Detta kommer att skapa ett nytt projekt och acceptera alla standardinställningar. Du kan också istället köra `npm init` och svara på alla frågor.

Now we can install node-sparkpost:

> npm install sparkpost --save

Efter installationen kan du importera och skapa en instans av SparkPost-klassen:

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.


Skicka e-post

Nu när du har en SparkPost-instans är du redo att skicka. Det finns en hel del alternativ tillgängliga för att skicka, men låt oss börja med ett enkelt exempel. Så här skickar du ett e-postmeddelande till en enskild mottagare och anger inline-innehåll:

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 stödja callback-funktioner.

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, dokument för resursen Transmissions, and the API-dokumentation för transmissioner for more info.


Bonus: Skicka e-post med Nodemailer

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 SparkPost-transport för Nodemailer. You’ll need to install the nodemailer  and nodemailer-sparkpost-transport packages in your project.

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

Nu kan du skapa en nodemailer-transportinstans:

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

 

Lägg märke till hur jag läser API-nyckeln från en miljövariabel. Det är fortfarande en god praxis att aldrig ange detta direkt i koden.

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 vid README.md for all the options.

Så här skulle du skicka samma meddelande som ovan med 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); } })


Dubbel bonus: Skicka e-post med notif.me

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

Du kan nu skapa en notifme-instans med SparkPost-providern:

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

Återigen hämtar vi API-nyckeln från en miljövariabel. Vi har sagt det tre gånger - det är så viktigt....

Låt oss nu upprepa samma exempel, den här gången med 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 vid andra funktioner.

Det finns inget fel sätt att lära sig Node

När det gäller att skicka e-post med Node.js har du många alternativ. Vi har arbetat hårt för att göra det så smärtfritt som möjligt.

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

Den right message -> till right person -> at the right time.

By clicking "See Bird" you agree to Bird's Meddelande om integritet.

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

Den right message -> till right person -> at the right time.

By clicking "See Bird" you agree to Bird's Meddelande om integritet.