'How to send an email with embedded images using PowerShell
I want to send an automated email report with some embedded images that cannot be accessed externally from our network.
Google has lead me towards attaching the images and referencing them using CID Embedding in the body of the email using HTML.
I cannot find any guidance on how to do this, how do I set the content ID on an attached image so that it can be displayed in the HTML body?
Something along the lines of:
$html = "<ul><li><img src='cid:att'/></li><li>next img</li><li>another img</li></ul>"
Send-MailMessage -SmtpServer "stuff.co.uk" -to "person.co.uk" -from "mailbox.co.uk" -Subject "Report" -body $html -BodyAsHtml -Attachments \\some image from a server location.jpg
Solution 1:[1]
I got there in the end by adapting some code I found on reddit, it could definitely be improved but worked perfectly for my situation and because it's not the Base64 method, it shows the images on Outlook and Apple devices etc.
Hopefully this will help someone else in the future.
### Create a new mailmessage ###
$msg = new-object net.mail.mailmessage
$msg.From = 'email@address'
$msg.To.Add('email1, email2')
$msg.CC.Add('ccemail');
$msg.Subject = 'Daily Performance Report' + $(get-date -format dd/MM/yyyy)
$msg.IsBodyHtml = $true
#### Attach images with individual Content IDs (ContentId is used to refer to attachement inline in the email HTML body) ###
$attachment = New-Object System.Net.Mail.Attachment -ArgumentList "attachment file location e.g. C:\Users\username\documents\image.jpg"
$attachment.ContentDisposition.Inline = $true
$attachment.ContentId = 'image.jpg' #can be anything as long as it matches the CID reference in the HTML
$attachment.ContentDisposition.DispositionType = "Inline"
$attachment.ContentType.MediaType = "image/jpg"
$msg.Attachments.Add($attachment)
#repeat the above to add another attachment.
### Create the html body of the email ###
$msg.Body = "<p>Please see todays performance dashboards below. <p/>
<h2>Compliance Scores <h2/>
<h4 style='font-weight:normal' >Logon compliance scores for each environment<h4/>
<img src='cid:image.jpg' />"
### Create connection to smtp server and send the mail ###
$smtp = New-Object Net.Mail.SmtpClient('yoursmtp.server.com')
$smtp.Send($msg)
#$attachment.Dispose()
#$msg.Dispose()
Solution 2:[2]
Base64 might work for you. Just tried this code working with my client
function ImageToBase64($FilePath) {
$type = (dir $FilePath).extension -replace "\.",""
return $("data:image/$type;base64," + $([Convert]::ToBase64String([IO.File]::ReadAllBytes($FilePath))))
}
$html = "<ul><li><img src=`"" + $(ImageToBase64("c:\temp\image.jpg")) + "`"/></li><li><img src=`"" + $(ImageToBase64("c:\temp\nextimage.png")) + "`"/></li><li><img src=`"" + $(ImageToBase64("c:\temp\anotherimg.jpeg")) + "`"/></li></ul>"
Send-MailMessage -SmtpServer "stuff.co.uk" -to "person.co.uk" -from "mailbox.co.uk" -Subject "Report" -body $html -BodyAsHtml -Attachments \\some image from a server location.jpg
--Edit-- ATTENTION: I forgot to mention that not all clients will display base64 embedded images - thanks @Nathan
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PWNFUM |
| Solution 2 |
