Magic link email template

Sign people in without a password. Free to copy, with the best practices that keep the link safe.

What your users receive

Sign in to AcmeThe link expires in 15 minutes.

Copy the code

The email itself. Paste it where your app builds the message, then replace Acme with your product name, {{email}} with the address and {{link}} with the sign-in link your app generates.

Subject: Sign in to Acme
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="color-scheme" content="light">
  <meta name="supported-color-schemes" content="light">
  <title>Sign in to Acme</title>
</head>
<body style="margin:0;padding:0;background-color:#f4f4f5;">
  <div style="display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;">The link expires in 15 minutes.&#8199;&#65279;&#847;&#8199;&#65279;&#847;&#8199;&#65279;&#847;&#8199;&#65279;&#847;</div>
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f4f4f5;">
    <tr>
      <td align="center" style="padding:32px 16px;">
        <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:480px;background-color:#ffffff;border:1px solid #e4e4e7;border-radius:8px;">
          <tr>
            <td style="padding:32px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;color:#18181b;">
              <h1 style="margin:0;font-size:22px;line-height:28px;font-weight:700;">Sign in to Acme</h1>
              <p style="margin:12px 0 0;font-size:15px;line-height:22px;">Click the button to sign in as {{email}}. No password needed.</p>
              <table role="presentation" cellpadding="0" cellspacing="0" border="0">
                <tr>
                  <td style="padding-top:24px;">
                    <table role="presentation" cellpadding="0" cellspacing="0" border="0">
                      <tr>
                        <td bgcolor="#18181b" style="border-radius:6px;">
                          <a href="{{link}}" target="_blank" style="display:inline-block;padding:12px 24px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:15px;font-weight:600;color:#ffffff;text-decoration:none;border-radius:6px;">Sign in to Acme</a>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>
              <p style="margin:24px 0 0;font-size:13px;line-height:20px;color:#52525b;">The link expires in 15 minutes and works once.</p>
              <p style="margin:12px 0 0;font-size:13px;line-height:20px;color:#52525b;">Button not working? Copy this link into your browser:<br><a href="{{link}}" target="_blank" style="color:#52525b;word-break:break-all;">{{link}}</a></p>
              <p style="margin:12px 0 0;font-size:13px;line-height:20px;color:#52525b;">If you didn't try to sign in, ignore this email. Nobody can sign in without this link.</p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>

What to put in the email

A sign-in email is read in a hurry, often on a phone, by someone waiting on a login screen. Everything in it should get them in within seconds.

  1. Put the button first

    Place the sign-in button right under the heading, after one sentence at most. People open this email only to click. On a phone, a button that comes after a welcome text falls off the screen, and the email looks like it has no link.

    Avoid

    Sign in to Acme

    Welcome back to Acme! It is good to see you again. Since your last visit, we added dark mode, faster exports and a new dashboard for your team.

    You asked to sign in to your account with this email address. To keep your account safe, we send a new link each time you sign in. Click the button below to continue.

    Need help? Our support team answers every day from 9am to 6pm, and our help center has answers to the most common questions.

    Sign in to Acme

    The link expires in 15 minutes and works once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

    end of the screen
    Do

    Sign in to Acme

    Click the button to sign in as ada@example.com. No password needed.

    Sign in to Acme

    The link expires in 15 minutes and works once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

    end of the screen
  2. Add the full link as plain text

    Under the button, print the whole link as text that can be copied. Some email apps and company mail systems show text only, strip buttons, or display the email before its styles load. The text link is the fallback that always works.

    Sign in to Acme

    Click the button to sign in as ada@example.com. No password needed.

    Sign in to Acme

    The link expires in 15 minutes and works once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

  3. Keep secrets out of the subject and preview text

    The subject and the preview text (the hidden first line that inboxes show after the subject) appear on lock screens and in notifications, readable by anyone near the phone. Write "Sign in to Acme" and when the link expires, never the code or the link.

    Avoid
    10:04
    Acme
    Your sign-in code is 739214
    Do
    10:04
    Acme
    Sign in to AcmeThe link expires in 15 minutes.
  4. Say how long the link lasts

    Write when the link expires and that it works once. Someone who comes back to the email an hour later understands why it fails and asks for a new link, instead of writing to your support.

    Sign in to Acme

    Click the button to sign in as ada@example.com. No password needed.

    Sign in to Acme

    The link expires in 15 minutes and works once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

  5. Tell people what to do if they didn't ask

    Anyone can type someone else's address into a sign-in form. One sentence saying the email can be ignored reassures the person, and keeps them from reporting your email as phishing.

    Sign in to Acme

    Click the button to sign in as ada@example.com. No password needed.

    Sign in to Acme

    The link expires in 15 minutes and works once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

  6. Send a plain-text version too

    Send the HTML and a plain-text version in the same email. Text-only email apps and some screen readers use it, and spam filters are more suspicious of emails that only have HTML.

    Plain-text version
    Sign in to Acme
    
    Open this link to sign in as ada@example.com:
    
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI
    
    The link expires in 15 minutes and works once.
    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.
  7. Leave out images, tracking and promotions

    Images can be blocked, an open-tracking pixel tells you nothing useful here, and a promotional banner makes a security email look like marketing, which can send it to the Promotions tab or to spam. Text and one button load instantly everywhere.

Generating and checking the link

The link in the email is a key to the account: whoever has it signs in, no password asked. Your app should create and check it with the same care as a password.

  1. Make it expire quickly

    An email stays in the inbox for years, and an inbox is not a safe place: emails get forwarded, computers get shared, a mailbox can be hacked long after. A link that works forever opens the account to whoever finds it later. A short lifetime limits the risk to the few minutes when the person is actually signing in.

    10 to 15 minutes is the usual choice. It leaves time for an email held up a few minutes by a slow server or a spam filter, and makes an old email useless. Go beyond 30 minutes only for a good reason.

    15 minutes: the link worksyears in the inbox
    1. 10:00Ada asks for a link
    2. 10:03The email arrives, held up by a spam filter
    3. 10:04Ada clicks and is signed in
    4. 10:15The link expires
    5. 2028Someone opens the old email on a shared computer: the link no longer works
  2. Accept it only once

    Cancel the link as soon as it is used, and cancel earlier links when a new one is requested. A used link may still sit in the browser history or in a proxy log, where someone else could pick it up. And when someone asks for three links in a row, only the last one should work.

    AcmeCancelled
    Sign in to Acme
    AcmeCancelled
    Sign in to Acme
    AcmeWorks once
    Sign in to Acme
  3. Generate tokens nobody can guess

    Create the token with a cryptographic random generator: crypto.randomBytes(32) in Node.js, secrets.token_urlsafe(32) in Python, SecureRandom in Java. That gives 256 bits; OWASP's minimum for a session identifier is 64.

    Never build the token from something predictable: an incrementing number, a timestamp, a user id or Math.random(). If tokens follow each other, anyone who receives one can work out the next ones and sign in as other users.

    Avoid
    Numbers that follow each other
    /login?token=1041
    /login?token=1042
    /login?token=1043
    Do
    32 random bytes
    /login?token=
      ddCp76moRnu_nxz
      ef6I4o2ueM7TTYm
      PNsB3F-n5-uyI
  4. Store a hash of the token, not the token

    Keep only a hash of the token in your database, such as its SHA-256, and compare hashes when the link is opened. If the database leaks, through a backup or an injection, the attacker finds hashes that can't be turned back into working links.

    Passwords need a slow hash like bcrypt because people choose guessable ones. A random 256-bit token can't be guessed by trying, so a fast hash is enough.

    Avoid
    login_tokens
    user  token
    42    ddCp76moRnu_…
    Do
    login_tokens
    user  token_hash
    42    867d0ef77fbc…
  5. Limit attempts on the code

    If the email also carries a 6-digit code, remember that a code has only a million possible values, which a script can try one after the other. Accept a few wrong attempts, five for example, then cancel the code and ask for a new email.

    app.example.com/login
    Enter the code from the email123456Wrong code. 2 attempts left.

Making sure people get in

