How Do You Send an Email from the Command Prompt of Linux to a Long List of Recipients?

Problem scenarios
One or both of the following apply:

#1 You want to automate the sending of the exact same email to multiple people. You do not want to manually type in the recipients. You have the same subject and body that each recipient should receive. You want to customize the "from address" that the recipients see. How do you do this?

OR

#2 You are using the mail command on a Linux server. You want to customize the "from" address in the emails that you send. What do you do?

Solution
Prerequisites
You have installed mailx. If you are using a Red Hat derivative (e.g., CentOS/RHEL/Fedora), run this command: sudo yum -y install mailx

Solution
1. Create a file called list.csv. In it, put your email addresses inside. They should be separated by a comma. A space can be after the comma, but need not be.
2. Draft then use a command like this:

echo "payLoad of Message" | mail -s "placeSubjectHere" -r email@from.com $(cat list.csv)

-replace "payload of Message" with the message you want to send
-replace "placeSubjectHere" with the subject of your email
-replace "email@from.com" with the email address that you want the email to appear to be from

An Alternative Way To Send Email
Another way to do this is with Python; to learn how to use Python and not install anything else, see this external posting.

Leave a comment

Your email address will not be published. Required fields are marked *