Hello, I am working on a VBS script that will pull a list of email address from a database then email every one on the list with an attached file. I have a working script however I would like to reuse the main portion of it with out having to hard code each script.
'This section reads database and sends to every one in the table
Set OBJdbConnection = CreateObject("ADODB.Connection")
OBJdbConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\EmailList.mdb"
SQLQuery = "SELECT address FROM Email"
Set Result = OBJdbConnection.Execute(SQLQuery)
if Not Result.EOF then
Do While Not Result.EOF
SendMail Result("address")
Result.MoveNext
Loop
end if
OBJdbConnection.Close
'This sub sends the e-mail to all uses from the database.
Sub SendMail(address)
Dim objMessage, Rcpt
Rcpt = Chr(34) & "<" & address & ">"
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
Const cdoAnonymous = 0
Const cdoBasic = 1 ' clear text
Const cdoNTLM = 2 'NTLM
'Delivery Status Notifications
Const cdoDSNDefault = 0 'None
Const cdoDSNNever = 1 'None
Const cdoDSNFailure = 2 'Failure
Const cdoDSNSuccess = 4 'Success
Const cdoDSNDelay = 8 'Delay
Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
set objMsg = CreateObject("CDO.Message")
set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
With objFlds
.Item(" = cdoSendUsingPort
.Item(" = "mail.domain.com"
.Item(" = cdoBasic
.Item(" = "domain\usename"
.Item(" = "mypassword"
.Update
End With
strBody = "This is a sample message." & vbCRLF
strBody = strBody & "It was sent using CDO." & vbCRLF
With objMsg
Set .Configuration = objConf
.To = Rcpt
.From = "username@.com"
.Subject = "This is a CDO test message"
.TextBody = strBody
'use .HTMLBody to send HTML email.
.Addattachment "C:\2.pdf"
.Fields("urn:schemas:mailheader:disposition-notification-to") = "user@domain.com"
.DSNOptions = cdoDSNSuccessFailOrDelay
.Fields.update
.Send
End With
End Sub
This works fine, but I would like to replace the hard coded file attachemnt name and the table to run the query from.
I have tryed using this script to start that way I can change the path and table name.
Set oWSH = CreateObject("WScript.Shell")
oWSH.Environment("Volatile").Item("Tvar1") = "Tablename"
oWSH.Environment("Volatile").Item("Fvar1") = "Filename"
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objShell.Run "c:\database4.vbs"
The problem I am having is when I try and replace the table name for the query I am not getting the correct argument. When I try to use the var to change the file name it keeps tellign me file not found.
I just need some one to give me a little nudge in the correct direction.
Rob
The FaygoMan
'This section reads database and sends to every one in the table
Set OBJdbConnection = CreateObject("ADODB.Connection")
OBJdbConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\EmailList.mdb"
SQLQuery = "SELECT address FROM Email"
Set Result = OBJdbConnection.Execute(SQLQuery)
if Not Result.EOF then
Do While Not Result.EOF
SendMail Result("address")
Result.MoveNext
Loop
end if
OBJdbConnection.Close
'This sub sends the e-mail to all uses from the database.
Sub SendMail(address)
Dim objMessage, Rcpt
Rcpt = Chr(34) & "<" & address & ">"
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
Const cdoAnonymous = 0
Const cdoBasic = 1 ' clear text
Const cdoNTLM = 2 'NTLM
'Delivery Status Notifications
Const cdoDSNDefault = 0 'None
Const cdoDSNNever = 1 'None
Const cdoDSNFailure = 2 'Failure
Const cdoDSNSuccess = 4 'Success
Const cdoDSNDelay = 8 'Delay
Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
set objMsg = CreateObject("CDO.Message")
set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
With objFlds
.Item(" = cdoSendUsingPort
.Item(" = "mail.domain.com"
.Item(" = cdoBasic
.Item(" = "domain\usename"
.Item(" = "mypassword"
.Update
End With
strBody = "This is a sample message." & vbCRLF
strBody = strBody & "It was sent using CDO." & vbCRLF
With objMsg
Set .Configuration = objConf
.To = Rcpt
.From = "username@.com"
.Subject = "This is a CDO test message"
.TextBody = strBody
'use .HTMLBody to send HTML email.
.Addattachment "C:\2.pdf"
.Fields("urn:schemas:mailheader:disposition-notification-to") = "user@domain.com"
.DSNOptions = cdoDSNSuccessFailOrDelay
.Fields.update
.Send
End With
End Sub
This works fine, but I would like to replace the hard coded file attachemnt name and the table to run the query from.
I have tryed using this script to start that way I can change the path and table name.
Set oWSH = CreateObject("WScript.Shell")
oWSH.Environment("Volatile").Item("Tvar1") = "Tablename"
oWSH.Environment("Volatile").Item("Fvar1") = "Filename"
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objShell.Run "c:\database4.vbs"
The problem I am having is when I try and replace the table name for the query I am not getting the correct argument. When I try to use the var to change the file name it keeps tellign me file not found.
I just need some one to give me a little nudge in the correct direction.
Rob
The FaygoMan