Send Email using PowerShell from CSV data file

Just finished phase 1 of a project which required sending out an automated survey email to a dynamic list of people. The email message is the same in all of these except for an ID number embedded in a link within the message. Currently the list of emails along with the ID is automatically dumped out of the database nightly into a CSV file so I didn’t need to do anything there just yet.

Here’s the PowerShell script that I pieced together from various sites to get this to work

$EmailFileLocation = “d:\scripts\email\dailyemaillist.csv”
function SendSurvey($SendTo,$EmailID)
{
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “mailserver.domain.com”
$FromAddress = new-object System.Net.Mail.MailAddress(“email@domain.com”, “Your Name”)
$mailmessage.sender = $FromAddress
$mailmessage.from = $FromAddress
$mailmessage.To.add($SendTo)
$mailmessage.Subject = “Your Message Subject Here”
$mailmessage.IsBodyHtml = 1

# The line below will pull the message body from the specified file
# This is currently commented out to have the message body created within this script but can be changed easily.
# $mailmessage.Body = Get-Content .\emailbody.htm
$mailmessage.Body = “<html><body>
<p>This is an example message.</p>
<p>If this had been an actual message there would be something worth reading here.</p>
<p>bla bla bla…here’s the link example:
$EmailID </p>
<p><br>End of message</p>
</body></html>”
$Credentials = new-object System.Net.networkCredential
$Credentials.UserName = email@domain.com
$Credentials.Password = “Password”
$SMTPClient.Credentials = $Credentials
$SMTPClient.Port = 25
$smtpclient.Send($mailmessage)
}

$Guests = import-csv $EmailFileLocation -erroraction stop
foreach ($guest in $guests) {
SendSurvey $guest.email $guest.number
}

Hope this helps. One other note, if you’re setting this script to run automatically, you will either need to sign the script or change the permissions in PowerShell to allow it to run. You can use the following command to make the change:

set-executionpolicy RemoteSigned

Last but not least, here’s a few of the sites I found info on to get this working:

Advertisements
Explore posts in the same categories: PowerShell, Tech

One Comment on “Send Email using PowerShell from CSV data file”

  1. Jeffrey Snover Says:

    You are going like CTP 3 (hopefully this week). When you get it, check out the new Send-mailMessage cmdlet:

    Sends an e-mail message.
    Send-MailMessage
    (0)-To
    (1)-Subject | -sub
    -From
    (2)[-Body ]
    (3)[-SmtpServer | -ComputerName ]
    [-Attachments | -PsPath (ByValue)]
    [-Bcc ]
    [-BodyAsHtml | -BAH ]
    [-Cc ]
    [-Credential ]
    [-DeliveryNotificationOption | -DNO ]
    [-Encoding | -BE ]
    [-Priority ]
    [-UseSsl ]

    You can set an variable $PSEmailServer and that will be used if you don’t specify one.

    I would greatly appreciate it if you bashed the heck out of that command and let me if it is something you would use or not and if not, why not.

    Thanks!
    Jeffrey Snover [MSFT]
    Windows Management Partner Architect
    Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: