PowerShell to send email with content of a file

https://technet.microsoft.com/en-us/library/hh849925.aspx

I'm trying to use PowerShell to send an email with the body/content of the email
from a text file.  Pls give specific example, say the text filename is  mailbody.txt

In Unix, I would use pipe or redirection  (<, >, | ) to achieve this
sunhuxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunhuxAuthor Commented:
http://exchangeserverpro.com/powershell-email-message-body/

Something like the above but would like to assign emailbody the content
of a text or html file
Bill PrewIT / Software Engineering ConsultantCommented:
You can read the text file in with the body content first, into a variable, then just use the variable as the -body when you send the email.

$body = get-content yourfile.txt | out-string

~bp
kulboyCommented:
The example you give does it...

It reads the logfile line by line, and adds the CRLF funtion...  (wich you better do like Bill Prew states with | out-string)

#
#.SYNOPSIS
#Sends daily dropped ping report
#
#.EXAMPLE
#.\Send-DroppedPingReport.ps1
#

$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = "administrator@exchangeserverpro.net"
$messageSubject = "Dropped ping report"

[string]$messagebody = get-content mailbody.txt | out-string

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

sunhuxAuthor Commented:
Can I send both a html (as in Outlook, we can have html body for the email to get formatting)
as well as text file?

I was looking at another syntax:
$messageBody = [IO.File]::ReadAllText("full_path\test.txt")

Send-Email.ps1
=============
#.\Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"
#
#
param(
[string]$to,
[string]$subject,
[string]$body
)

$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = $to
$messageSubject = $subject
$messageBody = [IO.File]::ReadAllText("full_path\test.txt")

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
sunhuxAuthor Commented:
I meant:
Can I send either in html or plain text format using the given PowerShell commands?
Bill PrewIT / Software Engineering ConsultantCommented:
Not sure the exact question.

You do not want to send an attachment, right, we are still talking about putting content of a file into the body?

Is the file you are getting the content from a plain text file, or is that data already HTML data?

Are you wanting to sometimes send the text body email as a "plain text" email, and other times send it as "HTML" email?

~bp
sunhuxAuthor Commented:
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
Doesn't the line above  put content of a file into the body?  
Right, I'm not sending a file as attachment.

> sometimes send the text body email as a "plain text" email, and other times send it as "HTML" email?
Yes, the above is what I wanted
Bill PrewIT / Software Engineering ConsultantCommented:
When using the Send-MailMessage cmdlet, just add the "-BodyAsHtml" flag to have the body sent as HTML.

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.