Sending email with Mailjet

Mailjet is a third-party global email service that offers Compute Engine usersa free tier with 6,000 emails each month. In addition, Mailjet also offers:

  • An API, user interface, and SMTP relay to send transactional and marketingemail.
  • European data privacy compliance.
  • Email and deliverability features, such as customizable priority settings andautomated throttle management.
  • API resource library in Go, PHP, Nodejs, Java, Python, and Ruby to managesub-accounts, authentication, contacts, campaigns, custom payloads,statistics, real time events, and parsing through theEvent API.
  • IntegratedMJML framework tocreate responsive HTML email templates.
  • 24/7 follow-the-sun support in 4+ languages.
  • Ability to send email messages from domains other thangmail.com.

This document describes how to configure your VM instance to send mail throughMailjet using the following methods:

For extensive documentation of other email solutions, including examples ofintegration with most common SMTP servers, libraries, and frameworks, seeMailjet's documentation.

Before you begin

  1. Create a new Mailjet account on Mailjet'sGoogle partnerpage*.When signing up, provide the domain and email address from which you want tosend email messages.

  2. Get your Mailjet SMTP account credentials.

  3. Make sure the email address you want to send emails from has beenvalidated.

  4. Set up a firewall ruleto allow TCP traffic on port 2525.

* Google is compensated for customers who sign up for a non-free account.

Configuring Mailjet as an SMTP relay

Configuring Mailjet as a mail relay lets you forward email messages to Mailjetfor remote delivery.

Using Postfix

  1. Connect to your VM using SSH, replacinginstance-name with the name of the instanceyou want to send email messages from:

    gcloud compute sshinstance-name
  2. On the VM, install the Postfix Mail Transport Agent. When prompted,accept the default choices for domain names but select theLocal Onlyconfiguration.

    Debian

    sudo apt update && sudo apt -y install postfix libsasl2-modules

    CentOS

    sudo yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y

  3. Modify the Postfix configuration options. Postfix configuration options areset in themain.cf file. Open the file with the text editor of yourchoice:

    sudo vi /etc/postfix/main.cf
  4. Update the file:

    1. Comment out the following lines:

      # default_transport = error# relay_transport = error
    2. Add the following lines to the end of the file:

      relayhost = in-v3.mailjet.com:2525smtp_tls_security_level = encryptsmtp_sasl_auth_enable = yessmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwdsmtp_sasl_security_options = noanonymous

      These lines enforce SSL/TLS support and configureSMTP authentication for these requests. A simple access and securitylayer (SASL) module handles authentication in the Postfix configuration.

      Note: You must use port 2525 because port 25 is not allowed onCompute Engine.
    3. Save your changes and close the file.

  5. Create the SASL password map with the text editor of yourchoice:

    sudo vi /etc/postfix/sasl_passwd
  6. To thesasl_passwd file, add the following lines to provide the credentialsfor Mailjet:

    in-v3.mailjet.com:2525 YOUR_API_KEY:YOUR_SECRET_KEY
  7. Use thepostmap utility to generate a.db file:

    sudo postmap /etc/postfix/sasl_passwdsudo ls -l /etc/postfix/sasl_passwd*

    You should receive the following response:

    -rw------- 1 root root    68 Jun  1 10:50 /etc/postfix/sasl_passwd-rw------- 1 root root 12288 Jun  1 10:51 /etc/postfix/sasl_passwd.db
  8. Next, remove the file that contains your credentials because it is no longerneeded:

    sudo rm /etc/postfix/sasl_passwd
  9. Set the permissions on your.db file:

    sudo chmod 600 /etc/postfix/sasl_passwd.dbsudo ls -la /etc/postfix/sasl_passwd.db
  10. Finally, reload your configuration to load the modified parameters:

  11. Test your configuration. Install themailx ormailutils package and testyour configuration.

    Send a test message:

    echo 'Test passed.' | mail -s 'Test-Email' -aFrom:from-emaildestination-email

    Replace the following:

    Look in your system's logs for a status line containingstatus and thesuccessful server response code(250):

    Debian

    sudotail-n5/var/log/syslog

    CentOS

    sudotail-n5/var/log/maillog

If you run into issues setting up Postfix with Mailjet, see theMailjet Postfix setup instructions.

Using Nodemailer

The following instructions describe how to use Mailjet with Node.js on DebianWheezy.

