Email Settings

Overview

This documentation describes how to enable the email-related capabilities of Remark42.

  • email authentication for users:

    enabling this will let the user log in using their emails:

    Email authentication

  • email notifications for any users except anonymous:

    GitHub or Google or any other kind of user gets the ability to get email notifications about new replies to their comments (and any of the responses down the tree):

    Email notifications subscription

    The confirmation email carries a link that opens a page with a button confirming the subscription, which works without signing in, so the email can be opened on any device. Once confirmed, the page closes itself where the browser allows it, and the comment form shows the subscription without a reload. The token in the same email can still be pasted into the comment form instead. The link does not replace an address the user has already confirmed; to change it, unsubscribe first and open the link again.

Setup email server connection

To enable any email functionality, you need to set up an email (SMTP) server connection using these variables:

SMTP_HOST
SMTP_PORT
SMTP_HELO_HOST
SMTP_TLS
SMTP_INSECURE_SKIP_VERIFY
SMTP_STARTTLS
SMTP_USERNAME
SMTP_PASSWORD
SMTP_LOGIN_AUTH
SMTP_TIMEOUT

SMTP_LOGIN_AUTH=true makes Remark42 authenticate with the LOGIN mechanism instead of PLAIN, which some servers, such as Office 365 and Outlook.com, still require; it sends the credentials only over TLS or to localhost.

SMTP_HELO_HOST

SMTP_HELO_HOST sets the hostname Remark42 announces in the SMTP greeting. Left unset it stays localhost, which is what Remark42 has always sent, so existing setups need no change.

Set it when the mail server rejects the greeting. Postfix with reject_non_fqdn_helo_hostname or reject_unknown_helo_hostname, which is common on hardened relays, refuses localhost and the message never leaves, typically with a 504 in the log. Give it a fully qualified name that resolves to the sending host, for example:

SMTP_HELO_HOST=comments.example.com

It applies to both paths that send mail: notification emails and the verification emails used by email authentication.

Setup email notifications

User notifications

Here is the list of variables that affect user email notifications:

NOTIFY_USERS=email
NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>
NOTIFY_EMAIL_VERIFICATION_SUBJ # "Email verification" by default

Admin notifications

Admin would receive a message for each new comment on your site. Here is the list of variables that affect them:

NOTIFY_ADMINS=email
NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>
ADMIN_SHARED_EMAIL=admin@example.com

NOTIFY_EMAIL_FROM is required for both user and admin notifications. Without it, the server logs a warning naming it at start, sends no notification by email and does not offer readers an email subscription.

ADMIN_SHARED_EMAIL is required here: its default admin@ address is not used for notifications. Without it, the server logs notifications are not fully set up at start and sends no admin email.

Mailgun

Here is an example of a configuration using the Mailgun email service:

- SMTP_HOST=smtp.eu.mailgun.org
- SMTP_PORT=465
- SMTP_TLS=true
- SMTP_USERNAME=postmaster@mg.example.com
- SMTP_PASSWORD=secretpassword
- AUTH_EMAIL_FROM="Remark42 login"<notify@example.com>
- NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>

SendGrid

Here is an example of a configuration using the SendGrid email service:

- SMTP_HOST=smtp.sendgrid.net
- SMTP_PORT=465
- SMTP_TLS=true
- SMTP_USERNAME=apikey
- SMTP_PASSWORD=key-123456789
- AUTH_EMAIL_FROM="Remark42 login"<notify@example.com>
- NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>

Mailgun or SendGrid without exposing your server's IP

When you don't want to expose your IP (which is impossible with any SMTP provider) or when connecting to an external SMTP server is impossible due to firewall settings, set up an SMTP-to-API bridge and send messages through it.

To use any of the containers below within the Remark42 environment, set the following two SMTP variables:

- SMTP_HOST=mail
- SMTP_PORT=25

stevenolen/mailgun-smtp-server

Here is the docker-compose.yml configuration part spinning up a container for stevenolen/mailgun-smtp-server:

mailgun:
  image: stevenolen/mailgun-smtp-server
  container_name: "mail"
  hostname: "mail"

  logging:
    driver: json-file
    options:
      max-size: "10m"
      max-file: "5"

  environment:
    - MG_KEY=key-123456789
    - MG_DOMAIN=example.com

Please note that this Docker image is unmaintained and Europe domain names are not supported by this tool.

fgribreau/smtp-to-sendgrid-gateway

Here is the docker-compose.yml configuration part spinning up a container for fgribreau/smtp-to-sendgrid-gateway:

sendgrid:
  image: fgribreau/smtp-to-sendgrid-gateway
  container_name: "mail"
  hostname: "mail"

  logging:
    driver: json-file
    options:
      max-size: "10m"
      max-file: "5"

  environment:
    - SENDGRID_API=key-123456789

Gmail

If 2FA is enabled for your account, separate app password has to be generated. Configuration example for Gmail:

- SMTP_HOST=smtp.gmail.com
- SMTP_PORT=465
- SMTP_TLS=true
- SMTP_USERNAME=example.user@gmail.com
- SMTP_PASSWORD=aaaa bbbb cccc dddd
- AUTH_EMAIL_FROM="Remark42 login"<example.user@gmail.com>
- NOTIFY_EMAIL_FROM="Remark42 notifications"<example.user@gmail.com>

Amazon SES

Configuration example for Amazon SES (us-east-1 region):

- SMTP_HOST=email-smtp.us-east-1.amazonaws.com
- SMTP_PORT=465
- SMTP_TLS=true
- SMTP_USERNAME=access_key_id
- SMTP_PASSWORD=secret_access_key
- AUTH_EMAIL_FROM="Remark42 login"<notify@example.com>
- NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>

You must first verify a domain or email you will use in AUTH_EMAIL_FROM or NOTIFY_EMAIL_FROM.

Then you should obtain SMTP Credentials from Amazon SES Console.

Groundmail

Configuration example for Groundmail:

- SMTP_HOST=smtp.groundmail.dev
- SMTP_PORT=587
- SMTP_STARTTLS=true
- SMTP_USERNAME=groundmail
- SMTP_PASSWORD=gmsmtp_xxxxxxxxxxxx
- AUTH_EMAIL_FROM="Remark42 login"<notify@example.com>
- NOTIFY_EMAIL_FROM="Remark42 notifications"<notify@example.com>

SMTP_USERNAME is always groundmail, and SMTP_PASSWORD is an SMTP credential (starts with gmsmtp_) created in the Groundmail dashboard under SMTP. Verify the domain used in AUTH_EMAIL_FROM and NOTIFY_EMAIL_FROM before sending. If your host blocks outbound port 587, use SMTP_PORT=2525 with the same STARTTLS setting. See the Groundmail SMTP docs for account requirements.

Setup email authentication

Here is the list of variables that affect email authentication:

AUTH_EMAIL_ENABLE
AUTH_EMAIL_FROM
AUTH_EMAIL_SUBJ ("remark42 confirmation" by default)
AUTH_EMAIL_CONTENT_TYPE ("text/html" by default)

After you set SMTP_ variables, you can allow email authentication by setting the first two. AUTH_EMAIL_FROM is required: without it, the server logs a warning naming it at start and does not offer email sign-in.

- AUTH_EMAIL_ENABLE=true
- AUTH_EMAIL_FROM="Remark42 login"<notify@example.com>

A user signs in with a name and an email address. The name must contain 3 to 64 characters: letters, ASCII digits, underscores, hyphens or ordinary spaces, with no space at the start or end. A dot is refused, because the name appears in the email sent to an address nobody has confirmed yet. The token sent to the email address expires after 30 minutes.

HTML templates for emails and error messages

Remark42 uses golang templates for email templating. Templates are located in backend/app/templates/static and embedded into binary by go:embed directive.

Now we have the following templates:

  • email_confirmation_login.html.tmpl – used for confirmation of login
  • email_login.html.tmpl – used for the page the sign-in link in the login confirmation email opens, both before and after the sign-in
  • email_confirmation_subscription.html.tmpl – used for confirmation of subscription
  • email_subscribe.html.tmpl – used for the page the link in the subscription confirmation email opens, both before and after the subscription is confirmed
  • email_reply.html.tmpl – used for sending replies to user comments (when the user subscribed to it) and for noticing admins about new comments on a site
  • email_unsubscribe.html.tmpl – used for notification about successful unsubscribing from replies
  • error_response.html.tmpl – used for HTML errors

To replace any template, add the file with the same name to the working directory of the remark42 process: /srv in the Docker image, or the WorkingDirectory of a systemd service. In case you run Remark42 inside Docker Compose, you can put customised templates into a directory like customised_templates and then mount it like that:

volumes:
  - ./var:/srv/var
  - ./customised_templates/email_confirmation_login.html.tmpl:/srv/email_confirmation_login.html.tmpl:ro
  - ./customised_templates/email_login.html.tmpl:/srv/email_login.html.tmpl:ro
  - ./customised_templates/email_confirmation_subscription.html.tmpl:/srv/email_confirmation_subscription.html.tmpl:ro
  - ./customised_templates/email_subscribe.html.tmpl:/srv/email_subscribe.html.tmpl:ro
  - ./customised_templates/email_reply.html.tmpl:/srv/email_reply.html.tmpl:ro
  - ./customised_templates/email_unsubscribe.html.tmpl:/srv/email_unsubscribe.html.tmpl:ro
  - ./customised_templates/error_response.html.tmpl:/srv/error_response.html.tmpl:ro

The easiest way to test it is to mount error_response.html.tmpl, and then head to http://127.0.0.1:8080/email/unsubscribe.html, where you are supposed to see the page like the following:

Error_template

If the file is mounted correctly, the page will render the new file content immediately after hitting the refresh button in your browser once you change the file. This holds for the four page templates, email_login.html.tmpl, email_subscribe.html.tmpl, email_unsubscribe.html.tmpl and error_response.html.tmpl, which are read on every request. The three email templates are read once at start, so restart Remark42 after changing them.

Template variables

