Ulzurrun de Asanza i Sàez

Tag: email

Sending emails with NodeJS

Recently I’ve started a small project powered by NodeJS and it needs a way to send transactional emails (emails that are sent because an event is triggered). In my case the event is “clicking «I forgot my password» link”. Moreover, I want my project be easily deployed on a PaaS like Heroku so I prefer not relying on utilities like sendmail, or at least making it easy to move from sendmail to a third-party service like Postmark or Sendgrid.

With this two things in mind I looked for the best way to send mails in NodeJS and I  found something that maybe is not the best but it’s really easy to set up and use: an open source module named Nodemailer.

Using it is as easy as installing it via npm, creating a transport and calling a method.

Installing

Just run:

[code=bash]
npm install nodemailer
[/code]

I strongly recommend adding it to the dependencies in your package.js, but it is not required.

Creating a transport and sending an email

A transport is just a method to send emails. You can create a transport to send emails via Postmark (for those transactional emails, for instance) and a different transport to send emails via Sendgrid (to send newsletters). The transport is in charge to actually send the email.

If we want to send an email via sendmail we just create the appropriate transport:

[code=js]
// Require nodemailer.
var nodemailer = require( ‘nodemailer’ );

// Message we’ll send.
var sendmail_email = {
from: “SENDER-EMAIL-ADDRESS”,
to: “RECEIVER-EMAIL_ADDRESS”,
subject: “Hello world!”,
text: “Sendmail is working.”
};

// Sendmail transport.
var transport_sendmail = nodemailer.createTransport( “sendmail” );

// Send email.
transport_sendmail.sendMail( sendmail_email, function( error, message ) {
if ( error ) console.log( error );
else console.log( ‘Message sent’ );
} );
[/code]

If you try this your email will probably get marked as spam. That’s one of the reasons to rely on a third-party service. Let’s create a transport to send the emails with Postmark.

Moving from sendmail to Postmark

  1. Sign up for a Postmark account. It’s pretty easy but to sign the emails you’ll need a non-public email address (and to verify it you’ll also need access to its DNS records). I’ve used an email address @sumolari.com to test this. Setting up Postmark is really easy – it’s just following the steps that will be shown after signing up.
  2. Note the API key provided to you by Postmark. This API key will be used as username and password when authenticating to Postmark.
  3. Create a transport:

[code=js]
// Require nodemailer.
var nodemailer = require( ‘nodemailer’ );

// Message we’ll send.
var postmark_email = {
from: “SENDER-EMAIL-ADDRESS”,
to: “RECEIVER-EMAIL_ADDRESS”,
subject: “Hello world!”,
text: “Postmark is working.”
};

// Postmark transport.
var transport_postmark = nodemailer.createTransport( “postmark”, {
auth: {
user: “YOUR-API-KEY-HERE”,
pass: “YOUR-API-KEY-HERE”
}
});

// Send email.
transport_postmark.sendMail( postmark_email, function( error, message ) {
if ( error ) console.log( error );
else console.log( ‘Message sent’ );
} );
[/code]

As you can see just by changing the transport we can move from service to service which is really convenient (no one wants to be forced to use a service just because it was the one used at the beginning of project’s life cycle).


Disponible Thunderbird 3.0

ThunderbirdVía Genbeta descubro que ya se puede descargar Thunderbird 3.0, la versión final del cliente de correo electrónico grauito de Mozilla. Esta nueva versión viene cargada de novedades, entre las cuales destacan el nuevo asistente para la creación de cuentas (ahora tiene una Base de Datos con los datos de algunos servidores de proveedores de email, así se agiliza la creación de cuentas), un rediseño de la barra de herramientas, pestañas para ver los mensajes, mejor integración con Gmail…

Podéis descargarlo desde aquí.

Descarga ya Thunderbird 3 Beta 3

Thunderbird
Thunderbird

Vía Genbeta descubro que ya está disponible la tercera Beta de Thunderbird, el conocido cliente de correo electrónico desarrollado por Mozilla.

En esta nueva versión podremos encontrar mejoras en la integración con Gmail y una interfaz mejorada, que ahora permite, entre otras cosas, abrir los mensajes en pestañas (como lo haríamos con un navegador), una nueva vista de resumen (que nos permite seleccionar diversos mensajes  y saber quién lo envía, el título, el tamaño y un par de líneas de los emails).

Se puede descargar la nueva Beta desde aquí.

Gmail ya permite enviar adjuntos de hasta 25 MB

La verdad es que esta “noticia” la he descubierto de forma curiosa. Ayer un compañero me pidió que le enviase unos apuntes que he recopilado durante el año, y al tratar de enviarlos con mi cuenta de Gmail, éste no dejaba de advertirme de que los archivos superaban los 25 MB.

Y hoy, revisando los blogs a los que estoy suscrito descubro vía Genbeta que este límite de 25 MB es nuevo: antes el límite era de 20 MB.

Por un lado, excelente que aumenten el límite, pero por otro, ya tengo problemas con él, preferiría que lo aumentasen a 50  a 100 MB. ¿Y vosotros? ¿Tenéis problemas con los límites de los archivos adjuntos?