Debian
  1. Connect to your instance using SSH:
    gcloud compute sshinstance-name
  2. Update your package repositories:
    user@test-wheezy:~# sudo apt update
  3. Install Node.js dependencies:
    user@test-wheezy:~# sudo apt -y install git-core curl build-essential openssl libssl-dev
  4. Clone the Node.js repo from github:
    user@test-wheezy:~# git clone https://github.com/nodejs/node
  5. Change the directory to the Node.js source tree:
    user@test-wheezy:~# cd node
  6. Configure node software for this OS and virtual machine:
    user@test-wheezy:~# ./configure
  7. Build Node.js, npm, and related objects:
    user@test-wheezy:~# sudo make

    This can take a few minutes to complete.

  8. Install Node.js, npm, and other software in the default location:
    user@test-wheezy:~# sudo make install
  9. Install the mailer package:
    user@test-wheezy:~# npm install nodemailer nodemailer-smtp-transport
  10. In thenode directory, create a new file namedsendmail.js that contains the following #"suppresswarning" translate="no" track-metadata-position="GoogleCloudPlatform/nodejs-docs-samples/compute/mailjet.js/HEAD/compute_send" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js" track-metadata-snippet-file-url="https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js" feedback-context="{"language": "nodejs", "region_tag": "compute_send", "snippet_file_url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js"}" feedback-product="1634365" feedback-bucket="infraops" language="nodejs" data-github-path="GoogleCloudPlatform/nodejs-docs-samples/compute/mailjet.js" data-git-revision="HEAD" data-region-tag="compute_send" track-metadata-region-tag="compute_send" dir="ltr" is-upgraded syntax="JavaScript">constmailer=require('nodemailer');constsmtp=require('nodemailer-smtp-transport');asyncfunctionmailjet(){consttransport=mailer.createTransport(smtp({host:'in.mailjet.com',port:2525,auth:{user:process.env.MAILJET_API_KEY||'<your-mailjet-api-key',pass:process.env.MAILJET_API_SECRET||'<your-mailjet-api-secret>',},}));constjson=awaittransport.sendMail({from:'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM',// From addressto:'EMAIL@EXAMPLE.COM',// To addresssubject:'test email from Node.js on Google Cloud Platform',// Subjecttext:'Hello!\n\nThis a test email from Node.js.',// Content});console.log(json);}mailjet();
  • Run the program to send an email message through Mailjet:
    user@test-wheezy:~# node sendmail.js
  • CentOS
    1. Connect to your instance using SSH:
      gcloud compute sshinstance-name
    2. Update package repositories:
      user@test-centos:~# sudo yum update -y
    3. Install Node.js dependencies:
      user@test-centos:~# sudo yum install git-core curl openssl openssl-dev -y...user@test-centos:~# sudo yum groupinstall "Development Tools" -y...
    4. Clone Node.js repository from github:
      user@test-centos:~# git clone https://github.com/nodejs/node
    5. Change directory to the Node.js source tree:
      user@test-centos:~# cd node
    6. Configure node software for this OS and virtual machine:
      user@test-centos:~# ./configure
    7. Build Node.js, npm, and related objects:
      user@test-centos:~# sudo make

      This can take a few minutes to complete.

    8. Install Node.js, npm, and other software in the default location:
      user@test-centos:~# sudo make install
    9. Install the mailer package:
      user@test-centos:~# npm install npm install nodemailer nodemailer-smtp-transport
    10. In thenode directory, create a new file namedsendmail.js that contains the following #"suppresswarning" translate="no" track-metadata-position="GoogleCloudPlatform/nodejs-docs-samples/compute/mailjet.js/HEAD/compute_send" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js" track-metadata-snippet-file-url="https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js" feedback-context="{"language": "nodejs", "region_tag": "compute_send", "snippet_file_url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/HEAD/compute/mailjet.js"}" feedback-product="1634365" feedback-bucket="infraops" language="nodejs" data-github-path="GoogleCloudPlatform/nodejs-docs-samples/compute/mailjet.js" data-git-revision="HEAD" data-region-tag="compute_send" track-metadata-region-tag="compute_send" dir="ltr" is-upgraded syntax="JavaScript">constmailer=require('nodemailer');constsmtp=require('nodemailer-smtp-transport');asyncfunctionmailjet(){consttransport=mailer.createTransport(smtp({host:'in.mailjet.com',port:2525,auth:{user:process.env.MAILJET_API_KEY||'<your-mailjet-api-key',pass:process.env.MAILJET_API_SECRET||'<your-mailjet-api-secret>',},}));constjson=awaittransport.sendMail({from:'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM',// From addressto:'EMAIL@EXAMPLE.COM',// To addresssubject:'test email from Node.js on Google Cloud Platform',// Subjecttext:'Hello!\n\nThis a test email from Node.js.',// Content});console.log(json);}mailjet();
    11. Run the program to send an email message through Mailjet:
      user@test-centos:~# node sendmail.js