Modfiy scriopt to embed txt into body of email

100questions
100questions used Ask the Experts™
on
I would like to modify the following script so that it will embed the contents of the source file in the body of the email:

The source file is at:

C:\Users\User\Desktop\File\file.txt


[code][/###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  'email1@email.com'
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'DistList..txt')
            SmtpServer =      'smtp.smtp.com'
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
      }
      Send-MailMessage @splat
###PROGRAM END###code]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Since you want to send this as html, you need to tell more, especially if you want to add to $MessageBody, and if so, what are its contents?

Author

Commented:
The message body is only test which resembles something like this:

ORDERS:

Number: 12345

Item 1: ABC: 789456153212, Desc: adkl;fjakl;djfklajkl;j;afss, Qty: 500, Price: $1.00
Item 2: ABC: 456456545466, Desc: jakldfjakl;jdfl;kajflkjdl;s, Qty: 400, Price: $2.00
Item 3: ABC: 125212313123, Desc: akljdfkl;ajdflkjakl;djsf;d, Qty: 300, Price: $3.00
Item 4: ABC: 456465456466, Desc: aabblblbllllalalllalalllllla, Qty: 200, Price: $4.00
Item 5: UPC: 123456545564, Desc: uiuadsfjal;fdjal;kjdfl;akj, Qty: 100, Price: $5.00
Total: $0000.00
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Since there's nothing requiring html in there, do you really need BodyAsHtml?
###PROGRAM START###
# Invokes the Send-MailMessage function to send notification email
$splat = @{
	From =			'email1@email.com'
	To =			Get-Content -Path (Join-Path $PSScriptRoot 'DistList..txt')
	SmtpServer =	'smtp.smtp.com'
	Subject =		$subject
	body =			$MessageBody + "`r`n" + ([IO.File]::ReadAllText('C:\Users\User\Desktop\File\file.txt'))
	BodyAsHtml =	$false
}
Send-MailMessage @splat
###PROGRAM END###

Open in new window

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Thanks oBda, I realliy don't need BodyAsHtml..  let me try the script, many thanks,

Author

Commented:
oBdA, thanks again, just ran the script, however it does not respect the different lines shown in the data I provided..
In the body, it should reflect a new line, when the new line appears in the raw data.
Any changes?

Author

Commented:
It seems like Outlook was removing extra line breaks...…
Not sure how to force this in the script...…
Perhaps it's not possible..

Author

Commented:
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial