Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send email to multiple users

336 views
Skip to first unread message

Don Pedro

unread,
Nov 4, 2008, 10:16:01 AM11/4/08
to
Anyone got some code that will allow me to automate the sending of an email
to a list of email addresses contained in a CSV file?

All help greatly appreciated

Rob Campbell

unread,
Nov 4, 2008, 10:33:01 AM11/4/08
to
This is may "boilerplate" for sending email.

Do you help with reading in the csv and construting the loop, or just
sending the email?

#### Send email

$mailhost = "<mail relay>"

$from = "user@domain"

$to = "user@domain"

$subj = "Email Subject"

$body = "Email body."

#$attach = "<attachment path>"


$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.Host = $mailhost

$mailmessage = New-Object system.net.mail.mailmessage

$mailmessage.from = ($from)
$mailmessage.To.add($to)

$mailmessage.Subject = $subj
$mailmessage.Body = $body

if ($attach){
$mailattachment = new-object System.Net.Mail.Attachment($attach)
$mailmessage.attachments.add($mailattachment)
}

$smtpclient.Send($mailmessage)

Don Pedro

unread,
Nov 4, 2008, 10:46:20 AM11/4/08
to
Hi Rob,

Thanks for the reply. I would need help also with how to point to a CSV file
and also the looping thru the email addresses.

The $mailhost variable is the IP address of the SMTP box I assume? This
would be the hub transport server? Does it not need credentials passed to it
in order to allow relaying?

Thanks again.

Rob Campbell

unread,
Nov 4, 2008, 11:04:09 AM11/4/08
to
Whether or not it need credentials is going to depend on how the server is
configured.

Processing the csv file is going to depend on how your input looks. Usually
when processing a csv file it's easiest start with a file with column
headings that don't have an embeded spaces, and do an import-csv. This will
produce a collection of objects with properties that match the column heading
names.

You might want to play with this a litte from the command line first,
importing a sample csv file, then looking at the properties of one of the
returned objects to get a handle on how it's creating the objects from the
csv file.

Don Pedro

unread,
Nov 4, 2008, 11:25:04 AM11/4/08
to

Grand so. So what is the missing code for looping thru the csv file?

Thanks, Peter

Rob Campbell

unread,
Nov 4, 2008, 11:45:08 AM11/4/08
to
Assuming the column heading in your csv file that has the email address is
"email", something like:


#### Send email
$mailhost = "<mail relay>"
$from = "user@domain"

$subj = "Email Subject"
$body = "Email body."
#$attach = "<attachment path>"

$recpts = import-csv <csv file>

for each ($recp in $recpts){
$to = $recpt.email

$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.Host = $mailhost
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($from)
$mailmessage.To.add($to)
$mailmessage.Subject = $subj
$mailmessage.Body = $body

if ($attach){
$mailattachment = new-object System.Net.Mail.Attachment($attach)
$mailmessage.attachments.add($mailattachment)
}

$smtpclient.Send($mailmessage)

}


You can prune out the attachment bits if the email you're sending won't have
any attachements.

This will send one email per recipient in your csv. You can also re-arrange
where the loop is to have it send one email to multiple recipients.

Don Pedro

unread,
Nov 5, 2008, 6:57:01 AM11/5/08
to
This is my code:

$mailhost = "10.10.3.240"
$from = "myn...@mydomain.com"
$subj = "Email Subject"
$body = "This is a test."

$recpts = import-csv c:\email.csv

for each ($recp in $recpts){
$to = $recpt.email

$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.Host = $mailhost
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($from)
$mailmessage.To.add($to)
$mailmessage.Subject = $subj
$mailmessage.Body = $body

$smtpclient.Send($mailmessage)

}


When I run this, I get the message: Missing opening '(' after keyword 'for'.
At C:\send-mail.ps1:9 char:5
+ for e <<<< ach ($recp in $recpts){

Any ideas?

Shay Levy [MVP]

unread,
Nov 5, 2008, 7:01:10 AM11/5/08
to

Hello Don,

Type foreach with no spaces (e.g 'for each')


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


DP> This is my code:
DP>
DP> $mailhost = "10.10.3.240" $from = "myn...@mydomain.com" $subj =
DP> "Email Subject" $body = "This is a test."
DP>
DP> $recpts = import-csv c:\email.csv
DP>
DP> for each ($recp in $recpts){
DP> $to = $recpt.email
DP> $SmtpClient = new-object system.net.mail.smtpClient $SmtpClient.Host
DP> = $mailhost $mailmessage = New-Object system.net.mail.mailmessage
DP> $mailmessage.from = ($from) $mailmessage.To.add($to)
DP> $mailmessage.Subject = $subj $mailmessage.Body = $body
DP>
DP> $smtpclient.Send($mailmessage)
DP>
DP> }
DP>
DP> When I run this, I get the message: Missing opening '(' after
DP> keyword 'for'.
DP> At C:\send-mail.ps1:9 char:5
DP> + for e <<<< ach ($recp in $recpts){
DP> Any ideas?
DP>
DP> "Rob Campbell" wrote:
DP>

Rob Campbell

unread,
Nov 5, 2008, 8:06:01 AM11/5/08
to

As Shay noted, the "for each" is a typo (my bad).

There's also another typo in that line you need to correct.

> for each ($recp in $recpts){
> $to = $recpt.email

This should be

foreach ($recpt in $recpts){


Otherwise $recpt.email will evaluate correctly.

Don Pedro

unread,
Nov 5, 2008, 10:51:01 AM11/5/08
to
Rob/Shay,

Thanks, that worked a treat.

Two final questions. If I want to pass logon credentials for relaying, what
would I need to add?

Also, whats the best way to format - can I use rich text etc? If so, how?

Thanks a million for your excellent help so far.

Peter

Rob Campbell

unread,
Nov 5, 2008, 3:51:02 PM11/5/08
to
Maybe someone else can help with those. I'm just a network/Exchange admin.
My mail servers don't require authentication internally, and I've never
needed to script sending anything but plain text email, so I'm not going to
be much help.

OldDog

unread,
Nov 6, 2008, 4:10:25 PM11/6/08
to
On Nov 5, 9:51 am, Don Pedro <DonPe...@discussions.microsoft.com>
wrote:
> > > > > > > > > All help greatly appreciated- Hide quoted text -
>
> - Show quoted text -

Two final questions. If I want to pass logon credentials for
relaying, what
would I need to add?

$smtpclient.Credentials = new-object
system.net.networkcredential("<myname>","<mypassword>")
OR
$smtpclient.Credentials =
[System.Net.CredentialCache]::DefaultNetworkCredentials

<------- Question 2
----------------------------------------------------------------------
>
Here is a sample vbScript that uses HTML for the body of the email,
Should be easy enogh to convert to powershell.

Sending an HTML email.

Note the use of the Cc & Bcc properties to send using Blind Carbon
Copy (Bcc) and Carbon Copy (Cc).
These properties can be used with either text or HTML email.

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "m...@my.com"
objMessage.To = "te...@paulsadowski.com"
'The line below shows how to send using HTML included directly in your
script
objMessage.HTMLBody = "<h1>This is some sample message html.</h1>"

'The line below shows how to send a webpage from a remote site
'objMessage.CreateMHTMLBody "http://www.paulsadowski.com/wsh/"

'The line below shows how to send a webpage from a file on your
machine
'objMessage.CreateMHTMLBody "file://c|/temp/test.htm"

objMessage.Bcc = "y...@your.com"
objMessage.Cc = "yo...@your.com"
objMessage.Send

Don Pedro

unread,
Nov 7, 2008, 6:45:01 AM11/7/08
to
Thanks OldDog,

It seems those HTML commands will not work and have searched to find the PS
equivalent.

Not much joy yet so if anyone can help that would be great.

Shay Levy [MVP]

unread,
Nov 7, 2008, 8:08:24 AM11/7/08
to
Hi Don,


You can convert object(s) to HTML with ConvertTo-HTML and assign the output
to the mail message body:

$mailmessage.IsBodyHtml = $true
$mailmessage.Body = (Get-Process | ConvertTo-HTML)
$smtpclient.Send($mailmessage)

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

DP> Thanks OldDog,
DP>
DP> It seems those HTML commands will not work and have searched to find
DP> the PS equivalent.
DP>
DP> Not much joy yet so if anyone can help that would be great.
DP>
DP> "OldDog" wrote:
DP>

0 new messages