Send email in Meteor.JS is as simple as ABC Meteor JS 08.06.2016

meteor_mail.png

Send text email

The email package allows sending email from a Meteor app. To use it, add the package to your project by running in terminal

meteor add email check

Before we can actually send email in our application, we need to provide Meteor with access to an email provider. The server reads from the MAIL_URL environment variable to determine how to send mail and it variable should be of the form smtp://USERNAME:PASSWORD@HOST:PORT/.

There is Email.send method that only works on the server. Here is an example of how a client could use a server method call to send an email.

Let's define a method in server code that the client can call

import { Email } from 'meteor/email'

Meteor.methods({
    sendEmail: function (to, from, subject, text) {
        check([to, from, subject, text], [String]);

        // Let other method calls from the same client start running,
        // without waiting for the email sending to complete.
        this.unblock();

        Email.send({
            to: to,
            from: from,
            subject: subject,
            text: text
        });
    }
});

In client code call sendEmail method that asynchronously send an email

Meteor.call('sendEmail',
    'to@server.com',
    'from@server.com',
    'Test',
    'Hello world!'
);

Send html template

You can render template with data on client by Blaze-toHTMLWithData and send to server. Also you can render render spacebars to html by meteor-spacebars-tohtml.

Also you can render template on server by meteorhacks:ssr.

Installation

meteor add meteorhacks:ssr

Create html template in private directory

# vim /project/private/mail.html

Hello <b>{{username}}</b>, <br>
You have subscribed to news!

In server code

SSR.compileTemplate('htmlEmail', Assets.getText('mail.html'));

let emailData = {
    username: "John Smith"
};

Email.send({
    to: "to@server.com",
    from: "from@server.com",
    subject: "Notification",
    html: SSR.render('htmlEmail', emailData)
});

Add attachment

You can add attachments property to Email.send with different options. Full description of options here.

Example (on sever)

Email.send({
    to: "to@server.com",
    from: "from@server.com",
    subject: "Notification",
    html: SSR.render('htmlEmail', emailData),
    attachments: [{filePath: '/home/www/temp/logo.png'}]
});

Test locally by MailDev

We can use MailDev to test mail sanding locally. MailDev is a simple way to test your projects' emails during development with an easy to use web interface that runs on your machine.

# install 
sudo npm install -g maildev

# run
maildev

You can see web interface at http://0.0.0.0:1080.

Settings for Meteor.JS

# shell variable
export MAIL_URL='smtp://0.0.0.0:1025'

# in server code
Meteor.startup( function() {
    process.env.MAIL_URL = 'smtp://0.0.0.0:1025';
});

On server we can test mail send from meteor shell

# cd /project
# meteor shell

process.env.MAIL_URL = 'smtp://USERNAME:PASSWORD@HOST:PORT'
Email.send({to: "to@server.com", from: "noreply@server.com", subject: "Test",  text: 'Test'});