rss logo

GNU/Linux - sendmail to send emails from CLI or scripts

sendmail logo Debian Logo

I will put here some tricks that I use to send emails with the sendmail command.

sendmail command is very usefull to send emails in bash scripts.

Install and Configure msmtp

I personnaly use a private mail server but but it should also work with a gmail account.

Note: To do so, you need to enable less secure app (https://support.google.com/) in your gmail settings.

I don't use Exim anymore because it's horrible to set up under Debian.

  • Install msmtprc:
root@host:~# apt update && apt install ssmtp
  • Edit /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

Sending emails

Simple

  • Create a /tmp/mail.header file and write header:
To: user@shebangthedolphins.net
From: john@std.rocks
Subject: test mail

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

Send mails from a file list

  • Create /tmp/mail.list file:
user01@shebangthedolphins.net
user02@shebangthedolphins.net
user03@shebangthedolphins.net
  • Create a /tmp/mail.header.origin file with a temp@address wich will be replaced later:
To: temp@address
From: john@std.rocks
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: test mail
  • Create a /tmp/body:
Hi, this message has been sent with sendmail!
  • Send emails:
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)/" mail.header; cat /tmp/mail.header /tmp/body | /usr/sbin/sendmail -f john@std.rocks "$i"; done
  • Same but easier to read:
for i in $(cat /tmp/mail.list | sort -R | head -n 1); do
	sleep 20s					#wait 20 second beetween 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 mails from a file list

user@host:~$ for i in $(seq 1 60); do sleep 4h; for i in $(cat /tmp/mail.list.txt | sort -R | head -n 1); do cp /tmp/mail.header.origin /tmp/mail.header; echo "sending mail to $i"; sed -i "s/temp@shebangthedolphins.net/$i/" /tmp/mail.header; grep Date mail.header | sed -i "s/Date:.*/Date: $(date -R)/" mail.header; cat /tmp/mail.header /tmp/body | /usr/sbin/sendmail -f user@shebangthedolphins.net "$i"; done; done
/tmp/mail.header
X-Priority: 1 (Highest)
From: sender@shebangthedolphins.net
To: temp@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
IME-Version: 1.0
Disposition-Notification-To: <sender@shebangthedolphins.net>
cat /tmp/body 
Hello human world!

Headers tricks

If needed, we can add some headers:

  • Define content type and charset:
Content-type: text/plain; charset=utf-8
  • Set Mime-Version (Normally automatically added):
Mime-Version: 1.0
  • Set highest priority:
X-Priority: 1 (Highest)
  • Enable notification, to notify the sender about the ultimate disposition of the message :
Disposition-Notification-To: <sender@shebangthedolphins.net>

Send an email with an attachment

  • Convert the file to be sent to base64:
user@host:~$ base64 secret.svg 
PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdo
dD0iNjAwIj48cGF0aCBmaWxsPSIjRUQyOTM5IiBkPSJNMCAwaDkwMHY2MDBIMHoiLz48cGF0aCBm
aWxsPSIjZmZmIiBkPSJNMCAwaDYwMHY2MDBIMHoiLz48cGF0aCBmaWxsPSIjMDAyMzk1IiBkPSJN
CAwaDMwMHY2MDBIMHoiLz48L3N2Zz4=
  • Edit /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 /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
IME-Version: 1.0
Content-Type: multipart/mixed; boundary="19032019ABCDE"
  • Send email:
user@host:~$ cat /tmp/mail /tmp/mail | sendmail -f john@std.rocks user@shebangthedolphins.net