Send Email with images in PowerShell

Last week we had to send 200 mails using a CSV file. I have created a script that reads the information from the CSV file and add it in the mail.  In this blog I stripped the script down to a minimum.

It may look like a large script but it is possible to combine the commands.

This function sends mail that can be included with an image
Use this script to easily send multiple mails to customers using powershell and use an HTML layout.
if you want to add more images you can copy/paste the embed image and assign a new variable and ID
function SendMail
#Mail Variables
$EmailFrom = “”
$EmailSubject = “Email including images in HTML”
$smtpServer = “<SMTP Server>”
$SendTo = “<SendToMail>”
$Image = “C:\scripts\MailScript\TestImage.png”

#Embed Image
$att1 = new-object Net.Mail.Attachment($Image)
$att1.ContentType.MediaType = “image/png”
$att1.ContentId = “Attachment”

#More information on Mailmessage on
$mailmessage = New-Object

#Add attachment to the mail

#Mail body
$body = “<p style=’font-family: Calibri, sans-serif’>
<img src=’cid:Attachment’ /><br />
This is a demonstration mail.

#Mail info
$mailmessage.from = $emailfrom
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $body
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)

#Dispose attachments



Any comments or improvements on this script are welcome.