Send-MailMessage - Message Body content formatting


Hi PoshFam - I’m trying to automate a process in which we pull multiple csv files reflecting data usage for our mobile community. I have portion to obtain the data and am having an issue with representing the csv file locations in the email correspondence notifying of script completion. (Pls. see next post for function)

All is well and the script runs however the output is not that of what I was envisioning.

Results would be as shown:


When the recipient receives the email mail body the structure is as follows:

  • Email shows all csv's attached = Expected
  • HTML heading appears in the boy = Expected
  • Under "File attachment summary the csv locations are truncated and not showing up as a (one after the other) instead just one continuous string.
When getting the URL from $final_list is ran on the console the URLs of the files show up in list format.




I am not sure as to the formatting that I need in order for the email to show the proper formatting as it displays on the PS console.

Any guidance/assistance would be much appreciated.




Not sure why it didn’t copy the entire function before:

function Send-Results {


    Write-Log -msg "- Preparing Mail..."
    # Obtain csv listing to include newly exported data
    $final_list = (Get-ChildItem -Path $home_root -Include *.csv -Recurse).FullName
    $attachments = $final_list | Format-List -GroupBy Fullname | Out-String

    # Mail content
    $mailFormat = @"

Zero Data Usage Report export completed. (See attachments for reference)

File attachment summary:

<p>$attachments</p> <p>$</p> <p>&nbsp;</p> <p>Script Info:</p> <p>ScriptName = Get-ZeroDataUsage.ps1</p> <p>Script ran = $(get-date)</p> <p>Script logs = $($log_loc)&nbsp;&nbsp;</p> <p>&nbsp;</p> "@ # Save exported files as HTML #ConvertTo-Html -Head $mailFormat -PreContent "<h>***Summary***</h>" -PostContent $zeroDataHTML > "$fileSave.htm" ConvertTo-Html -Head $mailFormat > "$htm_file" # Save message body $mailBody = Get-Content "$htm_file" # Create params $mail_params = @{ SmtpServer = ''; To = ""; From = ""; Subject = "REPORT:`"Zero Data Usage`" extract completed $(Get-date -Format g) PST"; Body = "$mailBody"; BodyAsHtml = $true; Attachments = $final_list; #Attachments = "$zeroDataFile" + "$files"; } try { Write-Log "- Sending email..." Send-MailMessage @mail_params } catch { Write-Log -msg "- ERROR: $($Error.Exception.Message)" exit 1 } }

Your script was not displaying correctly due to the backticks being used to escape the quotation marks on line 37.

Use the following code instead of Param. It is easy in this way.

Send-MailMessage -From “” -To “” -Subject “” -Body “” -Attachments


@grokkit - Sorry for neglecting the backticks

@dhrubbharali20 - the param method works as I am able to send the email and with the attachments, however the issue is the presentation of the file URLs in the mail body.

Instead of having the URLs listed right after one another, they are being truncated and appearing to be on one line versus a row of lines.

Appreciate the feedback nonetheless.

I would remove the format-list, those are specifically for the display at the console.

@Doug, Appreciate the feed back. I have removed and still the same mail content output.

Looking at approaching via another method.

Thanks all for the feedback.

I have gone ahead and went w/the HTML w/CSS mail content approach.

Consider this thread archived.