logo rss

GNU/Linux - sendmail pour envoyer des courriels en CLI ou en scripts

Logo sendmail Logo Debian

Je mettrai ici quelques astuces que j'utilise pour envoyer des courriels avec la commande sendmail.

La commande sendmail est très utile lorsque l'on souhaite envoyer des e-mails dans des scripts bash.

Installer et Configurer msmtp

J'utilise personnelement un serveur de mails privé mais il est également possible d'utiliser un compte gmail.

Note : pour se faire il faudra au préalable activer le mode applications moins sécurisées depuis le compte gmail (https://support.google.com/).

  • Installer msmtprc :
root@host:~# apt update && apt install msmtp
  • Éditer le fichier /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

Envoyer des courriels

Simple

  • Créer un fichier /tmp/mail.header et configurer l'en-tête et le corp du message :
To: user@shebangthedolphins.net
From: john@std.rocks
Subject: test mail

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

Envoyer des mails depuis une liste d'adresses

  • Créer le fichier /tmp/mail.list et insérer les mails des destinataires :
user01@shebangthedolphins.net
user02@shebangthedolphins.net
user03@shebangthedolphins.net
  • Créer un fichier /tmp/mail.header.origin avec temp@address qui sera remplacé lors de l'envoi (avec sed -i) :
To: temp@address
From: john@std.rocks
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: test mail
  • Créer un fichier /tmp/body qui contiendra le corps du message :
Hi, this message has been sent with sendmail!
  • Envoyer les mails :
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
  • Identique mais plus facile à lire :
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

Autres en-têtes

Si nécessaire on peut ajouter d'autres en-têtes :

  • Définir le content type et le charset :
Content-type: text/plain; charset=utf-8
  • Définir le Mime-Version (Normalement automatiquement ajouté) :
Mime-Version: 1.0
  • Définir le highest priority :
X-Priority: 1 (Highest)
  • Activer la notification, pour infomer l'expéditeur de la destination finale du message :
Disposition-Notification-To: <sender@shebangthedolphins.net>

Envoyer un courriel avec une pièce jointe

  • Convertir le fichier à envoyer en base64 :
user@host:~$ base64 secret.svg 
PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdo
dD0iNjAwIj48cGF0aCBmaWxsPSIjRUQyOTM5IiBkPSJNMCAwaDkwMHY2MDBIMHoiLz48cGF0aCBm
aWxsPSIjZmZmIiBkPSJNMCAwaDYwMHY2MDBIMHoiLz48cGF0aCBmaWxsPSIjMDAyMzk1IiBkPSJN
CAwaDMwMHY2MDBIMHoiLz48L3N2Zz4=
  • Éditer le fichier /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
  • Éditer le fichier /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"
  • Envoyer le mail :
user@host:~$ cat /tmp/mail /tmp/mail | sendmail -f john@std.rocks user@shebangthedolphins.net