How to send a message with Send-MailMessage cmdLet powershell exchange which uses a html file as a parameter

Before the mailboxes migration E2003 toward E2010 SP2, I have to send a mail which body already exists as a html file.

Here the powershell I currently use :

import-csv d:\path\TestLot.csv  | foreach {Send-MailMessage -To $_.PrimarySmtpAddress -From adminscc@mydomain.com -Subject "Migration " -body "Your mailbox Migration will start in 2 days." -SmtpServer servXXX}

And it is allright !

But, I was asked to use an existing html file instead of my poor : -body "Your mailbox Migration will start in 2 days." 

Does anybody know the way to do this ?

Thank you for your help

January 16th, 2013 11:42am

$body = Get-Content c:\MyFileBody.html

Send-MailMessage -To $_.PrimarySmtpAddress -From adminscc@mydomain.com -Subject "Migration " -body $body -SmtpServer servXXX -BodyAsHtml
  • Marked as answer by PATMOY Friday, January 18, 2013 9:21 AM
Free Windows Admin Tool Kit Click here and download it now
January 16th, 2013 12:27pm

Hi,

Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.

If you have any feedback on our support, please click here .

January 18th, 2013 7:29am

Hi,

I have the html code in a variable so i can personalize the mails:

			$htmlcode = '<html><body style="background:#FFFFFF"><head><title>Activaion email</title></head>'
			$htmlcode += '<style type="text/css">'
			$htmlcode += '#HLine { font-size:20px; font-family:Verdana; font-weight: bold; }'
			$htmlcode += '#txt { font-size:12px; font-family:Verdana;}'
			$htmlcode += '</style>'
			$htmlcode += '<img src="/img/HBPMAN0aGcc.gif" width="52" height="52" />&nbsp;&nbsp;&nbsp;<br />'
			$htmlcode += '<h1 id="HLine">Your BlackBerry account has been created ...</h1>'
			$htmlcode += '<div id="txt"><strong>... Your User Data:</strong><br />'
			$htmlcode += '&nbsp;&nbsp;&nbsp;<br /><table border="0" id="txt"><tr><td>&nbsp;&nbsp;<strong>Username:</strong></td><td>'+$UsrData.Email+'</td></tr>'
			$htmlcode += '<tr><td>&nbsp;&nbsp;<strong>Activation Password:</strong></td><td>'+$UsrData.pass+'</td></tr></table><br />'
			$htmlcode += '<strong>The activation credentials are valid for just 5 days after receipt of this email!</strong><br /><br/ >'
			$htmlcode += '<strong>... Next Steps: </strong><br /><br />'
			$htmlcode += '<a href= "/bb/doc/EN.pdf">- Activate your Device</a>&nbsp;<br />'
			$htmlcode += '&nbsp;&nbsp;To be able to use your new BlackBerry you need to complete this steps.<br /><br />'
			$htmlcode += '<a href= "/bb/doc/Tips.pdf">- Tips and Tricks for your Device</a><br />'
			$htmlcode += '&nbsp;&nbsp;Tips and Tricks to make your Device experience a success.<br /><br />'
			$htmlcode += '<strong>... in case you require technical support:</strong><br /><br />'
			$htmlcode += 'If you experiance problems with your Device, please contact your local IT-Support.<br /><br />'
			$htmlcode += 'Thank You.'
			$htmlcode += '<br/><div></body></html>'
			Send-MailMessage -to $UsrData.Email -From $from -Body $htmlcode -SmtpServer $smtp -Subject $subject -BodyAsHtml

If you want to make sure the mail is displayed as a HTML Mail add to the send-mailmessage the option

-BodyAsHtml

Regards
Dan
  • Edited by Daniel.Sommer Friday, January 18, 2013 7:55 AM
  • Marked as answer by PATMOY Friday, January 18, 2013 8:45 AM
Free Windows Admin Tool Kit Click here and download it now
January 18th, 2013 7:53am

Hye,

With Daniel.Sommer reply , that 's the point.

That was my search and answered my question.

Thanks for your help, guys !

PAtmoy

January 18th, 2013 12:18pm

Hye,

With Kazun reply , that 's the point.

That was my search and answered my question.

Thanks for your help, guys !

Free Windows Admin Tool Kit Click here and download it now
January 18th, 2013 12:19pm

$body = Get-Content c:\MyFileBody.html

Send-MailMessage -To $_.PrimarySmtpAddress -From adminscc@mydomain.com -Subject "Migration " -body $body -SmtpServer servXXX -BodyAsHtml

Hi, I am having a similar issue trying to get the html from an attachment to output into the body of an email.

Here is how my script is defined.

# Send Mail Settings
$Recipients = @("ExchangeReporting@contoso.com")
$Sender = "ExchangeReports<NoReply@contoso.com>"
$Subject = "Exchange Hardware Report for $((get-date).ToShortDateString())"
# The body is populated with the content of the HTML file at the end of the script.  Any text to include prior to the HTML table
# Should be added here. 
#$Body = "This script was executed from " + (Split-Path -parent $MyInvocation.MyCommand.Definition) + " on computer " + $env:computername + "."
$body = Get-Content D:\Scripts\CAS_Hardware.htm
$SMTPServer = "exchange1.contoso.com"

# Send the report as an attachment
Send-MailMessage -To $Recipients -From $Sender -body $body -Subject $Subject -SmtpServer $SMTPServer -Attachments D:\Scripts\Cas_Hardware.htm -BodyAsHtml

I get the email with the attachment, but no body in the email from the htm file.

Any advice?

Thanks Bobby

April 5th, 2015 7:50pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics