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.

<#
.SYNOPSIS
This function sends mail that can be included with an image
.DESCRIPTION
Use this script to easily send multiple mails to customers using powershell and use an HTML layout.
.NOTES
if you want to add more images you can copy/paste the embed image and assign a new variable and ID
.LINK
http://www.maartenpeeters.nl
#>
function SendMail
{
#Mail Variables
$EmailFrom = “NoReply@maartenpeeters.nl”
$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 http://technet.microsoft.com/en-us/library/dd347693.aspx
$mailmessage = New-Object system.net.mail.mailmessage

#Add attachment to the mail
$mailmessage.Attachments.Add($att1)

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

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

#Dispose attachments
$att1.dispose()

}
SendMail

 

Any comments or improvements on this script are welcome.

 

Result:

image

13 thoughts on “Send Email with images in PowerShell

  1. Hello tanks for your post,
    I’m trynig to send an logo on an automatic email using powershell, however I tryed a lot of posts examples and the only result I got is the email with the correct attachement but a brocken link icon instead of the image on the email’s body.

    So I quit to use my posts compilation and adaptaition i copied your code just changing the image path. And the same thing happened.
    So I added 2 lines which referes to ContentDisposition.
    As you can see on the code bellow. Can you find out wy can’t the image appear in body?Is there any step that is obvious to everyone and not for me because its the 1st time I use powershell?

    I hope you can help,
    Tanks,
    Filipe

    #Mail Variables
    $EmailFrom = “fapostolo.pt@gmail.com”
    $EmailSubject = “TEST”
    $smtpServer =“smtp.gmail.com”
    $SendTo = “fapostolo.pt@gmail.com”
    $Image = “C:\birtruntime\ReportEngine\logo.png”

    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“fapostolo.pt”, “pass”);

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

    -Added-
    $att1.ContentDisposition.Inline = $true
    $att1.ContentDisposition.DispositionType = “Inline”
    —-

    #More information on Mailmessage on http://technet.microsoft.com/en-us/library/dd347693.aspx
    $mailmessage = New-Object system.net.mail.mailmessage

    #Add attachment to the mail
    $mailmessage.Attachments.Add($att1)

    #Mail body
    $body = “

    This is a demonstration mail.

    #Mail info
    $mailmessage.from = $emailfrom
    $mailmessage.To.add($sendto)
    $mailmessage.Subject = $emailsubject
    $mailmessage.Body = $body
    $mailmessage.IsBodyHTML = $true

    $SMTPClient.Send($mailmessage)

    #Dispose attachments
    $att1.dispose()

    • Hello Filipe,

      You have to specify the attachment ID in the $body. This will place the image in the body.

      Change:
      #Mail body
      $body = “

      This is a demonstration mail.

      To:
      #Mail body
      $body = “img src=’cid:Attachment’ / (place < at the front of img and > at the end of /)
      This is a demonstration mail.”

      Please let me know if you see the image in the mail after changing the above.

      Kind regards,
      Maarten

      • this suggestion sill not work, its amazing I search all the internet for a sample how to send email with embedded picture in powershell and all the examples not work.
        please test your example and share all syntax.

        Thank you in advanced,
        Ronen.

  2. Pingback: It’s The Little Things In Life | Illusionary Software

  3. Hi,

    I’m having same issue I only get the broken link icon in my mails.

    I tried JPG and PNG no luck.

    Did anyone figure this one out?

  4. $imgfile = “C:\Users\Rammy\Desktop\Pic.png”

    $SMTPServer = “smtp.gmail.com”
    $SMTPPort = “587”
    $Username = “motheramesh486@gmail.com”
    $Password = “xxxxxxxx”

    $to = “motheramesh486@gmail.com”
    $cc = “motheramesh486@gmail.com”
    $subject = “Email Subject”
    $body = @”

    “@

    $attachment = New-Object System.Net.Mail.Attachment($imgfile)

    $attachment.ContentDisposition.Inline = $True
    $attachment.ContentDisposition.DispositionType = “Inline”
    $attachment.ContentType.MediaType = “image/png”
    $attachment.ContentId = ‘image1.png’

    $message = New-Object System.Net.Mail.MailMessage
    $message.subject = $subject
    $message.IsBodyHtml = $True

    $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”

    $attachment.Dispose();
    $message.Dispose();

    I am able to send the mail using above script but it compressed pic length.

    same case like here:
    http://stackoverflow.com/questions/17534364/embedded-html-powershell-email-compressing-pic-length

    Thank you in advanced,
    Ramesh.

    • Hi Ramesh,

      I understand the problem. This happens because the imagesize is 3300 x 2451 which is large for a mail.
      It is perhaps possible to style the image tag to the image pixel size so it has a fixed width and height.
      Or to set the width and height to something which is readable in a mail.

      Let me know if it works by setting a max-width and height in the tag.

      Kind regards,
      Maarten

Leave a Reply

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