Linux sendmail / mail, use STMP to send mail from command line, with Postfix (Extra bonus: The best smartphone and desktop clients)

Testing the systems mail ability:

To send an email from the command line in Linux, you can use the mail or sendmail command, depending on your system configuration. Here’s a basic example using the mail command:

echo "This is the body of the email" | mail -s "Subject of the email" recipient@example.com

Alternatively, if your system is configured to use sendmail, you can use it to send emails. Here’s an example:

echo "Subject: Subject of the email\n\nThis is the body of the email" | sendmail -t recipient@example.com



Mail Clients Desktop/Mobile: Finding the Right Fit

When it comes to choosing a mail client, there are several options to consider.

Vivaldi Mail Client: Its a browser and a mail client, two in one. Vivaldi offers a professional email client. Its robust features include an extensive mail labeling and organizing system, providing a seamless experience for users.

Thunderbird: While Thunderbird has been a longstanding choice, I’ve found myself less inclined to recommend it recently. The internal mechanisms may lead to bugs and freezes over a period of time of use plus, it lacks some features, like having a place to display comprehensively all mails from all folders by deafult.

Microsoft Outlook: What is this, 1991 and we are on a Windows 98 machine? Microsoft Outlook, reminiscent of a bygone era, fails to impress in today’s competitive landscape. Its compatibility issues, particularly on mobile platforms, make it a less appealing option. In contrast,

Apple’s MacOS mail client offers a smoother experience, seamlessly integrating with various SMTP servers, i even liked working with the MacOS mail client, its not something too big or too impresive, its a simple tool that does its job with no pains in the dash.

BlueMail: A newcomer to the scene, BlueMail stands out for its user-friendly interface and compatibility across devices. It excels on smartphones, offering a seamless experience, albeit with some limitations on PC. Notably, it may struggle with handling large file attachment uploads.

Linux sendmail / mail system

Assume you have a FreeBPX – Asterisk system or any kind of software that wants to use the systems sendmail command to send mails and you want to set up that with an SMPT server

Lets install postfix for that:

sudo apt update
sudo apt install postfix

When installing on “select the configuration type” question select [ Internet Site ]

If it was already installed before this,then do

sudo dpkg-reconfigure postfix

And then select “Internet site”

We will assume our settings is this:

serversmpt.example.com
port TLS465
usernamemymail@example.com
password12345

sudo nano /etc/postfix/main.cf

Now we have main.cf in our editor we change the red lines:

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2



# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
#smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache


smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = tele-HeroBox.lan
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = $myhostname, tele-HeroBox, localhost.localdomain, , localhost
relayhost = [smpt.example.com]:465
smtp_use_tls = yes

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_security_level = encrypt
smtp_sasl_security_options = noanonymous
smtp_tls_wrappermode = yes

smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_protocols = !SSLv2, !SSLv3
smtp_tls_mandatory_protocols = !SSLv2, !SSLv3
smtp_tls_protocols = !SSLv2, !SSLv3

sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps =  regexp:/etc/postfix/sender_canonical_maps
smtp_header_checks = regexp:/etc/postfix/header_check

#Settings to Keep the from address the same
sender_canonical_classes = envelope_sender, header_sender
sender_canonical_maps =  regexp:/etc/postfix/sender_canonical_maps
smtp_header_checks = regexp:/etc/postfix/header_check

disable_dns_lookups = no

After you make sure the configuration looks alright for your goals, hit ctrl+o on your keyboard and press enter to save the file. Then restart postfix.

sudo systemctl restart postfix

Step 2: Update Password Files for SMTP Authentication:

Update password files for SMTP authentication, follow these steps:

  1. Edit the sasl_passwd file to update credentials for the new server:
sudo nano /etc/postfix/sasl_passwd

Make sure the file looks like this: [server]:port username:password

[smpt.example.com]:465 mymail@example.com:12345
  1. Save the changes and exit the text editor.
  2. Create a Postfix lookup table from the sasl_passwd file:
sudo postmap /etc/postfix/sasl_passwd
  1. Secure the password file:
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
  1. Reload Postfix to apply the changes:
sudo systemctl reload postfix

#Settings to Keep the from address the same

In order to keep the “Form:” Mail address the same of your SMTP server all the time, to avoid getting labeled as spam or blocked as spoofing, we added these two canonical maps into our main.cf, we need to create these files as:

  1. Type “/.+/ mymail@example.com” to the file /etc/postfix/sender_canonical_maps
sudo nano /etc/postfix/sender_canonical_maps
/.+/ mymail@example.com
  1. Type “/From:.*/ REPLACE From: mymail@example.com” to the file /etc/postfix/header_check
sudo nano /etc/postfix/header_check
/From:.*/ REPLACE From: mymail@example.com

Restart the postfix

service postfix restart
  1. Do a test again
echo "This is the body of the email" | mail -s "Subject of the email" recipient@example.com

And thats all.. hope you fixed your issues or found this usefeul

One comment

Comments are closed.