Wednesday, June 25, 2014

Everyday Powershell - Part 24 - Send Email With Inline Image

Ever wanted to include inline images with an email you are sending out from a script?

We came across the requirement during our Exchange 2013 Migration.

This was ridiculously easy to achieve in Powershell 4.
Send-MailMessage -to someuser@someserver.com -From someotheruser@someserver.com -SmtpServer somemailserver -Subject "Test" -BodyAsHtml "TEST <br /><img src="attachement.png" />" -Attachments "C:\Scripts\attachement.png"

This is how we used to do it!
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$smtpServer = "somemailserver"
$att = new-object Net.Mail.Attachment("c:\scripts\attachment.png")
$att.ContentId = "att"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = "Some Text<br />
<img src="cid:att" />"

$msg.From = "someuser@manly.nsw.gov.au"
$msg.To.Add("someotheruser@manly.nsw.gov.au")
$msg.Subject = "Some Subject" + (get-date -format yyyyMMdd)
$msg.Body = $body
$msg.IsBodyHTML = $true
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

UPDATE - The HTML included in the script bugged out... Will fix shortly.

UPDATE 2 - reader sivaramsharma asked if send-mailmessage could handle multiple attachments, if this interests you too head over to Part 31 where we give a useful example.

11 comments:

  1. Hi Ben,
    Your Script works like charm for sending Single PNG files in Body of E-mail, I have several PNG files generated from several loops in single Script, Could you please provide Script for sending Multiple PNG files as in Body of E-mail for same. All PNG files will be generated and stored in c:\Reports\ in my Script.
    Thanks in Advance.

    Thanks & Regards,
    Siva

    ReplyDelete
  2. Hey mate thanks for the question... Attachments parameter accepts multiple values as input. Will post the exact syntax as a new post.

    ReplyDelete
  3. The send mailmessage code above does not work in version powershell 4

    ReplyDelete
  4. I'm sure it was just a typo...

    To fix the -BodyAsHtml just remove the double quotations around the image location and use single quotations instead.

    Thank Ben for your excellent and concise example!

    ReplyDelete
    Replies
    1. Thanks! I was stuck on this and that worked perfectly.

      Delete
  5. How can I add a local image to this email script below....it works perfectly fine but now I'd like to embed an image (off my c: drive) directly to the email.....everything I try is not working. I keep getting errors....


    $smtp = New-Object Net.Mail.SmtpClient("server.123.com")
    $smtp.Send("XYZ@123.com","ABC@123.com","You've been warned",

    "Warning...
    ")

    ReplyDelete
  6. I was looking for a way to send an inline image using Send-MailMessage option with no luck for days until I found your post, thanks mate, is as easy as I thought it should be.

    ReplyDelete
  7. Hi Team,

    I am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi Team,

    I am actually trying to put the image as first and then using the excel output where the image is not coming now.Help me

    ReplyDelete
  10. I was doing some testing and found you can do it just fine using the send-mailmessage service just fine. Attach the files normally using this service and then in the body of the email add an image with the source of "cid:" and the filename of the file you attached. i.e. file attached is "c:\temp\test.png" would be "cid:test.png".

    ReplyDelete