Sending emails from Linux Command line can be useful for testing and debugging your server's email whether it be Postfix, Sendmail etc. Here is how:
Quick & easy instructions:
From your linux command prompt, type:
sudo /usr/sbin/sendmail -f firstname.lastname@example.org email@example.com
- Open a terminal window to your server. I do this using Putty. Normally you can simply enter the IP or domain name in the host field of putty and connect with your user and password. If you happen to be on an Amazon cloud server like me, using an EC2 instance, then you'll put your IP or domain name in the host field, click on shh/auth in the left column and select the location of your .ppk file which has your security keys to access your server via shell.
- Once you're at your server's command prompt, simply enter: /usr/sbin/sendmail -f firstname.lastname@example.org email@example.com, replace from and to emails with actual email addresses and press enter. You should get a blank line as a response. If it didn't work like that, try using sudo first like: sudo /usr/sbin/sendmail -f firstname.lastname@example.org email@example.com
- Next, simply type your email content in the shell window. You can enter as many lines as you wish pressing enter after each until you're done with your email's content.
- Then press cntrl+d to send the email.
That's one way to do it, read on for other methods.
Sending Email from Linux command line using Mail
If you have the mailx package installed, you can also send emails even easier by typing the following at he command prompt:
mail -s "the subject" firstname.lastname@example.org
Replace my email with the email you want to send out to. Replace "the subject" with your own email subject and press enter.
Type your message, press enter again and hit cntrl+d to send the email.
If you get an error reading something like "bash mail not found", then you need to install the mailx package. If you are using Centos or Red Hat Linux, simply type the following command to install mailx:
sudo yum install mailx
...or if you're using Ubuntu or Debian Linux, use:
sudo apt-get install mailutils
Then try the above example again and it will work.
Here are some of the most used options to use with the mail command:
-s subject (The subject of the mail)
-c email-address (Mark a copy to this “email-address”, or CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC)
You could use these options like this:
echo "Your email content..." | mail -s "your subject" email@example.com -c firstname.lastname@example.org -b email@example.com
Remember to replace "Your email content..." with the content of your email body, "your subject" with your own email subject and the three example email addresses with your to-email address, CC address and BCC address respectively.
FYI: CC -sends a copy of the same email to the email named and BCC - sends a BLINK copy of the email. BCC stands for Blink Carbon Copy and is used in cases where you don't want the reciever seeing everytone ealse you have sent the same email to. CC works the same but people you CC can see who else you send the same email message to.
Sending email using content from a file
To use a .txt file or similar plain text file as the content for your outgoing email, use this command:
mail -s "subject" firstname.lastname@example.org < /var/www/html/email-content.txt
Avoid Ending up in Spam or Junk Mail folders
I did several tests to find the best way to send emails from the command line without ending up in the recipient's spam or junk mail folder. Since I was using an Amazon EC2 instance with SES, I will first explain the server environment used and then I'll show the results of several tests. Some went straight to spam and others went right to the recipients In Box as desired.
Amazon EC2 SES environment
Here are the circumstances that my test cases were executed under:
The following command line commands went to my In Box:
- /usr/sbin/sendmail email@example.com
- sudo mail firstname.lastname@example.org
- mail email@example.com
The following command line commands wen to my Spam:
- /usr/sbin/sendmail -f SESverified@email.com firstname.lastname@example.org
- sudo /usr/sbin/sendmail -f SESverified@email.com email@example.com
- sudo /usr/sbin/sendmail firstname.lastname@example.org
In the above examples, I didn't use parameters for subject, CC or BCC because they didn't make any difference. Only the use of sendmail or mail commands, sudo and the -f parameter made a difference on whether the emails were delivered to my in box or my spam folder.
As you can see from the above spam tests, the best way to avoid spam is to:
- Avoid using the -f parameter even with an SES verified email address.
- While /usr/sbin/sendmail method did work when avoiding both the -f param and the sudo command, the mail method worked with or without sudo.
- Either use /usr/sbin/sendmail without -f parameter and without using sudo, or use mail for the best chances of your email getting through to your recipients in boxes.
Having Trouble Sending Email?
If you're emails are not getting through and you don't know why, check your server's log files. Below I'll show you how to find mist email related error logs. They can normally be found in /var/spool/mail/username which on an Amazon Linux distro would be either:
NOTE: username, root and ec2-user are files, NOT directories. For example, navigate to /var/spool/mail like:
...and first open root file with your Linux command line text editor like:
sudo vi root
...and check for errors. TIP: use [shift]+g to skip to the last line of the file using VI editor.
There you have two method for sending emails from your Linux command line. One of the two is sure to work. If not, install mailx as we did above. There are yet other options available such as Mutt for sending emails with attachments(Google "Mutt email" for info)