Each template has access to different variables. Email templates use Go's html/template, which escapes ordinary values for HTML output.

email_reply.html.tmpl — comment notification

Used for notifying users about replies to their comments and for admin notifications about new comments.

VariableTypeDescription
{{.UserName}}stringComment author name
{{.UserPicture}}stringComment author avatar URL; empty unless the avatar is stored by this server, so wrap the image in {{if .UserPicture}}
{{.CommentText}}stringComment body (HTML, with links and images removed)
{{.CommentLink}}stringDirect link to the comment. In an admin notification the page address is written again from its parts, and the link is empty when it is not a plain http or https address with a host, so wrap it in {{if .CommentLink}}
{{.CommentDate}}time.TimeComment timestamp, format with {{.CommentDate.Format "02.01.2006 at 15:04"}}
{{.ParentUserName}}stringParent comment author name
{{.ParentUserPicture}}stringParent comment author avatar URL; empty unless the avatar is stored by this server, so wrap the image in {{if .ParentUserPicture}}
{{.ParentCommentText}}stringParent comment body (HTML, with links and images removed)
{{.ParentCommentLink}}stringDirect link to parent comment, with the same rule for admin notifications
{{.ParentCommentDate}}time.TimeParent comment timestamp
{{.PostTitle}}stringTitle of the post
{{.Email}}stringRecipient email address
{{.UnsubscribeLink}}stringUnsubscribe URL
{{.ForAdmin}}boolTrue when this is an admin notification
{{.LinkHost}}stringHost that {{.CommentLink}} and {{.ParentCommentLink}} open, in ASCII, set in admin notifications only: the page address of a comment comes from the commenter, so the default template names the host next to both links

email_confirmation_subscription.html.tmpl — subscription confirmation

Sent when a user subscribes to email notifications about replies to their comments.

VariableTypeDescription
{{.User}}stringUsername, reduced to letters, digits, underscores and single spaces, at most 64 characters
{{.Token}}stringConfirmation token
{{.Email}}stringRecipient email address
{{.Site}}stringSite name
{{.SubscribeURL}}stringStart of the confirmation link; the link is {{.SubscribeURL}}{{.Token}}

email_subscribe.html.tmpl — subscription confirmation page

Shown at /email/subscribe.html when the link from the subscription confirmation email is opened. Without {{.Confirmed}} the page must offer a form posting back to itself, <form method="post"> with no action, as opening the link alone does not subscribe: mail scanners open links in incoming messages on their own. The token is in the page's address, so a form whose action leaves out the query gets an error page.

The page's Content Security Policy allows inline styles, a <script> carrying nonce="{{.Nonce}}", and requests and forms to Remark42 itself; images, fonts, external stylesheets and scripts without the nonce do not load. A copy needs no script: the default one uses it to send the form in place and close the tab afterwards.

VariableTypeDescription
{{.Site}}stringSite name
{{.Confirmed}}boolTrue once the form was submitted and the subscription is stored
{{.Nonce}}stringValue for the nonce attribute of a <script>; the page's Content Security Policy runs no script without it

email_confirmation_login.html.tmpl — login confirmation

Sent when a user logs in via email authentication.

VariableTypeDescription
{{.User}}stringUsername
{{.Token}}stringLogin token to paste into the comments
{{.LinkToken}}stringLogin token for the sign-in link; the link is {{.URL}}/email/login.html?tkn={{.LinkToken}}. Using one of the two tokens leaves the other valid
{{.URL}}stringRemark42 address, REMARK_URL
{{.Site}}stringSite name
{{.Address}}stringRecipient address

Shown at /email/login.html when the sign-in link from the login confirmation email is opened. It signs in the browser that opens it. Without {{.SignedIn}} the page must offer a form posting back to itself, <form method="post"> with no action, as opening the link alone does not sign in: mail scanners open links in incoming messages on their own. The rest of what the subscription confirmation page needs applies here too: the token is in the address, and the Content Security Policy is the same.

VariableTypeDescription
{{.User}}stringUsername
{{.Site}}stringSite name
{{.SignedIn}}boolTrue once the form was submitted and this browser is signed in, or when it already was
{{.Nonce}}stringValue for the nonce attribute of a <script>; the page's Content Security Policy runs no script without it

error_response.html.tmpl

Shown when the subscription confirmation page, the unsubscribe page or the sign-in link page fails.

VariableTypeDescription
{{.Error}}stringError message
{{.Details}}stringDescription of the failure

Plain-text emails

For plain-text login confirmations, set AUTH_EMAIL_CONTENT_TYPE=text/plain and provide a plain-text email_confirmation_login.html.tmpl:

Confirmation for {{.User}} on site {{.Site}}

Open this link to sign in in this browser:
{{.URL}}/email/login.html?tkn={{.LinkToken}}

Or copy this token into the login form:
{{.Token}}

Sent to {{.Address}}

The template still uses html/template, so values are HTML-escaped even with a plain-text content type. Comment and subscription notification emails always use text/html; there is no NOTIFY_EMAIL_CONTENT_TYPE setting. Keep their custom templates in HTML.