convert-tohtml -postcontent

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

convert-tohtml -postcontent

Post by dan.potter »

Seems like this should be easy but I'm having trouble here.

I need to attach an image after the grid. It always shows a red x. I can get it to appear by adding it into the body but it appears before the grid.

$emailbody = $dnstoprocess | ConvertTo-Html -head $a -body "DNS Records Processed <br><br>" -PostContent "<br><img src=dnschart.png />" | out-string
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

The source has to have the full path to the image file. It would be best if it were on a web server.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

$post='<br><img src="file:///c:\temp\dnschart.png"/>'

... -PostContent $post | out-file

Attribute values need to be in quotes.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: convert-tohtml -postcontent

Post by dan.potter »

got it like this. I don't know why I wasn't seeing it. Friday scripting logic.

$out = $dnstoprocess | ConvertTo-Html -head $a -body "Dns Records Processed<br><br>" | out-string

$emailbody = "$out<br><img src=dnschart.png />"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

Not a very good fix. You need to quote attribute values or the browser versions will give you headaches. The code assumes the image file is in the HTML folder.

Look at the HTML courses at W3Schools to get accurate background o how HTML works.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: convert-tohtml -postcontent

Post by dan.potter »

I'm embedding a picture attached to the email inline with the body following the table. Seems to work.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

You cannot embed a picture in to an email like that. It will show on your system but will be a red x on any other system. You can use a web server to show the image as long as the server is available to the people who receive your mail.

To embed a picture we must add the picture as an attachment and refer to it by CID.

<img src="cid:image1.jpg">

Add image as an attachment. CID stands for "Content ID".
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: convert-tohtml -postcontent

Post by dan.potter »

I don't know why you say it wouldn't show. This email is going to a distribution list and everyone says they can see it. The script runs on a server and it displays on my workstation.

maybe it has something to do with using the old method of sending email?

$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$msg.IsBodyHTML = $true

gci .\dnschart.png | %{

$attachment = new-object Net.Mail.Attachment($_)
$msg.attachments.add($attachment)

}

$mailer.send($msg)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

As long as the name of the file is correct and specified as a CID the attachment will display. If the users extract the attachment it will display. Without CID:<filename> it won't be visible without extraction. If we use Outlook 2016 then the attachment is temped by default. Other email clients and web based mail will not see the attachment.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: convert-tohtml -postcontent

Post by jvierra »

I just tested. In Outlook 2016 it will display because outlook dynamically extracts pictures from trusted sources. Outlook can display it. I ran the same message to two webmail sites and neither could display the picture.

Here is one solution that allows the mail to support all clients equally: https://gallery.technet.microsoft.com/s ... e-3a920a6d

If you only need to use Outlook then this would not be necessary.
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked