Solved

How can I send an email using Powershell?

Posted on 2014-10-08
15
200 Views
Last Modified: 2014-10-14
I would like to send an automated email using Powershell, in an Exchange environment, in a network setting.
The email needs to look at a text file and insert the data inside the text file in the body of the email, without attaching the text file.

A sample script would be useful.
0
Comment
Question by:100questions
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40368420
You could use send-mailmessage cmdlet.  Below is an example:

send-mailmessage -from "UserA <userA@mydomain.com>" -to "UserB <userB@mydomain.com.com>"" -subject "Requested data with attachment" -body "As promised, included is the attachment." -Attachments "file1.csv" -priority High -dno onSuccess, onFailure -smtpServer smtp.mydomain.com

Also look at this technote document for more info:

http://technet.microsoft.com/en-us/library/hh849925.aspx
0
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 40368427
Hi

have a look at this site many good examples

https://4sysops.com/archives/send-mailmessage-paramaters-and-examples/

you will need to enter to exchange server in as the smtp server.  You also may need to add a rule in exchange to allow relaying from the sending host.
let me know how you get on.

Regards
Joe
0
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 40368431
which version of Powershell are you running ? as send-mailmessage has only been available from Powershell V3 onwards.
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 11

Expert Comment

by:Ganesh Kumar A
ID: 40368471
Here is the sample script you can help for good start, but later you need to modify or enhance whenever required..

#READ LINE & WRITE AS INPUT FROM TEXT FILE TO MAIL BODY
# ALL CAPS are variable which needs to be replaced with your mail server info and email id and subject.

$smtpServer = "YOUR_MAIL_SERVER_NAME"
$smtpFrom = "FROM_EMAIL_ID"
$smtpTo = "TO_EMAIL_ID"
$messageSubject = "YOUR_MAIL_SUBJECT"

[string]$messagebody = ""

$txtinput = Get-Content "C:\test.txt"

foreach ($lineinputfile in $txtinput)
{

      $messagebody = $messagebody + $lineinputfile + "`r`n"

}

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

Author Comment

by:100questions
ID: 40368501
Thanks to all.  This seems to work - however what I need to do is copy the contents of a text file into the body of the email.

This is the script I have so far which works however I need to copy the contents of a text file into the body of the attachment, not attach the text file to the email.   The text file must not be attached to the email.

send-mailmessage -from "Name <email@email.com>" -to "Name <email@email.com>" -subject "Requested data with attachment" -body "As promised, included is the attachment." -priority High -dno onSuccess, onFailure -smtpServer info.domain.local
0
 

Author Comment

by:100questions
ID: 40368513
@GaneshKumar -  Thanks, I will try this.  Is this a Powershell script?
0
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 40368535
yes this is a Powershell script
0
 

Author Comment

by:100questions
ID: 40368584
@GaneshKumar - Thanks, this works, however it does not respect the spacing in the original text file when it copies the contents of the data into the body of the email.

For instance this is the data in the source text file.

PO Number: 1234567894
Item 1: UPC: 12345678912341, Desc: Desc 1, Qty: 10, Price: $5.00
Item 2: UPC: 56456465465465, Desc: Desc 2, Qty: 1, Price: $10
Item 3: UPC: 45646464646555, Desc: Desc 3, Qty: 2, Price: $22.00
Item 4: UPC: 45646545646545, Desc: Desc 4, Qty: 4, Price: $10
Item 5: UPC: 15615656156161, Desc: Desc 5, Qty: 5, Price: $12.00
Item 6: UPC: 54654656465465, Desc: Desc 6, Qty: 5, Price: $4.00
Item 7: UPC: 45646645464646, Desc: Desc 7, Qty: 1, Price: $2
Total: $100.00

But inside the body of the email it's not shown orderly as it's shown above..

It does not respect the break at each line.. it bunches data together.
When each line ends, then it needs to break to a new line, or else the data is not being properly imported into the body.

Any modifications to the script?
0
 
LVL 10

Expert Comment

by:Joe Klimis
ID: 40368653
try this
#READ LINE & WRITE AS INPUT FROM TEXT FILE TO MAIL BODY
# ALL CAPS are variable which needs to be replaced with your mail server info and email id and subject.

$smtpServer = "YOUR_MAIL_SERVER_NAME"
$smtpFrom = "FROM_EMAIL_ID"
$smtpTo = "TO_EMAIL_ID"
$messageSubject = "YOUR_MAIL_SUBJECT"

$messagebody= (Get-Content C:\test.txt | out-string )
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

Open in new window

0
 

Author Comment

by:100questions
ID: 40368674
@JoeKlimis - thanks Joe, however it only respects the break at the first line and the last line, the rest is attached together and not properly outputting correctly.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40369656
Joe, I know the online pages tell different, but Send-MailMessage is available since v2 (see for example http://theucguy.net/send-emails-easily-using-powershell-v2/), and I've used and suggested it since.

100questions, something like this worked fine for me:
Send-MailMessage -SmtpServer mail.there.com -From me@here.com -To you@there.com -Subject Test -Body ((get-content file.txt) -join "`r`n")

Open in new window

0
 

Author Comment

by:100questions
ID: 40370936
@Olemo -  Thanks however this does not work well at all, since it does not respect the separate lines, again it bunches the 2nd and penultim line together incorrectly.
0
 

Accepted Solution

by:
100questions earned 0 total points
ID: 40370950
This solution works for me:

$Data = Get-Content "C:\Users\user\data\Text.txt"
 
  Send-MailMessage -From 'email@email.com' -To 'email@email.com','email@email.com' `
  -SmtpServer 'servername.local' `
  -Subject 'Information needed - test ' `
  -BodyAsHtml `
  -Body ($Data -join "<br>")
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40371290
it worked for me, as said, with Exchange and Outlook. The HTML mail approach however is more reliable.
0
 

Author Closing Comment

by:100questions
ID: 40379401
Works for me.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Arrays & Loops in PowerShell 4 35
query all mailbox rules 5 34
add error checking to powershell script 2 16
Ruby or Python 7 63
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question