Script to send bulk email to multiple users list

by Daniel Pham
Published: Updated:

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:

"[email protected]","password"

The requirement for me is to write a script for each user on that list using the [email protected] 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.

The whole script is in the content below.

#!/bin/bash
# Script author: Daniel Pham
# Script site: https://devopslite.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/daniel/input.csv"

# Function use to send email
f_sendemail () {
   mail -s "Notification your new password" -S [email protected] $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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.