Windows Vista Forums

Send email to multiple users
  1. #1


    Don Pedro Guest

    Send email to multiple users

    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

      My System SpecsSystem Spec

  2. #2


    Rob Campbell Guest

    RE: Send email to multiple users

    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@xxxxxx"

    $to = "user@xxxxxx"

    $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" wrote:

    > 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

      My System SpecsSystem Spec

  3. #3


    Don Pedro Guest

    RE: Send email to multiple users

    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" wrote:

    > 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@xxxxxx"
    >
    > $to = "user@xxxxxx"
    >
    > $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" wrote:
    >

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

      My System SpecsSystem Spec

  4. #4


    Rob Campbell Guest

    RE: Send email to multiple users

    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" wrote:

    > 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" wrote:
    >

    > > 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@xxxxxx"
    > >
    > > $to = "user@xxxxxx"
    > >
    > > $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" wrote:
    > >

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

      My System SpecsSystem Spec

  5. #5


    Don Pedro Guest

    RE: Send email to multiple users


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

    Thanks, Peter

    "Rob Campbell" wrote:

    > 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" wrote:
    >

    > > 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" wrote:
    > >

    > > > 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@xxxxxx"
    > > >
    > > > $to = "user@xxxxxx"
    > > >
    > > > $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" wrote:
    > > >
    > > > > 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

      My System SpecsSystem Spec

  6. #6


    Rob Campbell Guest

    RE: Send email to multiple users

    Assuming the column heading in your csv file that has the email address is
    "email", something like:


    #### Send email
    $mailhost = "<mail relay>"
    $from = "user@xxxxxx"
    $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" wrote:

    >
    > Grand so. So what is the missing code for looping thru the csv file?
    >
    > Thanks, Peter
    >
    > "Rob Campbell" wrote:
    >

    > > 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" wrote:
    > >

    > > > 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" wrote:
    > > >
    > > > > 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@xxxxxx"
    > > > >
    > > > > $to = "user@xxxxxx"
    > > > >
    > > > > $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" wrote:
    > > > >
    > > > > > 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

      My System SpecsSystem Spec

  7. #7


    Don Pedro Guest

    RE: Send email to multiple users

    This is my code:

    $mailhost = "10.10.3.240"
    $from = "myname@xxxxxx"
    $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?


    "Rob Campbell" wrote:

    > Assuming the column heading in your csv file that has the email address is
    > "email", something like:
    >
    >
    > #### Send email
    > $mailhost = "<mail relay>"
    > $from = "user@xxxxxx"
    > $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" wrote:
    >

    > >
    > > Grand so. So what is the missing code for looping thru the csv file?
    > >
    > > Thanks, Peter
    > >
    > > "Rob Campbell" wrote:
    > >

    > > > 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" wrote:
    > > >
    > > > > 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" wrote:
    > > > >
    > > > > > 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@xxxxxx"
    > > > > >
    > > > > > $to = "user@xxxxxx"
    > > > > >
    > > > > > $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" wrote:
    > > > > >
    > > > > > > 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

      My System SpecsSystem Spec

  8. #8


    Shay Levy [MVP] Guest

    RE: Send email to multiple users

    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 = "myname@xxxxxx" $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>

    >> Assuming the column heading in your csv file that has the email
    >> address is "email", something like:
    >>
    >> #### Send email $mailhost = "<mail relay>" $from = "user@xxxxxx"
    >> $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" wrote:
    >>

    >>> Grand so. So what is the missing code for looping thru the csv file?
    >>>
    >>> Thanks, Peter
    >>>
    >>> "Rob Campbell" wrote:
    >>>
    >>>> 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" wrote:
    >>>>
    >>>>> 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" wrote:
    >>>>>
    >>>>>> 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@xxxxxx"
    >>>>>>
    >>>>>> $to = "user@xxxxxx"
    >>>>>>
    >>>>>> $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" wrote:
    >>>>>>
    >>>>>>> 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
    >>>>>>>


      My System SpecsSystem Spec

  9. #9


    Rob Campbell Guest

    RE: Send email to multiple users

    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" wrote:

    > This is my code:
    >
    > $mailhost = "10.10.3.240"
    > $from = "myname@xxxxxx"
    > $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?
    >
    >
    > "Rob Campbell" wrote:
    >

    > > Assuming the column heading in your csv file that has the email address is
    > > "email", something like:
    > >
    > >
    > > #### Send email
    > > $mailhost = "<mail relay>"
    > > $from = "user@xxxxxx"
    > > $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" wrote:
    > >

    > > >
    > > > Grand so. So what is the missing code for looping thru the csv file?
    > > >
    > > > Thanks, Peter
    > > >
    > > > "Rob Campbell" wrote:
    > > >
    > > > > 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" wrote:
    > > > >
    > > > > > 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" wrote:
    > > > > >
    > > > > > > 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@xxxxxx"
    > > > > > >
    > > > > > > $to = "user@xxxxxx"
    > > > > > >
    > > > > > > $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" wrote:
    > > > > > >
    > > > > > > > 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

      My System SpecsSystem Spec

  10. #10


    Don Pedro Guest

    RE: Send email to multiple users

    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" wrote:

    > 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" wrote:
    >

    > > This is my code:
    > >
    > > $mailhost = "10.10.3.240"
    > > $from = "myname@xxxxxx"
    > > $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?
    > >
    > >
    > > "Rob Campbell" wrote:
    > >

    > > > Assuming the column heading in your csv file that has the email address is
    > > > "email", something like:
    > > >
    > > >
    > > > #### Send email
    > > > $mailhost = "<mail relay>"
    > > > $from = "user@xxxxxx"
    > > > $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" wrote:
    > > >
    > > > >
    > > > > Grand so. So what is the missing code for looping thru the csv file?
    > > > >
    > > > > Thanks, Peter
    > > > >
    > > > > "Rob Campbell" wrote:
    > > > >
    > > > > > 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" wrote:
    > > > > >
    > > > > > > 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" wrote:
    > > > > > >
    > > > > > > > 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@xxxxxx"
    > > > > > > >
    > > > > > > > $to = "user@xxxxxx"
    > > > > > > >
    > > > > > > > $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" wrote:
    > > > > > > >
    > > > > > > > > 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

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Send email to multiple users problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I restrict some users so they can't send out external email? john doe SBS Server 2 25 Feb 2010
Re: How can multiple users access same mailbox and send as that ma Ace Fekay [MVP-DS, MCT] SBS Server 0 01 Feb 2010
send email to multiple address Dennis Lim Vista mail 1 21 May 2009
how to send a msg to multiple email addresses Moreau Vista mail 7 18 Nov 2008
Multiple email users David Anderson Vista mail 9 29 May 2008