Solved

How can I send an email using Powershell?

Posted on 2014-10-08
15
198 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:JoeKlimis
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:JoeKlimis
ID: 40368431
which version of Powershell are you running ? as send-mailmessage has only been available from Powershell V3 onwards.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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:JoeKlimis
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:JoeKlimis
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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