Developers

How to add a QR code to an email template

To add a QR code to an email, use an img tag with a PNG from https://qrcodesforever.com/qr.png and your content in the query string. Use PNG rather than SVG, set width and height as attributes, and write the link out in text too, because some email apps hide images until the reader allows them.

Tickets, boarding passes and receipts are the emails where a QR code earns its place: something will scan it later, at a door or a counter. A newsletter code that just opens a link is usually worse than the link.

The snippet

<table role="presentation" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td align="center" style="padding:16px; background:#ffffff;">
      <img src="https://qrcodesforever.com/qr.png?url=example.com%2Fticket%2F123"
           alt="QR code for your ticket" width="180" height="180"
           style="display:block; border:0;">
      <p style="margin:8px 0 0; font-size:14px;">
        Can't scan it? <a href="https://example.com/ticket/123">Open your ticket</a>
      </p>
    </td>
  </tr>
</table>

The image is a 512-pixel PNG of about 29 KB, and its code contains https://example.com/ticket/123. Shown at 180 pixels, it stays sharp on high-density phone screens. Each choice in the snippet is there for a reason:

  • PNG, not SVG. Email apps differ a lot in how they handle SVG. PNG shows up nearly everywhere.
  • width and height as attributes, because some email apps ignore CSS sizes on images.
  • display:block and border:0 remove the stray gaps and borders some apps add around images.
  • A white cell behind the code. Some apps invert colors in dark mode. A QR code needs dark modules on a light background to scan, so the cell gives it one. Leave transparent=1 out of email for the same reason.
  • A text link, for readers whose app holds back images and for anyone reading on the phone they'd scan with.

A code per recipient

Put a merge tag inside the image URL and each person gets their own code:

https://qrcodesforever.com/qr.png?url=example.com%2Fticket%2F{{ticket_id}}

Tag syntax depends on the platform; Mailchimp uses *|MERGE|* tags and many others use {{ }}. A ticket number made of digits needs no encoding. If the merged value can contain spaces, & or #, encode it in the tool or store an encoded copy, because an unencoded & would cut the code's link short.

Every recipient's code is a separate image URL, and each one is drawn on first request and then served from cache. If you send from your own code, build the URL with a real encoder; the Node.js and Python guides show how.

Other kinds of code in email

An event invite can carry a calendar code, so someone with a printed copy can add the event by scanning. A rental's arrival email can carry a Wi-Fi code. The embed reference has the fields for both. Brand colors work with fg and bg as long as the code stays dark on light; check a pair with the contrast checker before it goes to a list.

Questions people ask

Can people scan a QR code in an email on the phone they're reading it on?

Not with the camera, since it's on the same screen. Some phones can read a code from a screenshot, but most people won't think to try. That's why a clickable link belongs next to every code. Codes in email do their real work when the reader opens it on a computer, prints it, or shows it at a door.

Written by Last updated