Blog • Developer

Adding email to automated invoicing with Moltin

Last updated: 24 Jun 2019
In our last blog post, we showed you how to automatically generate a PDF invoice on order creation. Today, let’s learn how to automatically email that invoice to the customer! If you didn’t read the previous post, you’ll need to do so in order to complete this tutorial.

If you’d like to go directly to the code, have a look at the invoicing repo.

Right, let’s jump straight in.

We need to add one more dependency to our project, so in Terminal, cd to the root directory of the project, and run;

  npm i -s nodemailer

Great! Now we’re going to be working with the invoice.js file that we created before, so hop back into that. If you need a reference, the file can be found in the repo.

Add our new dependency to the top, but below the other ones:

  const nodemailer = require('nodemailer');

Now, below our generateInvoice function, add the following code:

  let transporter = nodemailer.createTransport({
service: config.service,
auth: {
user: config.user,
pass: config.pass
let mailOptions = {
from: '"Your Store" <>', // sender address
to: '', // list of receivers
subject: 'Your Invoice', // Subject line
text: 'Please find your invoice attached', // plain text body
html: '<b>Your Invoice</b>', // html body
attachments: [{   // file on disk as an attachment
filename: 'invoice.pdf',
path: './invoice.pdf' // stream this file
var sendMail = () => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
console.log('Message %s sent: %s', info.messageId, info.response);

Look for where we’ve triggered the generateInvoice function (within second .then of Moltin.Orders.Items). Replace the code within this .then with:

  var to = + '.pdf';
generateInvoice(invoice, 'invoice.pdf', function() {
console.log("Saved invoice to" + to);
mailOptions.attachments[0].filename = to;
mailOptions.attachments[0].path = './' + to;
return sendMail();

Lastly, where we have our callback, add this line: =;

We’re done! Now when our integration is triggered by an order being created, it will automatically generate the invoice with the order details, and email it to the email address specified at checkout.

Save Your Seat! Magento Migration Considerations

Discover best practices for migrating off of M1 from a Magento certified partner and decide if M2 is right for you.

From Our Blog

20 Jun 2019

Analytics for your Moltin store

Today we’re going to take a look at how to use best-in-class data warehousing and analytics solutions on top of Moltin, specifically in relation to orders and order items.

Read More
Matt Foyle
9 min. read in Developer
19 Jun 2019

Further Flows improvements

We've been working hard recently to improve the developer experience when using the Moltin API to build an online store.

Read More
James Owers
9 min. read in Announcements | Developer
29 May 2019

What to consider when migrating eCommerce platforms

Even though you are well aware of the pitfalls of your current eCommerce platform - be it rigidity, technical debt or the inability to innovate quickly; the thought of re-platforming can be a scary...

Read More
Joanna Suau
9 min. read in Magento Migration | Developer | eCommerce