Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use vbScript to email info from csv file

Posted on 2004-04-05
8
Medium Priority
?
580 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 1400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses

971 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