Most "I can't sign in" messages have nothing to do with the email's design. They come from what happens around it.

  1. Tell people where to look

    After the form is sent, the screen is where people wait. Show the address the email went to, so a typo is spotted at once, and the sender to look for. Mention the spam folder, and offer to send the email again after a short wait.

    app.example.com/login
    Check your inboxWe sent a sign-in link to ada@example.com. It comes from Acme <login@example.com>.Not there after a minute? Look in your spam folder.Resend in 45 sUse another address
  2. Expect security scanners to open the link first

    Many company email systems open every link in incoming mail to check it before the person sees it. If your link signs the person in on the first visit, the scanner uses it up and your user sees "link expired".

    Make the link open a page of your app with a "Sign in" button, and sign in only when the button is pressed. A scanner loads the page but doesn't submit the form.

    Signs in on the first visit
    1. A company scanner opens the link to check it
    2. Your app signs the scanner in and cancels the link
    3. Ada clicks the link
    4. This link has expired
    Signs in on a button press
    1. A company scanner opens the link to check it
    2. It finds a page with a button, and leaves
    3. Ada opens the link and presses Sign in
    4. Ada is signed in
    app.example.com/login?token=ddCp76moRnu_…
    Sign in to Acme as ada@example.com?Sign in
  3. Plan for a different device

    People often ask for the link on their computer and open the email on their phone. The link then signs in the phone, while the computer still shows the login screen. A 6-digit code in the same email lets them finish on the computer: turn on "Add a code for other devices" above the template to add it.

    The email, on the phone

    Sign in to Acme

    Click the button to sign in as ada@example.com. No password needed.

    Sign in to Acme
    Signing in on another device? Enter this code instead:739214

    The link and the code expire in 15 minutes and work once.

    Button not working? Copy this link into your browser:
    https://app.example.com/login?token=ddCp76moRnu_nxzef6I4o2ueM7TTYmPNsB3F-n5-uyI

    If you didn't try to sign in, ignore this email. Nobody can sign in without this link.

    The login screen, on the computer
    app.example.com/login
    Check your emailClick the link in the email, or enter its code here:739214
  4. Turn off click tracking

    Email services that track clicks replace each link with a redirect through their own servers. The sign-in token then passes through their logs, and some company filters block the redirect. Nobody needs click statistics on a sign-in email.

    Click tracking on
    The link in the email
    https://click.mailer.example.net/
      ls/click?upn=u001.Kx8fQ2…
    → logged, then sent to your link
    Click tracking off
    The link in the email
    https://app.example.com/login
      ?token=ddCp76moRnu_…
    → straight to your app
  5. Bring people back where they were

    Someone who opens a page of your app while signed out should land on that page after signing in, not on the home page. Carry it in the link, such as ?next=/settings/billing.

    Accept only paths of your own app. A next pointing to another site would let an attacker request a genuine sign-in email for someone, from your domain, that ends on the attacker's page.

    Refuse
    /login?next=https://evil.example
    → another site, after sign-in
    Accept
    /login?next=/settings/billing
    → back where Ada was

Protecting the sign-in form

The form that sends the email is public: anyone, or any script, can type any address into it.

  1. Limit how often a link can be requested

    Limit requests per email address and per IP address, for example one email a minute and five an hour for an address. Without a limit, a script can flood someone's inbox with hundreds of sign-in emails from your domain. You pay for every one, and the recipient's spam reports damage your domain's reputation, until your real users' emails land in spam too.

    Add a CAPTCHA to the form if it is attacked anyway.

    Bob's inbox, without a limit
    Acme10:00:04
    Sign in to Acme
    Acme10:00:03
    Sign in to Acme
    Acme10:00:02
    Sign in to Acme
    Acme10:00:01
    Sign in to Acme
    + 296 more
  2. Don't reveal who has an account

    Show the same message whether the address has an account or not. Otherwise anyone can use the form to check who is signed up to your product.

    Avoid
    app.example.com/login
    No account found for bob@example.com.
    Do
    app.example.com/login
    If an account exists for bob@example.com, we've sent it a sign-in link.

Getting it delivered

A sign-in email that arrives late or in spam is a user who can't get in, and often one who leaves.

  1. Send it from your own domain, under your product's name

    Send from an address on your domain, with your product as the sender name, so people recognize the email in a full inbox. Set up SPF, DKIM and DMARC: three DNS records that prove to Gmail, Outlook and Yahoo that the email really comes from you. Without them, inboxes treat it as suspect.

    Avoid
    noreply@send-7f3.example.net
    Sign in to AcmeThe link expires in 15 minutes.
    Do
    Acme <login@example.com>
    Sign in to AcmeThe link expires in 15 minutes.
  2. Keep it apart from your newsletters

    Send sign-in emails separately from your marketing campaigns, through a service made for transactional email, from your main domain while newsletters use a subdomain. If a newsletter gets spam complaints, the reputation it damages is not the one your sign-in emails depend on.

    Newslettersnews@news.example.com · its own reputationSign-in emailslogin@example.com · its own reputation

Send it with Kula

Kula sends sign-in emails and other transactional emails from your own domain, through an API or SMTP, separately from your newsletters and without click tracking. Free up to 3,000 emails a month.

Start free