Panduan untuk Menggunakan SparkPost dengan Node.js

Panduan untuk Menggunakan SparkPost dengan Node.js

Panduan untuk Menggunakan SparkPost dengan Node.js

Sep 1, 2017

Diterbitkan oleh

Diterbitkan oleh

Bird

Bird

Kategori:

Kategori:

Email

Email

Ready to see Bird
in action?

Ready to see Bird
in action?

A Guide to Using SparkPost with Node.js

Pengantar ke 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 (bahkan sebelum aku dipekerjakan).

Izinkan saya membantu Anda untuk mulai mengirim email dengan SparkPost pada proyek Node.js Anda.


Installing & Setup

I’m going to assume that you have Node.js terinstal. Because we follow the Jadwal Dukungan Jangka Panjang (LTS) Node.js, 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.

Mari kita buat sebuah proyek npm baru. Jika Anda sudah memilikinya, Anda dapat melewati bagian ini.

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

Ini akan membuat sebuah proyek baru dan menerima semua default. Anda juga dapat menjalankan `npm init` dan menjawab semua pertanyaan.

Now we can install node-sparkpost:

> npm install sparkpost --save

Setelah terinstal, Anda dapat mengimpor dan membuat instance dari kelas SparkPost:

const SparkPost = require('sparkpost') const client = new SparkPost('KUNCI API ANDA')

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.


Mengirim Email

Sekarang setelah Anda memiliki instance SparkPost, Anda siap untuk mengirim. Ada beberapa opsi yang tersedia untuk mengirim, tetapi mari kita mulai dengan contoh sederhana. Berikut ini cara Anda mengirim email ke satu penerima, dengan menentukan konten sebaris:

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 mendukung fungsi panggilan balik.

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, dokumen untuk sumber daya Transmisi, and the Dokumentasi API Transmisi for more info.


Bonus: Mengirim Email dengan 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 Transportasi SparkPost untuk Nodemailer. You’ll need to install the nodemailer  and nodemailer-sparkpost-transport packages in your project.

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

Sekarang Anda dapat membuat instance transport nodemailer:

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

 

Perhatikan bagaimana saya membaca API Key dari variabel lingkungan. Praktik terbaiknya adalah jangan pernah meletakkannya secara langsung di dalam kode Anda.

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

Berikut adalah cara Anda mengirim pesan yang sama di atas menggunakan 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); } })


Bonus Ganda: Mengirim Email dengan 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

Anda sekarang dapat membuat instance notifme dengan penyedia SparkPost:

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

Sekali lagi, kita menarik API Key dari sebuah variabel lingkungan. Kami telah mengatakannya tiga kali - ini sangat penting 🙂.

Sekarang mari kita ulangi contoh yang sama, kali ini menggunakan 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 di fitur lainnya.

Tidak ada cara yang salah untuk Node

Ketika mengirim email menggunakan Node.js, Anda memiliki banyak pilihan. Kami telah bekerja keras untuk membuatnya semudah mungkin.

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

The right message -> ke right person -> at the right time.

By clicking "See Bird" you agree to Bird's Pemberitahuan Privasi.

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

The right message -> ke right person -> at the right time.

By clicking "See Bird" you agree to Bird's Pemberitahuan Privasi.