Site icon R-bloggers

{emayili} Support for Gmail, SendGrid & Mailgun

[This article was first published on R - datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The {emayili} package supports configuring a generic SMTP server via the server() function. In the most recent version, v0.6.5, we add three new functions, gmail(), sendgrid() and mailgun(), which provide specific support for Gmail, SendGrid and Mailgun.

library(emayili)

options(envelope.details = TRUE)
options(envelope.invisible = FALSE)

packageVersion("emayili")
[1] '0.6.5'

Gmail

Gmail is a popular email service that’s rather pervasive and does not require an introduction. The gmail() function makes it possible to easily use the Gmail SMTP server, requiring only username and password for authentication.

# Using server().
smtp <- server(
  host = "smtp.gmail.com",
  port = 587,
  username = "bob@gmail.com",
  password = "bd40ef6d4a9413de9c1318a65cbae5d7"
)
# Using gmail().
smtp <- gmail(
  username = "bob@gmail.com",
  password = "bd40ef6d4a9413de9c1318a65cbae5d7"
)

By default gmail() will use port 587 (TLS), but you can specify the port argument if for some reason you prefer to use port 465 (SSL).

SendGrid

SendGrid is a cloud-based service for sending and managing email at scale.

To send an email via SendGrid from {emayili} use the sendgrid() function. You’ll need to first get an API key, which will then be used as the SMTP password.

smtp <- sendgrid(
  password = "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"
)

Below is the lightly redacted raw content of a message sent by Bob () to Alice () via SendGrid using {emayili}.

Delivered-To: alice@gmail.com
Received: by 2002:a92:dc07:0:0:0:0:0 with SMTP id t7csp5413809iln;
        Thu, 14 Oct 2021 00:42:07 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJzUkPUh4K+kyOdnqDyLpZy61eYgtm91lhZddP6z3nlPOIX6Sprs0pXjtaP2UiGjSnojckAO
X-Received: by 2002:a25:e755:: with SMTP id e82mr4573229ybh.528.1634197327823;
        Thu, 14 Oct 2021 00:42:07 -0700 (PDT)
Received: from xtrwkvxq.outbound-mail.sendgrid.net (xtrwkvxq.outbound-mail.sendgrid.net. [167.89.24.164])
        by mx.google.com with ESMTPS id n62si2129988ybn.282.2021.10.14.00.42.07
        for <alice@gmail.com>
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Thu, 14 Oct 2021 00:42:07 -0700 (PDT)
Received: by filterdrecv-64fcb979b9-ttm9r with SMTP id filterdrecv-64fcb979b9-ttm9r-1-6167DF4E-7
        2021-10-14 07:42:06.149765894 +0000 UTC m=+2619921.806202168
Received: from propane (unknown)
    by geopod-ismtpd-6-1 (SG) with ESMTP
    id WQM06PcSTEGg9g6DXD15-Q
    for <alice@gmail.com>;
    Thu, 14 Oct 2021 07:42:06.009 +0000 (UTC)
Date: Thu, 14 Oct 2021 07:42:06 +0000 (UTC)
X-Mailer:                  {emayili}-0.6.4
MIME-Version:              1.0
From: bob@gmail.com
Subject: Mail via SendGrid
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Disposition:       inline
Content-Transfer-Encoding: 7bit
Content-MD5:               lS0sVtBIWVgzZ0e83ZhZDQ==
Message-ID: <WQM06PcSTEGg9g6DXD15-Q@geopod-ismtpd-6-1>
X-SG-EID: 
 =?us-ascii?Q?LCYZPn5Un+WXHvrSNyN5bUctJrIgrTLrkNMw3pKU=2FxUypxaYVR4293QfCj3U9f?=
 =?us-ascii?Q?KknTRh0vtKYz3cREfpwZJXAA0MdhlRN=2FTOlo5Rv?=
 =?us-ascii?Q?WMV2qu7qgo3Vx01e2DuJDPvmzuG1NNVsN=2FAdfmO?=
 =?us-ascii?Q?TXWQ1kY=2FoRp24Dekt2E8fZSr=2Fc0Bo6ci0KOdkpH?=
 =?us-ascii?Q?Zxno0nvvAGn4GhTvOs0E3kvZmCRWsQyQffw=3D=3D?=
To: alice@gmail.com
X-Entity-ID: yskshUftiOQOXDmOqHb1EA==

Hello!

The salient bits of information to extract from the email headers are:

  • the message is delivered by the SendGrid SMTP server, outbound-mail.sendgrid.net (see the Received field) and
  • the custom X-SG-EID field which identifies the message as being sent by SendGrid. The value of this field is encoded according to RFC 2047.

Mailgun

Mailgun is another cloud-based service for sending and managing emails. To use Mailgun with {emayili} you’ll first need to register a sending domain. This will then be assigned a username and password, which you’ll specify in the call to mailgun().

smtp <- mailgun(
  username = "postmaster@sandbox9ptce35fdf0b31338dec4284eb7aaa59.mailgun.org",
  password = "44d072e7g2b5f3bf23b2b642da0fe3a7-2ac825a1-a5be680a"
)

Here’s the simplified raw content of a message sent by Bob () to Alice () via Mailgun using {emayili}.

Delivered-To: alice@gmail.com
Received: by 2002:a92:dc07:0:0:0:0:0 with SMTP id t7csp5413647iln;
        Thu, 14 Oct 2021 00:41:51 -0700 (PDT)
X-Received: by 2002:ad4:4b63:: with SMTP id m3mr4009333qvx.28.1634197311440;
        Thu, 14 Oct 2021 00:41:51 -0700 (PDT)
Received: from m228-62.mailgun.net (m228-62.mailgun.net. [159.135.228.62])
        by mx.google.com with UTF8SMTPS id az43si1987716qkb.315.2021.10.14.00.41.51
        for <alice@gmail.com>
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Thu, 14 Oct 2021 00:41:51 -0700 (PDT)
X-Mailgun-Sending-Ip: 159.135.228.62
X-Mailgun-Sid: WyIyNDFkOCIsICJhbmRyZXdAZmF0aG9tZGF0YS5kZXYiLCAiNzY5ZTg3Il0=
Received: from propane (host-92-12-243-150.as13285.net [92.12.243.150]) by
 smtp-out-n03.prod.us-west-2.postgun.com with SMTP id
 6167df3ff3e5b80f1f369570 (version=TLS1.3, cipher=TLS_AES_128_GCM_SHA256);
 Thu, 14 Oct 2021 07:41:51 GMT
Sender: bob=gmail.com@mail.gmail.com
Message-Id: <20211014074151.535b7df0826d28e2@mail.gmail.com>
Date: Thu, 14 Oct 2021 07:41:50 GMT
X-Mailer: {emayili}-0.6.4
MIME-Version: 1.0
To: alice@gmail.com
From: bob@gmail.com
Subject: Mail via Mailgun
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Content-MD5: lS0sVtBIWVgzZ0e83ZhZDQ==

Hello!

The important pieces of information to notice in the email headers are:

  • the message is delivered by a Mailgun SMTP server (see the Received fields) and
  • the custom X-Mailgun-Sid field which identifies the message as being sent by Mailgun.

More Services

We’ll shortly be adding support for other services like Sendinblue and MailerSend.

To leave a comment for the author, please follow the link and comment on their blog: R - datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.