Solved

Use vbScript to email info from csv file

Posted on 2004-04-05
8
535 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Curtv
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 14

Expert Comment

by:aelatik
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now