All help greatly appreciated
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)
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.
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.
Thanks, Peter
#### 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.
$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?
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>
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.
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
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
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.
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>