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?
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
0
Bill PrewCommented:
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
0
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

0

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
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

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)
0
sunhuxAuthor Commented:
I meant:
Can I send either in html or plain text format using the given PowerShell commands?
0
Bill PrewCommented:
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
0
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
0
Bill PrewCommented:
When using the Send-MailMessage cmdlet, just add the "-BodyAsHtml" flag to have the body sent as HTML.

~bp
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.