Script to send bulk email to multiple users list

Share this post to your friends !

Script to send bulk email to multiple users list
5 (100%) 2 vote[s]

WriteBash - Today i will send you a script to send bulk email to multiple users list. The story is, I received a request to write a script to send email notifications to all users with specified content. The username and password for each person is written in a csv file.

Request analysis

I get a csv file, this file is a list of all users’ usernames and passwords, there are format for each line as follows:

"username@domain.com","password"

The requirement for me is to write a script for each user on that list using the username@domain.com email address. The email content includes the user’s UID (ie the username does not include @domain.com) and the corresponding password of the user.

What are the program ideas? Very simple, I will read each line of data in the list file. Get the email address, get the UID from the email address, get the corresponding password. And then call the mail function for each user.

Script to send bulk email

script-to-send-bulk-email-to-multiple-users-list Script to send bulk email to multiple users list
Script to send bulk email to multiple users list.

The whole script is in the content below.

#!/bin/bash
# Script author: Danie Pham
# Script site: https://www.writebash.com
# Script date: 04/12/2018
# Script version: 1.0
# Script use: this script use to send bulk email to multiple users list

# Define the input file
FILE_INPUT="/home/danie/input.csv"

# Function use to send email
f_sendemail () {
   mail -s "Notification your new password" -S from=system@domain.com $1 <<END_OF_MAIL
Dear $1,

Example content here.

Your username: $2
Your password: $3

Thanks and regards !

END_OF_MAIL
}

# Function to read the input file
f_readfile () {
   while read -r line
   do
      EMAIL_ACCOUNT=`echo $line | awk -F',' '{print $1}'`
      EMAIL_UID=`echo $line | awk -F',' '{print $1}' | awk -F'@' '{print $1}'`
      PASSWORD=`echo $line | awk -F',' '{print $2}'`

      # Call the function send email
      f_sendemail $EMAIL_ACCOUNT $EMAIL_UID $PASSWORD
   done < $FILE_INPUT
}

# Call the function "f_readfile"
f_readfile

exit

I will explain the script above. Script consists of two main functions:

Function 1: f_sendemail ()

We use this function to send email to each user. The function has three parameters: $1, $2, $3 correspond to three values are email address, UID, password.

There are 2 notes in this function:

Note 1: The function uses the mail command, if your system does not have this command, you need to install the mailx package for the server.

Note 2: The lines between the tags END_OF_MAIL must be left-justified, with no spaces or spaces before each line.

You can edit any content between two tags END_OF_MAIL. I put the words “Example content here.”

Function 2: f_readfile ()

This function will read each line in the input file. For each line, the function takes the values EMAIL_ACCOUNT, EMAIL_UID, PASSWORD. We use these three values to send to f_sendemail.

This function runs until the last line in the file and then exits the script.

Conclusion

This is a fairly simple script, but as a system admin, so it will be of great help to you. Or maybe you can use it in another job or you can know how to write a script so that others can understand.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.