PowerShell script to send emails to multiple users

Table of Contents


Powershell script to send email to bulk users in there respective Language (English, French and Spanish)
1 Purpose of document
2 Prerequisites
2.1 List of users
2.2 System requirement
3 Script Logic
4 Script Execution
5 Script Files
5.1 Script:
5.2 Input File:
5.3 Script Execution:
6 Output
6.1 Output File:
6.2 Email Notification
6.3 Email contents
7 End of Document

Powershell script to send email to bulk users in there respective Language

1 Purpose of document

Purpose of this document is to explain how to send email to multiple external users (Out of the organization users) in their respective communication language using PowerShell Script.

2 Prerequisites

2.1 List of users

  • Require list of users (external users) to whom need to send the communication.

2.2 System requirement

  • PowerShell script can be executed from any of the system which is domain joined (may be from JumpServer/ Management server in your organization).
  • Jump Server should be reachable to your organization exchange server and firewall rules should be open.
  • Firewall rule should be open between Source (Jump Server), Destination (Exchange Server), Port 25 (TCP)
  • You can also check your receive connector configuration on your exchange Server, to ensure your mail server is willing to relay emails from your Jump Server. More detail about receive connector you may refer MicroSoft article Receive Connectors.

Considering below are the system names.

System Names Name
AD Domain YOURDOMAIN.COM
JumpServer name JUMP01.YOURDOMAIN.COM
Exchange Server name EXCH01.YOURDOMAIN.COM

3 Script Logic

  • Provide external users list as input file (CSV file name “EXT-User-List.csv”).
  • CSV file first line should contain columns as below (Case sensitive) marked in blue. Below emails are randomly put here just to explain..
  • Only three languages are considered (English, French, and Spanish)
First Name Surname Full Name Email Com. Language
Niraj Srivastava Niraj Srivastava nirajsrivastava1@gmail.com English
Mohenjo Daro Mohenjo Daro Mohenjo.daro@testing.com French
Cool Stuff Cool Stuff Cool.stuff@stuffs.com Spanish
  • Script will send email in respective languages.
    1. If “Com. Language” in CSV file is “English” it will send email in English.
    2. If “Com. Language” in CSV file is “French” it will send email in French and English.
    3. If “Com. Language” in CSV file is “Spanish” it will send email in Spanish and English.
    4. If “Com. Language” in CSV file is “any other language” it will send email in English.

4 Script Execution

Considering System requirement is fulfilled as per Section 2.2 and able to send email form Jump Server. Create below folders/ file on Jump Server JUMP01.YOURDOMAIN.COM.

  1. Create folder “C:\NSITAutomation\Scripts\”
  2. Create folder “C:\NSITAutomation\Scripts\INPUT”
  3. Create folder “C:\NSITAutomation\Scripts\ OUTPUT”
  4. Create CSV file “EXT-User-List.csv” under folder “C:\NSITAutomation\Scripts\INPUT” and provide contents as explained in “Section 3”.
  5. Script will capture log in comma separate file “EmailSentToUser_27-Nov-2019.log”format in folder “C:\ NSITAutomation\Scripts\OUTPUT”.

5 Script Files

Script, sample input, output, email notification attached.

5.1 Script:

Script is written in PowerShell

PowerShell Script Start

## Owner : Niraj Srivastava
## Date : 27-Nov-2019
## Version : 1.0 Final Version OK with Attachment
## Send One time email to External users

cls
$str = "*" * 130
$dt= (Get-Date).ToString("dd-MMM-yyyy")
$dt
$hr= (get-date).hour
$min = (get-date).minute
$sec = (get-date).second
$tm = "$hr"+":"+"$min"+":"+"$sec"
$tm

$inptfile2 = "C:\NSITAutomation\Scripts\INPUT\EXT-User-List.csv"
$emailsenttocsv = "C:\NSITAutomation\Scripts\OUTPUT\EmailSentToUser_$dt.log"

#Remove-Item –path $outputmimcsv2 -ErrorAction SilentlyContinue
Remove-Item –path $emailsenttocsv -ErrorAction SilentlyContinue

$rpt = "Report Date,$dt $tm,Status1,Status2,Status3,Status4"
$headers = "FirstName,LastName,FullName,Email,Language,Message"

write $rpt  | Out-File -filepath  $emailsenttocsv
write $headers  | Out-File -filepath $emailsenttocsv -Append
#############
# Prompt to connect to SMTP Server
$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")


