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
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
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
$body = get-content yourfile.txt | out-string
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("fu ll_path\te st.txt")
Send-Email.ps1
=============
#.\Send-Email.ps1 -To "administrator@exchangeser verpro.net " -Subject "Test email" -Body "This is a test"
#
#
param(
[string]$to,
[string]$subject,
[string]$body
)
$smtpServer = "ho-ex2010-caht1.exchanges erverpro.n et"
$smtpFrom = "reports@exchangeserverpro .net"
$smtpTo = $to
$messageSubject = $subject
$messageBody = [IO.File]::ReadAllText("fu ll_path\te st.txt")
$smtp = New-Object Net.Mail.SmtpClient($smtpS erver)
$smtp.Send($smtpFrom,$smtp To,$messag esubject,$ messagebod y)
as well as text file?
I was looking at another syntax:
$messageBody = [IO.File]::ReadAllText("fu
Send-Email.ps1
=============
#.\Send-Email.ps1 -To "administrator@exchangeser
#
#
param(
[string]$to,
[string]$subject,
[string]$body
)
$smtpServer = "ho-ex2010-caht1.exchanges
$smtpFrom = "reports@exchangeserverpro
$smtpTo = $to
$messageSubject = $subject
$messageBody = [IO.File]::ReadAllText("fu
$smtp = New-Object Net.Mail.SmtpClient($smtpS
$smtp.Send($smtpFrom,$smtp
ASKER
I meant:
Can I send either in html or plain text format using the given PowerShell commands?
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
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
ASKER
$smtp.Send($smtpFrom,$smtp To,$messag esubject,$ messagebod y)
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Something like the above but would like to assign emailbody the content
of a text or html file