Solved

Use vbScript to email info from csv file

Posted on 2004-04-05
8
541 Views
Last Modified: 2016-06-25
Need to establish a vbscript that will pull email addresses from a csv file & and pull parts of message body from same line then proceed to the next address. Prefer to use SMTP from IIS.

Example:
cvs file format:
name,emailaddress,reg
name,emailaddress,reg
name,emailaddress,reg
name,emailaddress,reg
etc

Message would be something like

To: "emailaddress"
From: admin@ourdomain.com
MessageSubject: requested information

Message body

Dear "name"

Thank you...blah blah blah below is the requested information
"reg"

Word in "" are the variables that would need to be pulled from the csv file.

Thanks

0
Comment
Question by:Curtv
  • 4
  • 3
8 Comments
 
LVL 14

Accepted Solution

by:
aelatik earned 350 total points
ID: 10760563
You can use various mailers to accomplish this. I made one that uses the CDO object. You need the set you SMTP adress to get it working.

   
    Dim FSO, TEXTSTREAM, CSV_FILE, CSV_LINES, CSV_ITEMS, CSV_LINE
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set TEXTSTREAM = FSO.OpenTextFile("c:\temp.csv", 1, False, 0)
        CSV_FILE = TEXTSTREAM.ReadAll
    Set TEXTSTREAM = Nothing
    Set FSO = Nothing
        CSV_LINES = Split(CSV_FILE, vbCrLf)
       
        For Each CSV_LINE In CSV_LINES
            CSV_ITEMS = Split(CSV_LINE, ",")
            Dim X
            X = "Dear " & CSV_ITEMS(0) & vbCrLf
            X = X & "Thank you...blah blah blah below is the requested information" & vbCrLf
            X = X & CSV_ITEMS(2) & vbCrLf
            X = X & " Word in ? are the variables that would need to be pulled from the csv file." & vbCrLf
            X = X & "Thanks"
            Mail CSV_ITEMS(0), CSV_ITEMS(1), X ' Call the mailer
            wscript.echo "Mailed to " & CSV_ITEMS(1)
        Next

Function Mail(M_NAME, M_TO, M_BODY)
    Set Mail = CreateObject("CDO.Message")
    Set CON = CreateObject("CDO.Configuration")
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "255.255.255.255"
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        CON.Fields.Update
    Set Mail.Configuration = CON
        Mail.From = "admin@ourdomain.com"
        Mail.To = M_TO
        Mail.CC = ""
        Mail.Subject = "requested information"
        Mail.HTMLBody = M_BODY
        Mail.Send
    Set Mail = Nothing
    Set CON = Nothing
End Function
0
 

Author Comment

by:Curtv
ID: 10760968
For educational purposes why are these pointing to microsoft

        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "255.255.255.255"
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        CON.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

Thanks for your fast response. I will try it out.

0
 
LVL 14

Expert Comment

by:aelatik
ID: 10763094
Curtv,

It looks like it is pointing to Microsoft but it isn't. This "http://schemas.microsoft.com/cdo/configuration/smtpserver" is a field/object name.
I had the same confusion in the beginning to.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Curtv
ID: 10766589
aelatik

Forgive my newbieness, perl guy.

I was looking for a way to send html mail. I was following http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10239836.html?query=format+text+html&searchType=topic, I must have done something wrong, got no text instead of html. From other postings there needs to be a Mail.BodyFormat = 0

For clarity is there a simple way to bold or underline one of the variables?

Thanks
0
 
LVL 14

Expert Comment

by:aelatik
ID: 10766918
Mail.BodyFormat = 0 is for CDONTS, this is the CDO object ( Windows 2000 and Windows XP ).

Does your mailclient show HTML formatted emails ?
0
 
LVL 14

Expert Comment

by:aelatik
ID: 10766944
I use exactly the same code and it works with me.

Do it like this :

            X = "<b>Dear</b> " & CSV_ITEMS(0) & vbCrLf
            X = X & "Thank you...blah blah blah below is the requested information" & vbCrLf
            X = X & CSV_ITEMS(2) & vbCrLf
            X = X & " Word in ? are the variables that would need to be pulled from the csv file." & vbCrLf
            X = X & "<b>Thanks</b>"
0
 

Author Comment

by:Curtv
ID: 10767200
Ah, making a moutain out of a mole hill. You are correct it works just as you said.

Thanks
0
 

Expert Comment

by:Member_2_7966052
ID: 41674228
Hi Everyone,

Could we do following CSV file and use the above script to send each email address an email with the left side content... abcd1234 and add123

CSV file content:

abcd1234, abacd@abc.com
add123,     add@add.com

purpose of the above would be the script reads  above CSV file. send email to abacd@abc.com the left hand side content "abacd1234" to "abacd@abc.com" ?

looking forward to hearing from you soon.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question