Postfix E-Mail Configuration for Koha on Linux

What is Postfix and Why do we need it?

Having a Mail server on your network is one of the essential things for a server administrator, and for a Library Management System, it is important to keep your users, informed about their circulation activities and new updates of the library, the library patrons need to be informed, whenever they checkout or check-in a book, and also to send reminders about their overdue, and fines. It is best to do this task by sending automated E-Mails. But this is applicable in other scenarios as well, be it a database administrator who needs email updates about scheduled backups, and a web administrator who needs to receive system load notifications through email. Postfix is a fast and popular Mail Transfer Agent suitable (MTA) server widely used with Linux servers. It is Free and Open Source. Its main job is to relay mail locally or to an intended destination outside the network. Some Other SMTP servers are Sendmail, Postfix, and Qmail. Koha users can install Postfix to send email notices to library users. The Installation part of Postfix is common for all other server uses as well. Here I explain steps to install and configure Postfix on any Debian based Linux server, I am using Ubuntu 18.04 in this exercise.

Prerequisites

We need to line up some essential things before installing and configuring Postfix. A Fully Qualified Domain Name is required in order to configure Postfix, but you can either use an existing Gmail account or create a new one for the purpose send notices from Koha. Keep in mind that Gmail prevents sending emails from any third party utility. so do enable Less secure apps in the Gmail account settings.

Installing Postfix

Postfix is included in Ubuntu’s default repositories, all you need to do is type in the following command in the terminal window.

sudo apt-get update
sudo apt-get install postfix

Additionally, Install the other required packages. (In my case this was installed along with postfix)

apt-get install libsasl2-2
apt-get install libsasl2-modules
apt-get install ca-certificates

Also, install the ‘mailutils’ which we will be need later for a test run.

sudo apt-get install mailutils

When the popup text window appears in the terminal window select OK and press enter.

Next, you can select “No Configuration” if you want to avoid any configuration wizard questions on the terminal screen because ultimately we will be making the configuration in the config file directly. but if you want to go through it you can select “Internet Site” which will prompt you with some questions and based on your input will create a fresh conf file in the postfix installation directory. I selected “No Configuration” and simply

Postfix gets its configuration from the “main.cf” file in “/etc/postfix” folder, so the settings in this file are critical. A fresh templet of this configuration file is located in the “/usr/share/postfix” folder. We copy the “main.cf.debian” templet file into “/etc/postfix/” folder with following command. Now, remember this file is important and all the critical settings are stored in this file only.

cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf

Open this main.cf file with any editor of your choice and add a few lines.

sudo nano /etc/postfix/main.cf

The following lines are to be added at the bottom of the file. once done save and close the file.

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Because we are using Google’s SMTP server, which uses SASL it is important to enabling SASL Authentication in the configuration file. The login credentials of the email to be used are not saved in the same configuration file, Those are stored in a separate file in the postfix directory. To specify the username and password, create a new file named “sasl_passwd” by using:

sudo nano /etc/postfix/sasl_passwd

Add the following line with the Gmail User id and password and save it.

[smtp.gmail.com]:587    yourID@gmail.com:yourpassword

Then change the access permission of this file by chmod command as follows:

chmod 600 /etc/postfix/sasl_passwd

This will write-protect this file, so nobody messes with your E-Mail credentials.

Next, we need to translate the /etc/postfix/sasl_passwd to Postfix lookup tables so that postfix can read and use it, run the following command to do that:

postmap /etc/postfix/sasl_passwd

Now create aliases.DB run the following command

postalias hash:/etc/aliases

Public-key cryptography is a method of encrypting application traffic using a Secure Socket Layer (SSL) or Transport Layer Security (TLS) connection, and Certificate is a method used to distribute a public key and other information about a server and the organization who is responsible for it. The smtp_tls_CAfile field defines a file containing CA certificates of root CAs trusted to sign either remote SMTP server certificates or intermediate CA certificates, and stmp_use_tls uses TLS when a remote SMTP server announces STARTTLS support, the default is not using TLS. The default template of the CA Certificate is located in /etc/ssl/certs folder, you can make a copy of the file into the postfix folder by using the following command:

sudo cp /etc/ssl/certs//thawte_Primary_Root_CA.pem /etc/postfix/cacert.pem

This would make a copy of the cacert.pem file in the postfix directory. with this, your postfix server all loaded and ready, to fire it up for the first time just Restart the postfix to reload the configuration with the following command:

sudo /etc/init.d/postfix restart

at this stage, the postfix relay would be working perfectly and we can check it by sending a test email with the following command.

echo This is a test mail from postfix" | mail -s "Sendmail from postfix Relay" youremail@gmail.com

Additionally, we can monitor ‘mail.log’ or ‘mail.err’, in case there is an error.

Enabling E-Mails in Koha

After having the postfix relay server running perfectly, we need to enable the email service in Koha and we can use the following command to do so:

koha-email-enable <your koha instanace name>

This is all we had to do in the command line, now few parameters are to be set in the Koha Web UI. for this, we need to enable email notifications for each patron category from the koha administration page.

Koha --> Administration --> Patron Categories --> Overdue Notice required  --> YES.

Then, we need to make some changes in Global System preferences:

Global System preferences –> Administration–>  KohaAdminEmailAddress,  and mention the koha admin email id from which the emails will be sent. This would be the same email id we used in the postfix configuration.

After typing in the email id, from which the mails will be sent, go to the ‘Patrons’ tab in Global System Preferences and turn on the following parameters.

Gobal System preferences-->Patron  -->AutoEmailOpacUser --> send
Gobal System preferences-->Patron  -->AutoEmailPrimaryAddress --> first valid
Gobal System preferences-->Patron -->Enhancedmessagingpreferences --> Allow

To send the notices and emails to the users on a particular event, like, while checking out a book, checking in a book, overdue notice, updating profile, etc settings in the Notice and Triggers section in tools need to be configured. To do this go to Koha –> Tools –> Overdue Notice/status triggers

The templates used to craft the emails are defined in the “Tools: Notices” section of the staff interface in Koha. To customize the notices you can go to Koha–> Tools –> Notices & Slips.

It is a good idea to do configure this task on the automatic mode by using cron jobs so that the server prepares the messages and pushes them to the mail transport agent on a given time schedule daily. To set up a cronjob to activate the scripts.

cd /usr/share/koha/bin/cronjobs
./overdue_notices.pl
./advance_notices.pl -c
./process_message_queue.pl

Setup a cronjob

crontab -e

and add the following line at the end of the file:

run cron at 5 p.m everyday
0 17 * * * /etc/cron.daily/koha-common

It is advised to set up your circulation rules before setting up the automatic cron jobs, so each user gets appropriate notification and reminder at the appropriate time.