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.
I personnaly have my own mail server but I will show here how to set Exim working as a smarthost with a gmail account.
dc_eximconfig_configtype='smarthost' dc_other_hostnames='' dc_local_interfaces='127.0.0.1 ; ::1' dc_readhost='' dc_relay_domains='' dc_minimaldns='false' dc_relay_nets='' dc_smarthost='smtp.gmail.com:587' CFILEMODE='644' dc_use_split_config='true' dc_hide_mailname='false' dc_mailname_in_oh='true' dc_localdelivery='mail_spool'
*:std_2021@gmail.com:myGMAILpassword123
Now that our Exim smarthost is correctly set we can use our sendmail command.
To: user@shebangthedolphins.net From: std_2021@gmail.com Subject: test mail Hello human world! This message has been sent with sendmail!
user@host:~$ cat /tmp/mail.header | sendmail -f std_2021@gmail.com user@shebangthedolphins.net
user01@shebangthedolphins.net user02@shebangthedolphins.net user03@shebangthedolphins.net
To: temp@address
From: std_2021@gmail.com
Date: Thu, 17 Jun 2021 08:45:25 +0200
Subject: test mail
Hi, this message has been sent with sendmail!
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 std_2021@gmail.com "$i"; done
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 std_2021@gmail.com "$i" #send email to current recipient done
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!
If needed, we can add some headers :
Content-type: text/plain; charset=utf-8
Mime-Version: 1.0
X-Priority: 1 (Highest)
Disposition-Notification-To: <sender@shebangthedolphins.net>
user@host:~$ base64 secret.svg PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5MDAiIGhlaWdo dD0iNjAwIj48cGF0aCBmaWxsPSIjRUQyOTM5IiBkPSJNMCAwaDkwMHY2MDBIMHoiLz48cGF0aCBm aWxsPSIjZmZmIiBkPSJNMCAwaDYwMHY2MDBIMHoiLz48cGF0aCBmaWxsPSIjMDAyMzk1IiBkPSJN CAwaDMwMHY2MDBIMHoiLz48L3N2Zz4=
--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
To: user@shebangthedolphins.net
From: std_2021@gmail.com
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"
user@host:~$ cat /tmp/mail /tmp/mail | sendmail -f std_2021@gmail.com user@shebangthedolphins.net
Contact :