The official website of Bipul Raman

How to send bulk email using PowerShell Script

No comments

Several times we need to email to numerous people and email content varies from person to person. PowerShell script can be an easier solution for you. Below is the sample PowerShell Script to send bulk emails. It is fetching data from CSV file (represented in above screenshot). This script will send individual email to every single person listed in CSV. A basic knowledge on PowerShell required to modify the code as per your requirement.

You can find this code at my GitHub Repository also.


<#
Script for bulk emailing by getting data from CSV
Author: Bipul Raman
#>

$MailingList = Import-Csv E:\MailingList.csv

#SMTP Server and port may differ for different email service provider
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"

#Your email id and password
$Username = "[email protected]"
$Password = "Password"

#Iterating data from CSV mailing list and sending email to each person
foreach ( $person in $MailingList)
{
    $iName = $person.Name
    $iEmail =  $person.Email    
    $iAddress = $person.Address

    $to = $person.Email
    $cc = "[email protected]"
    $subject = "Email Subject"
    $body = @" 

Hi $iName,

Your address is  : $iAddress

Regards,
Your Name  
                            
"@    
$attachment = "C:\test.txt"

    $message = New-Object System.Net.Mail.MailMessage
    $message.subject = $subject
    $message.body = $body
    $message.to.add($to)
    $message.cc.add($cc)
    $message.from = $username
    $message.attachments.add($attachment)

    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    $smtp.send($message)
    Write-Host Mail Sent to $iName
}

No comments :

Post a Comment