if(!(Test-Path -Path $inptfile2 ))
{
cls
write $str
Write "No input file found $inptfile2, Please create inputfile CSV file."
Write "Input CSV file should contain data as show below. CSV file should contain first line exactly(Case sencitive) shown below.Input CSV file should contain one user detail per line"
Write ""
write $str
Write "First Name,Surname,Full Name,Email,Com. Language"
Write "Niraj,Srivasatava,Niraj Srivasatava,nirajsrivastava1@gmail.com,English"
Write "Niraj,Srivasatava,Niraj Srivasatava,niraj.srivastava@nsitautomatio.in,Spanish"
Write "Niraj,Srivasatava,Niraj Srivasatava,nirajsrivastava1@gmail.com,French" 
write $str
}
else
{
$usersincsv = import-csv $inptfile2 | sort

#$emailfrom = "yourorg.admin@yourdoamin.com" ## email Should be from your Domain.
$emailfrom = "niraj.srivastava@nsitautomation.in" ## email Should be from your Domain.
#$emailcc = "yourorg.manager@yourdoamin.com"
$emailcc = "niraj.srivastava@nsitautomation.in"
#$emailSMTPServer = "EXCH01.YOURDOMAIN.COM"
$emailSMTPServer = "mail.nsitautomation.in"
$emailsubject = "Welcome to company new URL"
$emailattach = "C:\NSITAutomation\Scripts\INPUT\NSITAutomation.PDF"


foreach($userincsv in $usersincsv)
{
$sfFirstname = $userincsv.'First Name'
$sfLastname = $userincsv.'Surname'
$sfdisplayname = $userincsv.'Full Name'
$sfcommlanguage = $userincsv.'Com. Language'
$sfemail = $userincsv.'Email'
$emailto = $sfemail
Write "Executing for $sfdisplayname,$sfemail,$sfcommlanguage"
if (!$sfemail ) { $sfemail= "NULL" }

if ($sfemail -eq "NULL")
{
Write "Skiped $sfdisplayname"
}
else
{
if ($sfcommlanguage -eq 'English')
{
Write "$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in English at email $sfemail"
Write "$sfFirstname,$sfLastname,$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in English at email $sfemail" | Out-File -filepath $emailsenttocsv -Append 
# English
$emailsubject = "New URL Launched"
$emailbody = "Dear NS IT Automation Users,</b> <br>" 
$emailbody += '<P>Company had launched new Web Site..</P>'
$emailbody += '<P>Please click this link and have a look to technical documents.<a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></p>'
$emailbody += "<P>Thank you,<br><font color=black>The NS IT Automation team</P></br>"
Send-MailMessage -Attachments $emailattach  -Credential($Credential) -From $emailfrom -To $emailto -Cc $emailcc -SmtpServer $emailSMTPServer -Subject $emailsubject -Body $emailbody -BodyAsHtml -ErrorAction SilentlyContinue
$str

}

if ($sfcommlanguage -eq 'French')
{
Write "$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in French at email $sfemail"
Write "$sfFirstname,$sfLastname,$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in French at email $sfemail" | Out-File -filepath $emailsenttocsv -Append 

# French
$emailsubject = "Nouvelle URL lanc&#xE9;e"

$emailbody = 'Cher utilisateur nsitautomation.in,</b> <br>'
$emailbody += "<P>La soci&#xE9;t&#xE9; a lanc&#xE9; un nouveau site web</P>"
$emailbody += '<P>Veuillez cliquer sur ce lien et consulter les documents techniques.<a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></P>'
$emailbody += "<P>Merci beaucoup,<br><font color=black>L&rsquo;&#233;quipe NS IT Automation</P></br><hr>"

$emailbody += "Dear NS IT Automation Users,</b> <br>" 
$emailbody += '<P>Company had launched new Web Site..</P>'
$emailbody += '<P>Please click this link and have a look to technical documents.<a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></p>'
$emailbody += "<P>Thank you,<br><font color=black>The NS IT Automation team</P></br>"

Send-MailMessage -Attachments $emailattach  -Credential($Credential) -From $emailfrom -To $emailto -Cc $emailcc -SmtpServer $emailSMTPServer -Subject $emailsubject -Body $emailbody -BodyAsHtml -ErrorAction SilentlyContinue
$str

}

if ($sfcommlanguage -eq 'Spanish')
{
Write "$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in Spanish at email $sfemail"
Write "$sfFirstname,$sfLastname,$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in Spanish at email $sfemail" | Out-File -filepath $emailsenttocsv -Append 

# Spanish
$emailsubject = "Nueva URL lanzada"

$emailbody = "Estimados usuarios de NS IT Automation,</b> <br>" 
$emailbody += "<P>La compa&ntilde;&iacute;a había lanzado un nuevo sitio web.</P>"
$emailbody += '<P>Haga clic en este enlace y consulte los documentos técnicos.<a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></p>'
$emailbody += "<P>Muchas gracias,<br><font color=black>El equipo de NS IT Automation</P><hr>"

$emailbody += "Dear NS IT Automation Users,</b> <br></b>" 
$emailbody += '<P>Company had launched new Web Site..</P>'
$emailbody += '<P>Please click this link and have a look to technical documents.<a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></p>'
$emailbody += "<P>Thank you,<br><font color=black>The NS IT Automation team</P></br>"

Send-MailMessage -Attachments $emailattach  -Credential($Credential) -From $emailfrom -To $emailto -Cc $emailcc -SmtpServer $emailSMTPServer  -Subject $emailsubject -Body $emailbody -BodyAsHtml -ErrorAction SilentlyContinue  
$str
}

if (($sfcommlanguage -ne 'English') -and ($sfcommlanguage -ne 'French') -and ($sfcommlanguage -ne 'Spanish'))
{
$sfcommlanguage= "Nill"
Write "$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in English at email $sfemail"
Write "$sfFirstname,$sfLastname,$sfdisplayname,$sfemail,$sfcommlanguage, hence Communication sent in English at email $sfemail" | Out-File -filepath $emailsenttocsv -Append 

# English
$emailsubject = "New URL Launch"
$emailbody = "Dear NS IT Automation Users,</b> <br>" 
$emailbody += '<P>Company had launched new Web Site..</P>'
$emailbody += '<P>Please click this link and have a look to technical documents.  <a href="https://nsitautomation.in/"> https://nsitautomation.in/</a></p>'
$emailbody += "<P>Thank you,<br><font color=black>The NS IT Automation team</P></br>"
Send-MailMessage -Attachments $emailattach  -Credential($Credential) -From $emailfrom -To $emailto -Cc $emailcc -SmtpServer $emailSMTPServer -Subject $emailsubject -Body $emailbody -BodyAsHtml -ErrorAction SilentlyContinue
$str
}

}

}

#Remove-Item –path $outputfile
$str
#}
Write ""
Write "Email Sent to Log available at $emailsenttocsv"
}

