How to use cURL to send HTML email with Gmail

Oh the joys of testing HTML emails with Microsoft Outlook for Windows and other dumb clients!

First, if you have Google 2-step verification enabled (surely you do?), you need to add a new app password - otherwise turn on access for less secure apps.

Then sending the email is as simple as:

curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user '[email protected]:password' \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --upload-file mail.txt

Where email.txt can be something as basic as:

From: "User Name" <[email protected]>
To: "John Smith" <[email protected]>
Subject: This is a test

Hi John,
I’m sending this mail with curl thru my gmail account.
Bye!

or fully-fledged multipart text+html email:

MIME-Version: 1.0
Subject: This is a test
From: Maciej Zgadzaj <[email protected]>
To: Maciej Zgadzaj <[email protected]>
Content-Type: multipart/alternative; boundary="00000000000067dfe705d9d0ff7a"

--00000000000067dfe705d9d0ff7a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi John,
I'm sending this mail with curl thru my gmail account.
Bye!

--00000000000067dfe705d9d0ff7a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
    <p>Hi John,</p>
    <p>I'm sending this mail with curl thru my gmail account.</p>
    <p>Bye!</p>
  </body>
</html>

--00000000000067dfe705d9d0ff7a--

You can build your own, or just copy-paste the source of any received email (in Gmail it is under More > Show original), just stripping out any unnecessary headers.

Based on this StackOverflow (obviously!) response.