Installation | Maintenance | Beyond Lino

The email settings of a Lino site

Most Lino applications send emails to site users or business partners as part of their functionality. Here is an overview of the settings related to sending emails to the outside world.

These settings are usually stored in the local settings module.

local settings module

A module meant to contain Django settings to be shared to multiple sites on this machine, but only on this machine. It is conventionally named lino_local.settings.

Reference

Here are the Django settings for sending emails.

ADMINS

A list with one or more email addresses of site maintainers to be notified when an exception happens on the server.

EMAIL_HOST

The SMTP host that will accept outgoing mails from this site.

See also https://docs.djangoproject.com/en/5.0/ref/settings/#email-host

EMAIL_PORT

The port to use when submitting outgoing mails from this site to EMAIL_HOST.

See also https://docs.djangoproject.com/en/5.0/ref/settings/#email-port

EMAIL_HOST_USER

The user name to use when connecting to the EMAIL_HOST. If this is empty. Django will try to submit anonymously.

See also https://docs.djangoproject.com/en/5.0/ref/settings/#email-host-user

EMAIL_HOST_PASSWORD

The password to use when connecting as EMAIL_HOST_USER to the EMAIL_HOST.

See How to store secret credentials

See also https://docs.djangoproject.com/en/5.0/ref/settings/#email-host-password

SERVER_EMAIL

The address to use as sender in outgoing mails to the admins

DEFAULT_FROM_EMAIL

Default value for sender of outgoing emails when application code doesn’t specify it.

EMAIL_SUBJECT_PREFIX

The subject prefix to use for emails to the ADMINS.

See Django docs

Lino also uses this in lino.modlib.notify.

EMAIL_USE_SSL

Start an SMTP connection that is secure from the beginning using smtplib.SMTP_SSL.

See Django docs

EMAIL_USE_TLS

Start an unsecured SMTP connection and then use smtplib.SMTP.starttls() to encrypt it. This is the more secure successor of EMAIL_USE_SSL.

See Django docs

EMAIL_SSL_KEYFILE
EMAIL_SSL_CERTFILE

Both EMAIL_SSL_KEYFILE and EMAIL_SSL_CERTFILE may be set. When you have a keyfile, then you must also have a certfile. When none is set, Django will use create_default_context() (which is recommended according to Python Security considerations)

How to store secret credentials

Install the dotenv Python package:

pip install python-dotenv

Create a file ~/.env with this content:

EMAIL_HOST_USER=joe
EMAIL_HOST_PASSWORD=kkFUW8TRSXVgeq

In your settings.py file:

import dotenv; secrets = dotenv.dotenv_values()
EMAIL_HOST_USER = secrets['EMAIL_HOST_USER']
EMAIL_HOST_PASSWORD = secrets['EMAIL_HOST_PASSWORD']

Helo command rejected: need fully-qualified hostname

You may receive this error message when sending from a machine that has no public FQDN to an SMTP server that has strict smtpd_helo_restrictions.

Here is a hack to make Django use your IP address rather than socket.getfqdn() when sending email. Add the following to your settings.py file:

from socket import gethostname, gethostbyname
from django.core.mail import DNS_NAME
ip_addr = gethostbyname(gethostname())
DNS_NAME._fqdn = "[{}]".format(ip_addr)
print("Using your IP address [{}] as FQDN in HELO".format(ip_addr))