Script End

5.2 Input File:

Input file is CSV (comma separated) file contains as shown in Section 4. Each line should contain only one user entry. File name “EXT-User-List.csv”

First NameSurnameFull NameEmailCom. Language
NirajSrivastavaNiraj Srivastavanirajsrivastava1@gmail.comEnglish
MohenjoDaroMohenjo Daronirajsrivastava1@gmail.comFrench
CoolStuffCool Stuffnirajsrivastava1@gmail.comSpanish

5.3       Script Execution:

  • Script will prompt for credentials to authenticate to exchange server, if your system is non domain joined system.
  • For domain joined system comment below line in script and script will not prompt for password

$Credential = $host.ui.PromptForCredential(“Need credentials”, “Please enter your user name and password.”, “”, “NetBiosUserName”)


Onscreen output.

6 Output

6.1 Output File:

Script provide output files

Report Date,28-Nov-2019 6:41:25,Status1,Status2,Status3,Status4
FirstName,LastName,FullName,Email,Language,Message
Niraj,Srivastava,Niraj Srivastava,nirajsrivastava1@gmail.com,English, hence Communication sent in English at email nirajsrivastava1@gmail.com
Mohenjo,Daro,Mohenjo Daro,nirajsrivastava1@gmail.com,French, hence Communication sent in French at email nirajsrivastava1@gmail.com
Cool,Stuff,Cool Stuff,nirajsrivastava1@gmail.com,Spanish, hence Communication sent in Spanish at email nirajsrivastava1@gmail.com

6.2       Email Notification

Script generates email for all the users (sample email attached).

6.3       Email contents

7         End of Document

Facebook Comments

Leave a Reply

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