I want to generate embedded Images for an email in PowerShell. But no luck. I am using <p><img src='file:///C:\temp\Picture1.png' /></p>
# Email Subject Set Here
$subject="Your password will expire soon"
# Email Body Set Here, Note You can use HTML, including İmages.
$body ="
<P style='font-size: 12pt; font-family: Arial Narrow;'>Dear FIRST_NAME,</P>
<P style='font-size: 12pt; font-family: Arial Narrow;'>The current password of the account 'PRINCIPAL_ACCOUNT_NAME' in the 'FQDN_DOMAIN' AD domain will expire at <B>PWD_EXPIRY_DATE</B>. Please change your password as soon as possible!</P>
<P style='font-size: 12pt; font-family: Arial Narrow;'><U>The new password must meet the following requirements:</U></P>
<UL>
<LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Min. nr. of characters in a PWD = 'PWD_MIN_LENGTH characters' <BR><I>(The minimum number of characters to be used in a password)</I></P></LI>
<LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Min. PWD age = 'PWD_MIN_AGE days' <BR><I>(Minimum number of days the new password must be used before it can be changed again)</I></P></LI>
<LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Max. PWD age = 'PWD_MAX_AGE days' <BR><I>(Maximum number of days the new password can be used before it must be changed again)</I></P></LI>
<LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Nr. of previous PWDs in history = 'PWD_HISTORY' <BR><I>(Number of new and unique passwords required before an old password can be reused again)</I></P></LI>
<LI><P style='font-size: 12pt; font-family: Arial Narrow;'>Password complexity enabled? = 'PWD_COMPLEX' <BR><I>(The new password must meet complexity requirements ONLY when configured to 'TRUE')</I></P></LI>
</UL>
<p><img src='file:///C:\temp\Picture1.png' /></p>
"
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding
thanks,
CodePudding user response:
Unfortunately directly referencing a file on your C:\ won't work (too many security issues with that) and Send-MailMessage
won't pick it up. Instead you have 2 options, and, as confusing and trivial as it sounds, it largely depends on your target email program.
CID Image Embedding
The "old school" method, is to attach the image to the email and use Content-ID (CID) references to link the two. If you are using the desktop Outlook application, this is the preferred method. The downside is that people don't always trust emails with attachments, and there are spam/filtering implications by sending emails with attachments, so sometimes this isn't always preferred. Also some web based email providers have trouble displaying the images properly.
$body = "<p>.... <img src='cid:Picture1.png' alt='Embedded Image'> ....</p>"
$file = 'C:\temp\Picture1.png'
Send-Mailmessage -Attachments $File -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding
Inline Embedding
Inline embedding embeds the data as base64
encoded data (basically changes the binary image data to plain text) and the email client/browser decodes it. This means no attachments, and usually more delivery success. Because base64 encoding is used in the web, all web based email clients have no problems displaying them. The downside is that the desktop version of Outlook 2010 will display "images blocked by default".
$file = Get-Content 'C:\temp\Picture1.png'
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($file)
$EncodedText =[Convert]::ToBase64String($Bytes)
$body = "<p>.... <img src='data:image/png;base64,$EncodedText' alt='Embedded Image'> ....</p>"
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $encoding