Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 - Customizing a form message


    (Page 3 of 4 )

    We have a functioning mass-mailing script but that’s never good enough for me.  Let’s add some extra bells and whistles, shall we?

    If you’ve ever used any kind of mass-mailing system before you’ve probably noticed that they let you use variables in your message that are replaced with each contact’s information as the message is sent.  This is known as form mailing.  An example message might look like this.

    message.txt

    Dear $name,

    <p>

    We're sending this email to notify you that your name has been added to our mailing list.  If you would like to be removed, please contact us at <a rel=nofollow target=_blank style=color:blue; href= "mailto:remove@mymail.com ">remove@mymail.com</a>.

    <p>

    Thanks,<br>

    <a rel=nofollow target=_blank style=color:blue; href="http://www.abccompany.com">ABC Company, Inc.</a>

    I’ll call this file message.txt and save it in the same directory as my other two files.  We’ll need to add some code to read in its contents.  We’ll add a small snippet to the beginning of our script to do that.

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

    strSubject = "Daily Sales Recap"

     

    Set objMessage = CreateObject("CDO.Message")

    Set objFso = CreateObject("Scripting.FileSystemObject")

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

    Set objBody = objFso.OpenTextFile("message.txt")

     

    strBody = objBody.ReadAll

    objBody.Close

    In my text example, the variable $name is used as a form element.  It should be replaced with the name of our recipient before the message is sent.  To do that we’ll need to add some code to our subroutine so that it’s replaced before sending each message.

    Sub SendMail(strName, strToEmail, strBody)

       objMessage.From = strFromEmail

       objMessage.To = strToEmail

       objMessage.Subject = strSubject

       strBody = Replace(strBody, "$name", strName)

       objMessage.HTMLBody = strBody

       objMesasge.AutoGenerateTextBody = True

       objMessage.Configuration.Fields.Item _

           ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

       objMessage.Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _

           "smtp.mail.com"

       objMessage.Configuration.Fields.Item _

       ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _

           = 25

       objMessage.Configuration.Fields.Update

       objMessage.Send

    End Sub

    Here I’ve added a line that uses the VBS Replace function to find every occurrence of $name in our body text and replace it with the contents of the strName variable.  You would need to add another line to replace any additional form elements as well.

    Running the script now will replace our body text with the name of our first contact, but then we run into a little problem.  After the first replacement, strBody no longer contains any of our form elements.  How do we go about changing it for each of the other contacts?

    The answer is simple.  We could write some code to determine the name of the last contact and do a replacement on that, but that’s too much work.  The easiest way is to change our Sub statement one last time.

    Sub SendMail(strName, strToEmail, ByVal strBody)

    I’ve added the ByVal statement.  The ByVal statement will create a duplicate instance of strBody in memory.  One will be the original strBody in the Global namespace, and the duplicate will only be available in the SendMail namespace.

    What does this do?  This means that any changes we make to strBody in the SendMail subroutine will only affect its copy.  This means that when we exit the subroutine, we will be left with the global strBody in its unaltered form.

    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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT