My script works fine by return the data I need and emailing it to me, but the html format is not working. When I receive the email, it shows all the styling I coded at the top of the email; here’s how it look"
< style>BODY{font-family: Arial; font-size: 10pt;}TABLE{border: 1px solid black; border-collapse: collapse;}TH{border: 1px solid black; background: #dddddd; padding: 5px; }TD{border: 1px solid black; padding: 5px; }< /style>
Email Size
Here is the script:
<#
#.SYNOPSIS
#Send SMTP email via the Hub Transport server
#
#.Example
#.\Send-Email.ps1 -To "administrator@mdd.com" -Subject "Test email" -Body "This is a test"
#
#>
$smtpServer = "domain.com"
$smtpFrom = "administrator@domain.com"
$smtpTo = "kmyles@domain.com"
$messageSubject = "Test"
$message = New-Object Net.Mail.MailMessage $smtpFrom, $smtpTo
$message.Subject = $messageSubject
$message.IsBodyHtml = $true
$searchBase='dc=users,dc=domain,dc=com'
$style = "< style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "< /style>"
$message.body = get-aduser -filter * -SearchBase $searchBase|
%{
if($mbstats=Get-MailboxStatistics $_.Name -ea 0){
$size=$mbstats.TotalItemSize.Value.ToMB()
}else{
$size=0
}
New-Object psobject -Property @{
Email=$_.UserPrincipalName
Size=$size
}
} | sort size -Descending | where size -gt 2048 | select email,size | ConvertTo-HTML -Head $style
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
Your help again will be greatly appreciated.
7 Spice ups
DoctorDNS
(DoctorDNS)
March 14, 2014, 4:50pm
2
Has this ever worked?
First, if it were me, I’d take a look at the message body before you send it. What does it look like? Postint the full HTML is more helpful than just posting the result.
Secondly, the -HEAD parameter in effect populates the … field so I am kind of not too surprised that you see what you see.
I’d try this:
1, Remove the -Head from the ConvertTo-Html statement
Just after line 39 (before the send), I’d do
$message=$style+$message
I edited like you suggested and now its giving an error:
"Cannot find an overload for “Send” and the argument “1”
The script worked before i made the changes except for the html formatting.
anthony
(Twon of An)
March 14, 2014, 5:17pm
4
I’d recommend using the Send-MailMessage cmdlet, it has the -BodyAsHTML flag and its worked great for sending HTML for me.
3 Spice ups
How would I rewrite that. It took me 2 weeks to get this to work. I have no knowledge in scripting and coding. I’m teaching myself powershell
chrisseiter
(Chris Seiter (LBFF))
March 14, 2014, 5:25pm
6
For the version you have, use
$smtp.IsBodyHtml = $True
$smtp.Send($Message)
http://exchangeserverpro.com/powershell-send-html-email/
2 Spice ups
Yah, use Send-MailMessage instead, it’s SO much easier to use!
Get-Help Send-MailMessage -ShowWindow
2 Spice ups
this the error I got when trying to use Send-MailMessage
Send-MailMessage : The remote name could not be resolved: '< style>BODY{font-family: Arial; font-size: 10pt;}TABLE{border: 1px solid
black; border-collapse: collapse;}TH{border: 1px solid black; background: #dddddd ; padding: 5px; }TD{border: 1px solid black; padding:
5px; }< /style>’
At C:\Users\kmyles\Desktop\Send-Email1.ps1:43 char:1
Send-MailMessage -from $smtpFrom -To $smtpTo -Subject $messageSubject -Body $mes …
CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Can you post your whole Send-MailMessage line from your script?
Send-MailMessage $PSEmailServer -from $smtpFrom -To $smtpTo -Subject $messageSubject -Body $message -BodyAsHtml
Looks like you’re really close Kerry, just need to add the parameter name to your line:
Send-MailMessage -SMTPServer $PSEmailServer -from $smtpFrom -To $smtpTo -Subject $messageSubject -Body $message -BodyAsHtml
When I added the parameter name, I gave the error I posted above about cannot resolve…
Where do you assign the $PSEmailServer variable? What’s the value?
Are you sure? The error message suggests your $Style variable is going in for SMTPServer:
Send-MailMessage : The remote name could not be resolved: '< style>BODY{font-family: Arial; font-size: 10pt;}TABLE{border: 1px solid
I would suggest downloading and installing PowerGUI (from PowerGUI.org ). This is a free ISE that has a variable list that you can watch as you step through the script (F11). Just hit F11 and go line by line watching your variables on the right.
I use Send-MailMessage all the time, especially with HTML output so there’s no question it can work. Maybe post your whole code again. Not really sure what else to do!
<#
#.SYNOPSIS
#Send SMTP email via the Hub Transport server
#.Example
#.\Send-Email.ps1 -To " administrator@domain.com " -Subject “Test email” -Body “This is a test”
#>
$smtpServer = “smtp.server.com ”
$smtpFrom = " administrator@domain.com "
$smtpTo = " kmyles@domain.com "
$messageSubject = “Test”
$message = New-Object Net.Mail.MailMessage $smtpFrom, $smtpTo
$message.Subject = $messageSubject
#$message.IsBodyHtml = $true
$searchBase=‘dc=domian,dc=com’
$style = “< style>BODY{font-family: Arial; font-size: 10pt;}”
$style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
$style = $style + “TH{border: 1px solid black; background: #dddddd ; padding: 5px; }”
$style = $style + “TD{border: 1px solid black; padding: 5px; }”
$style = $style + “< /style>”
$message.body = get-aduser -filter * -SearchBase $searchBase|
%{
if($mbstats=Get-MailboxStatistics $.Name -ea 0){
$size=$mbstats.TotalItemSize.Value.ToMB()
}else{
$size=0
}
New-Object psobject -Property @{
Email=$ .UserPrincipalName
Size=$size
}
} | sort size -Descending | where size -gt 2048 | select email,size | ConvertTo-html
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$PSEmailServer = “stl11mx.stl2.mddcpa.com ”
Send-MailMessage $PSEmailServer -from $smtpFrom -To $smtpTo -Subject $messageSubject -Body $message -BodyAsHtml $style
#$smtp.Send($message)
martin9700
(Martin9700)
March 16, 2014, 10:01am
18
So, first things first, you need to start using the code button so your code comes out properly coded!
Now, for your Send-MailMessage you have about 4 problems as I see it.
You haven’t specified the proper parameters for $PSEmailServer. You have to add -SMTPServer $PSEmailServer in there so the cmdlet knows what to do with that variable.
The Body parameter is set ok, but you’re feeding it the $message variable, which is a .NET object and needs to be a string. You need to remark out the line that says $Message = New-Object …the rest of it… Then you need to change $message.body = Get-ADUser …the rest of it… to just say $message = …
You’re adding $Style to the end of the Send-MailMessage command, but that doesn’t have anything in it. That needs to be in the ConvertTo-HTML cmdlet above, with the -Head parameter: ConvertTo-HTML -Head $Style
After the ConvertTo-HTML cmdlet, the variable is STILL not ready for transmission. You then need to pipe it into Out-String so it will be a string format, which is what Send-MailMessage wants.
Have you verified that your Get-ADUser loop even works? It looks like it should but I always like to make sure before I start sending emails.
It is not giving me the option to use the code button; but I’m going to repost again anyway. Tell me if this is what you were meaning with the edits you suggested: