Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Building a Mass-Emailer in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Building a Mass-Emailer in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-04-04

    Table of Contents:
  • Building a Mass-Emailer in WSH
  • Creating a mailing list
  • Customizing a form message
  • Wrapping things up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Mass-Emailer in WSH - Creating a mailing list


    (Page 2 of 4 )

    This script won’t be doing us a whole lot of good if we can’t tell it where to send our email.  For the purposes of this article I’m going to read my email list from a comma-delimited text file.  With a little more code, you could easily query a database or read them from a spreadsheet.  You could even read them from your Outlook Address Book if you wanted.

    emaillist.csv

    John Doe,john@mymail.com

    Sales Group,sales@mycompany.com

    Jane Wilkens,janew@outsourcing.com

    Frank,frank_k@yahoo.co.uk

    Here I just created a simple CSV file with names and corresponding email addresses.  I’ve named it emaillist.csv and saved it to the same directory as my script.  It should be apparent that we’re going to need to do a little parsing to be able to use this list so let’s make a call to the FileSystemObject and open our text file.

    strFromEmail = "My Name <me@mymail.com>"

    strSubject = "Daily Sales Recap"

    strBody = "C:salesdailyreport.htm"

     

    Set objMessage = CreateObject("CDO.Message")

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objList = objFso.OpenTextFile("emaillist.csv")

    Now we need to create a small parsing script to read in our names and email addresses.  We’ll use a simple Do…Loop like you’ve probably seen a dozen times.

    Do Until objList.AtEndOfStream

       strLine = objList.ReadLine

       SendMail

    Loop

    objFile.Close

    This simple snippet loops through our file one line at a time and then calls our SendMail routine for each contact.  Of course, this doesn’t do us much good at the moment because all we’ve done is read the information into our script.  Now let’s parse it.

    Do Until objFile.AtEndOfStream

       strLine = objFile.ReadLine

       arrContact = Split(strLine, ", ")

       strName = arrContact(0)

       strToEmail = arrContact(1)

       ReDim arrContact(0)

       SendMail strName, strToEmail, strBody

    Loop

    objFile.Close

    Each line in our text file contains a name and an email address separated by a comma.  VBScript’s Split function works nicely to split our text line into a simple array.  The first array item contains the contact’s name and the second contains the email address.  Don’t forget that array items are zero-based.

    After assigning the variables we use a ReDim statement to empty the array’s contents for the next loop iteration.  This isn’t completely necessary, but it’s a good habit to get into because it can be a problem spot in more complex scripts.

    Finally, we send those variables off to our SendMail subroutine.  That means we’ll need to make a slight change to its declaration so that it accepts our variables as attributes.  While we’re making changes, let’s add a little extra code to prevent problems.

    Do Until objFile.AtEndOfStream

       strLine = objFile.ReadLine

       arrContact = Split(strLine, ", ")

       strName = Trim(Replace(arrContact(0), Chr(34), ""))

       strToEmail = Trim(Replace(arrContact(1), Chr(34), ""))

       ReDim arrContact(0)

       SendMail strName, strToEmail, strBody

    Loop

    objFile.Close

     

    Sub SendMail(strName, strToEmail, strBody)

    In this piece I added a little precautionary code.  I’ve made use of the Replace function to strip any unwanted quotation marks from our strings.  I finish up by using Trim to make sure there’s no leading or trailing spaces.

    Why have I done this?  I’m assuming that many CSV files are exported from some other program.  It’s not uncommon for programs to export text strings in quotation marks.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Learn how to create a mass-mailer using CDO in WSH. You'll be able to build and...
     

    WINDOWS SCRIPTING ARTICLES

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway