While trying to automate programs/application, sending mails from command line would really ease the job. Linux Mail command helps us to send email through the command line. Linux Mail is dependent on postfix / sendmail email services. With Linux Mail, we can send bulk emails.
1. Install mail utilities
#yum install mailx*
2. Send emails via command line as:
# mail -s "Test mail" user@domain.com <<< "Mail content"
- OR -
# echo "Test mail" | mail -s "Mail content" user@domain.com
-s prefix is used to provide email subject
3. to send contents of a file as email:
# mail -s "Mail Subject" user@domain.com < /etc/my.cnf
- OR -
# cat /etc/my.cnf | mail -s "MySQL config file" user@domain.com
4. CC and BCC can be added as:
# echo "Email Body" | mail -s "Test mail" user@domain.com -c user@gmail.com -b user@yahoo.com
5. By default mails from the server will be sent as the user 'user@serverName'. We can send emails as a specific user by:
# echo "Email Body" | mail -s "subject" someone@email.com -r user@domain.com
6. Sending emails to multiple recipients:
# mail -s "Subject" user@gmail.com,user@yahoo.com < /etc/my.cnf
7. To send attachments via email, we will have to use 'uuencode' command to send attachment as Linux mail doesn't allow us to send email attachments:
#yum install sharutils*
#uuencode log.txt | mail -s "Subject" user@domain.com < EmailBody.txt
Shell script automation tasks can be easily integrated using the Linux mail command.