How to send large batches of emails

There are many occasions when you need to send out a lot of emails in a short amount of time, using a list of email eddresses we can process these in powershell and quickly send a customised email to a large list within seconds

Import recipient addresses

First we need to import our list of email addresses. In my example I have made a small csv with a few email addresses and names. We can use the name and any other values we like later to customise the email to the recipient.


$addresses = import-csv C:\Users\reelfunky\Desktop\email.csv


Create a foreach loop

Next we will create a foreach loop to process each entry within the email list, within the loop we are creating the variables to use in the email text and the email command at the end of the loop


foreach ($address in $addresses){
$thisaddress=$address.email_address
$thisname = $address.name}


Create the message

Now we create the message, we can use variables to customise the email to the user, I am just using the recipient's name in this example but you can use make use of as many variables as you require


$messagebody ="Hi $thisname, I am just testing my bulk email tool, regards, Ricky"


Send the message

Finally, we take all the variables we have processed and put them into the send-mailmessage command. The foreach loop in my tests takes just a few milliseconds to process each cycle. If you do not have an smtp server there are ways to make use of googles or Microsoft etc


send-mailmessage -to $thisaddress -from test@powershellpower.com -Subject "Test Emails" -SmtpServer 'smtp.powershellpower.com' -Body $messagebody


The full script

$addresses = import-csv C:\Users\reelfunky\Desktop\email.csv
foreach ($address in $addresses){
$thisaddress=$address.email_address
$thisname = $address.name}
$messagebody ="Hi $thisname, I am just testing my bulk email tool, regards, Ricky"
send-mailmessage -to $thisaddress -from test@powershellpower.com -Subject "Test Emails" -SmtpServer 'smtp.powershellpower.com' -Body $messagebody


Cut & Paste


Comments
// Collect comments ''
Search