rss logo

Send Emails from the Command Line on GNU/Linux with sendmail and msmtp

sendmail logo Debian Logo

This article presents practical techniques to send emails from the command line using the sendmail command on GNU/Linux.

The sendmail command is particularly useful for sending emails from Bash scripts, enabling automation, monitoring, and system notifications.

Instead of configuring a full Mail Transfer Agent such as Exim, which can be complex to set up on Debian, this guide relies on a lightweight and efficient solution based on msmtp.

Install and Configure msmtp on Debian

In this guide, msmtp is used as a simple SMTP client acting as a sendmail replacement. While the examples are based on a private mail server, the configuration can also be adapted for common providers such as Gmail.

💡 Note: Since May 1, 2025, Google no longer allows SMTP authentication using simple username/password credentials. OAuth2 authentication is now required. Refer to the Google's official announcement here: support.google.com. Integration with sasl-xoauth2 may be used (GitHub repository), although this setup has not been tested in this article.

  • Install the msmtp package:
root@host:~# apt update && apt install msmtp
  • Create a symbolic link so that sendmail is provided by msmtp:
root@host:~# ln -s /usr/bin/msmtp /usr/bin/sendmail
  • Edit the global configuration file /etc/msmtprc:
# Set default values for all accounts.
defaults
auth           on	# Enable SMTP authentication
tls            on	# Use TLS to secure the connection
tls_trust_file /etc/ssl/certs/ca-certificates.crt	# Path to trusted CA certificates
logfile        /var/log/msmtp.log

#account n°1 settings
account stdrocks 
host mail.std.rocks	# SMTP server hostname
port 465		# SMTP port
from user@std.rocks	# Email address used in the 'From' header
user user@std.rocks	# SMTP login username
password MyUserP@ss	# SMTP password
tls_starttls off	# Disable STARTTLS
tls_certcheck off	# Disable TLS certificate verification

account default : stdrocks

Send Emails from the Command Line with sendmail

Send a Simple Email with sendmail

  • Create a /tmp/mail.header file and define the email headers and body:
To: user@shebangthedolphins.net
From: john@std.rocks
Subject: test mail

Hello human world!
This message has been sent with sendmail!
  • Send the email from the command line:
user@host:~$ cat /tmp/mail.header | sendmail -f john@std.rocks user@shebangthedolphins.net

Send Emails to Multiple Recipients from a File

  • Create a /tmp/mail.list file containing the list of recipient email addresses:
user01@shebangthedolphins.net
user02@shebangthedolphins.net
user03@shebangthedolphins.net
  • Create a template header file /tmp/mail.header.origin containing a placeholder email address (temp@address) that will be replaced dynamically:
To: temp@address
From: john@std.rocks
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: test mail
  • Create the email body file /tmp/body:
Hi, this message has been sent with sendmail!
  • Send emails using a loop and a recipient list:
user@host:~$ for i in $(cat /tmp/mail.list | sort -R | head -n 1); do sleep 20; cp /tmp/mail.header.origin /tmp/mail.header; echo "sending mail to $i"; sed -i "s/temp@address/$i/" /tmp/mail.header; sed -i "s/Date:.*/Date: $(date -R)/" /tmp/mail.header; cat /tmp/mail.header /tmp/body | /usr/sbin/sendmail -f john@std.rocks "$i"; done
  • Same script, formatted for better readability:
for i in $(cat /tmp/mail.list | sort -R | head -n 1); do
   sleep 20s					#wait 20 second between each mail
   cp /tmp/mail.header.origin /tmp/mail.header	#copy template header to current header
   echo "sending mail to $i" 	#echo current recipient
   sed -i "s/temp@address/$i/" /tmp/mail.header		#replace temp@address by current recipient mail address in /tmp/mail.header 
   sed -i "s/Date:.*/Date: $(date -R)/" /tmp/mail.header		#replace Date field by current date in /tmp/mail.header
   cat /tmp/mail.header /tmp/body | /usr/sbin/sendmail -f john@std.rocks "$i" #send email to current recipient
done

Send Emails Repeatedly with Delays

Use this loop to automate recurring email notifications from the Linux command line: it sends one message every 4 hours to a fixed recipient via sendmail and refreshes the Date header for each delivery.

  • First, create a header template file /tmp/mail.header:
X-Priority: 1 (Highest)
From: sender@shebangthedolphins.net
To: dest@shebangthedolphins.net
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: STD: Subject that I want
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Disposition-Notification-To: <sender@shebangthedolphins.net>
  • Then create your email body file /tmp/body:
echo "Hello human world!" > /tmp/body
  • Finally, run this command to send the emails:
user@host:~$ for i in $(seq 1 60); do
   echo "sending mail number $i"
   sed -i "s/^Date:.*/Date: $(date -R)/" /tmp/mail.header
   cat /tmp/mail.header /tmp/body | /usr/sbin/sendmail -f user@shebangthedolphins.net dest@shebangthedolphins.net
   sleep 4h
done

Email Header Tips

You can add or adjust email headers to control how messages are handled and displayed by mail clients.

  • Define the content type and character encoding:
Content-Type: text/plain; charset=utf-8
  • Set the MIME version (usually added automatically):
MIME-Version: 1.0
  • Set the message priority:
X-Priority: 1 (Highest)
  • Enable delivery status notifications to notify the sender when the message is delivered or read:
Disposition-Notification-To: <sender@shebangthedolphins.net>

Send an Email with an Attachment Using sendmail

This example shows how to send an email with an attachment by manually building a MIME multipart message and encoding the file in Base64.

  • First, convert the file to be attached to Base64:
user@host:~$ base64 secret.svg 
PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdo
dD0iNjAwIj48cGF0aCBmaWxsPSIjRUQyOTM5IiBkPSJNMCAwaDkwMHY2MDBIMHoiLz48cGF0aCBm
aWxsPSIjZmZmIiBkPSJNMCAwaDYwMHY2MDBIMHoiLz48cGF0aCBmaWxsPSIjMDAyMzk1IiBkPSJN
CAwaDMwMHY2MDBIMHoiLz48L3N2Zz4=
  • Create the email body with a multipart MIME structure in /tmp/body:
--19032019ABCDE
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

Hi, this message has been sent with sendmail!

--19032019ABCDE
Content-Type: application/svg; name="secret.svg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="secret.svg"

PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdo
dD0iNjAwIj48cGF0aCBmaWxsPSIjRUQyOTM5IiBkPSJNMCAwaDkwMHY2MDBIMHoiLz48cGF0aCBm
aWxsPSIjZmZmIiBkPSJNMCAwaDYwMHY2MDBIMHoiLz48cGF0aCBmaWxsPSIjMDAyMzk1IiBkPSJN
CAwaDMwMHY2MDBIMHoiLz48L3N2Zz4=
--19032019ABCDE
  • Edit the email header file /tmp/header:
To: user@shebangthedolphins.net
From: john@std.rocks
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: test mail
Content-type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="19032019ABCDE"
  • Send the email with the attachment:
user@host:~$ cat /tmp/header /tmp/body | sendmail -f john@std.rocks user@shebangthedolphins.net