Ubuntu: how to install and configure SMTP server – part 2: set a password for a Postfix SMTP user


Part 1: Ubuntu: how to install and configure SMTP server

Part 2: set a password for a Postfix SMTP user

To set a password for a Postfix SMTP user, you need to use the saslpasswd2, a control tool for the SASL (Simple Authentication and Security Layer) authentication system. Here are detailed instructions:

Install SASL : Make sure you have the package installed sasl2-bin so the tool is available saslpasswd2. If not, you can install it with the following command:

sudo apt update
sudo apt install sasl2-bin

result:

...
Preconfiguring packages ...
Selecting previously unselected package db5.3-util.
(Reading database ... 73725 files and directories currently installed.)
Preparing to unpack .../db5.3-util_5.3.28+dfsg1-0.8ubuntu3_amd64.deb ...
Unpacking db5.3-util (5.3.28+dfsg1-0.8ubuntu3) ...
Selecting previously unselected package db-util.
Preparing to unpack .../db-util_1%3a5.3.21~exp1ubuntu4_all.deb ...
Unpacking db-util (1:5.3.21~exp1ubuntu4) ...
Selecting previously unselected package sasl2-bin.
Preparing to unpack .../sasl2-bin_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
Unpacking sasl2-bin (2.1.27+dfsg2-3ubuntu1.2) ...
Setting up db5.3-util (5.3.28+dfsg1-0.8ubuntu3) ...
Setting up db-util (1:5.3.21~exp1ubuntu4) ...
Setting up sasl2-bin (2.1.27+dfsg2-3ubuntu1.2) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning processor microcode...
Scanning linux images...

Running kernel seems to be up-to-date.

The processor microcode seems to be up-to-date.

Restarting services...
 systemctl restart php8.1-fpm.service supervisor.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Generate password for SMTP user : Use command saslpasswd2 to generate password for SMTP user. For example, to create a password for a user user1:

sudo saslpasswd2 -c -u <your_realm> user1

Where,

<your_realm>

is the domain you want to use for authentication. Usually the domain name of the system. You will be asked to enter the password for the user after running this command.

example: sudo saslpasswd2 -c -u tutorialspots.com usersmtp

Configure Postfix to use SASL : Open Postfix’s configuration file ( /etc/postfix/main.cf ) and add or modify the following settings:

smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = <your_realm>
broken_sasl_auth_clients = yes

Restart the Postfix service : After you have performed the above steps, restart the Postfix service to apply the changes:

sudo systemctl restart postfix

Now, the user has the password and can use it for authentication when sending emails through Postfix’s SMTP server. Note that you need to notify users about the generated password so they can use it when necessary.

Test send email with command mail

When your SMTP server requires password authentication, you need to provide credentials when using the mail. You can do this by using file ~/.mailrc to set SMTP configuration parameters, including username and password.

Here’s how to use the command mail to send email when the SMTP server requires password authentication:

Create or edit files ~/.mailrc:

nano ~/.mailrc

Add the following lines to the file:

set smtp-use-starttls
set smtp=smtp://<username>:<password>@<smtp_server>:<port>

set smtp-use-starttls
set smtp=smtp://usersmtp:usersmtppassword@mail.tutorialspots.com:25

Replace

<username>, <password>, <smtp_server> and <port>

with the corresponding SMTP server information.
Save and close the file.

Mail command:

echo "email content" | mail -s "email subjet" -a "From: selector@domain.com" recipient@example.com

For example:

echo "Test email with authentication" | mail -s "Test email" -a "From: postmaster@tutorialspots.com" recipient@example.com

When you run this command mail it will use the credentials you provide to authenticate to the SMTP server and send email.

Leave a Reply