Link to home
Start Free TrialLog in
Avatar of sunhux
sunhux

asked on

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
Avatar of sunhux
sunhux

ASKER

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
Avatar of Bill Prew
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
ASKER CERTIFIED SOLUTION
Avatar of kulboy
kulboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

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)
Avatar of sunhux

ASKER

I meant:
Can I send either in html or plain text format using the given PowerShell commands?
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
Avatar of sunhux

ASKER

$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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial