Send mail from shell script
There are thousands of ways to send an email from the Linux shell, but in this case we are going to explain how to do it using msmtp, something more complex than other ways, but useful in certain situations.
To do this we edit the msmtp file
#nano ~/.msmtprc
We are going to configure a GMail account, although it is also possible to do this with our SMTP server. We copy the following text
account gmail host smtp.gmail.com tls on tls_certcheck off port 587 auth login from yourusername user [email protected] password yourpassword
default account: gmail
Now it’s time to edit the mutt configuration file of the user of the machine we intend to configure.
#nano ~/.muttrc
We introduce the following text:
set from = “[email protected]” set realname = “YourUserName” set sendmail = /usr/bin/msmtp
As a final step, we only have to modify permissions to the configuration file, given that having something as delicate as a password, it is only accessible by the authenticated user:
chmod 0600 ~/.msmtprc
We already have everything configured, now we can send emails using the mutt command, for example, we send the .bash_history file
# mutt -s “sent from my console lol” [email protected] < /home/user/.bash_history
and we have our email sent from gmail using SMTP (it can be applied to other “non-gmail” accounts but we must check the settings of our email provider)