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.
Next: Wrapping things up >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer