vbscript check if email is sent - email

I have created a script to send a weekly report but I don't know how to implement the status message if the email is successfully sent. Currently I just have a message which shows up on confirm to send the message, but that does not really check if the email is sent.
This is my script:
Dim fso, path, file, recentDate, recentFile, ToAddress, FromAddress, MessageSubject, MyTime, MessageBody, MessageAttachment, ol, ns, newMail
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
For Each file in fso.GetFolder("C:\REPORTData\temp").Files
If (recentFile is Nothing) Then
If UCase(fso.GetExtensionName(file.name)) <> "PDF" Then
Set recentFile = file
End If
ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
If UCase(fso.GetExtensionName(file.name)) <> "PDF" Then
Set recentFile = file
End If
End If
Next
ToAddress = "randomemail#randomemail.com"
MessageSubject = "Emailing: "+recentFile.Name
MessageBody = "Your message is ready to be sent with the following file or link attachments: "+recentFile.Name
MessageAttachment = "C:\REPORTData\temp\"+recentFile.Name
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
CurrentWeek = Right(recentFile.Name, 2)
result = MsgBox ("Send REPORT for week: "+CurrentWeek, vbYesNo + vbQuestion, "REPORT Sender v.0.1")
Select Case result
Case vbYes
newMail.Send
Set shell = CreateObject("Wscript.Shell")
shell.Popup "Your REPORT for week "+CurrentWeek+" has been sent", 2, "SUCCESS"
Case vbNo
End Select

Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dcandy#gmail.com"
emailObj.To = "dcandy#gmail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then
Msgbox "Done"
Else
Msgbox err.number
End if
is how.

Related

Add multiple files from folder as attachment to email vbs

I have a vbs script that I have been using to add a file from a folder as an attachment to an email, and then send it automatically through Outlook. Which works great.
The problem that I cannot figure out, is how to add 2 files which are in the same folder, to 1 email. I have been trying several things but the only thing I've been able to manage is adding "file 1" twice in an email, and "file 2" twice in another email.
I am a total novice at this so I apologize if this is some easy fix I can't figure out.
theFolder = "folder location"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(theFolder).Files
SendEmail objFSO.GetAbsolutePathName(objFile)
Next
Set objFSO = Nothing
Sub SendEmail(theFileName)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
objMail.Attachments.Add(theFileName)
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
Pass the folder name as a parameter instead:
theFolder = "folder location"
SendEmail theFolder
Sub SendEmail(folderName)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(folderName).Files
objMail.Attachments.Add(objFile.Path)
Next
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
Set objFSO = Nothing
End Sub
You need to call the Attachments.Add() for each file in the folder. So, your code may look like that:
theFolder = "folder location"
Sub SendEmail()
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.To = "emailaddress"
objMail.cc = ""
objMail.Subject = "subject"
objMail.Body = "body"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(theFolder).Files
objMail.Attachments.Add(objFSO.GetAbsolutePathName(objFile))
Next
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
Set objFSO = Nothing
End Sub

Vbs Multiple Line Email

I am using a vbs file to send emails but it sends it in one line only. I want it to be multiple lines with & vbCRLF but it is not working for me.
Please help me with vbs emails with multiple line text.
My code :
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dc#gail.com"
emailObj.To = "dc#gail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO" & vbCRLF & "Test CDO line 2"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourUserName"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password1"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "Done"
Here's how I did it, just using ".HTMLBody" to be able to add formatting. Works fine using outlook.
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
with objMail
.Display 'To display message
.To = "test#test.com"
.cc = "test#tested.com"
.Subject = "FYI"
.HTMLBody = "<HTML><BODY><p>Dear Team,</p><P> Please be informed, </p><P> Regards, </p><P><B> My Name </B><br><big><i>My Company</i> </big>(of awesomeness) </br></p></BODY></HTML>"
end with

Form Load/Unload Event Not Triggered After Importing Form in Access 2007

So I just manually split a database following the steps found Here. For some reason the load event is not triggered on one form and on another the unload event is not triggered (but in each case, the opposite one does work). The second form (Unload not working) also refuses to go into design mode until I close it manually and then open it into design mode. I have confirmed that the open and close events are linked properly to their corresponding VBA code so I know that isn't it.
Any help with resolving this issue would be appreciated.
Edit-
Here is the code for the Load:
Private Sub Form_Load()
Dim scr As ScriptControl
Dim SQL As String
CheckConnection
Set scr = New ScriptControl
SQL = "UPDATE [Part Number] SET [Part Number].[Select] = False WHERE ((([Part Number].[Select])=True));"
CurrentDb.Execute SQL
scr.Language = "VBScript"
scr.AddCode "Sub T :Dim ChangeReg: Set ChangeReg = CreateObject(""WScript.Shell""):ChangeReg.regwrite " & _
"""HKCU\Software\Microsoft\Office\12.0\Access\Security\VBAWarnings"", ""1"", ""REG_DWORD"" : " & _
"Set ChangeReg = CreateObject(""WScript.Shell""):ChangeReg.regwrite " & _
"""HKCU\Software\Microsoft\Office\12.0\Excel\Security\VBAWarnings"", ""1"", ""REG_DWORD"": end sub"
scr.Run "T"
Locked = False
CancelBupdate = False
LockOff
USRID = Environ("Username")
TTTCount = 0
Started = True
SourceSelect.Value = 1
DoCmd.SelectObject acTable, , True
DoCmd.RunCommand acCmdWindowHide
DoCmd.ShowToolbar "Ribbon", acToolbarNo
ChgFrmOpen = False
Me.LocationSelect.Visible = False
Me.ClrSupLoc.Visible = False
Me.Label20.Visible = False
PSOn = True
Me.RepSelect.Value = ""
Me.SupplierSelect.Value = ""
Me.SupNumSelect.Value = ""
Me.LocationSelect.Value = ""
Me.Base5Select.Value = ""
Me.FullNbrSelect.Value = ""
Me.Label104.Caption = "Last Updated On: " & DLookup("[Last Updated]", "[Last Updated]")
ChangeCount = 0
CT = ""
CPP = ""
PGNP = ""
UpdateSub '****
InfoGet
RunFilter
Cascade
Me.Requery
Select Case USRID
Case "vn034153"
UsrInfoSt = "Logged in as User"
Case "vn043156"
UsrInfoSt = "Logged in as User"
Blah
Case "vn034157"
UsrInfoSt = "Logged in as Admin"
Me.UpdateButton.Visible = True
Case "vn034160"
UsrInfoSt = "Logged in as User"
Case "vn028040"
UsrInfoSt = "Logged in as User"
Case "vn028033"
UsrInfoSt = "Logged in as Admin"
Me.UpdateButton.Visible = True
Case "vn034931"
UsrInfoSt = "Logged in as User"
Case Else
UsrInfoSt = "Logged in as User"
End Select
Me.UsrInfo.Caption = UsrInfoSt
Application.SetOption "Confirm Action Queries", 0
Application.SetOption "Confirm Document Deletions", 0
Application.SetOption "Confirm Record Changes", 0
DoCmd.SetWarnings False
Application.SetOption "Auto compact", True
End Sub
And here is the code for the unload (using a cmd button and neither sub runs):
Private Sub OpenDatabase_Click()
DoCmd.OpenForm "2BHPartsDatabaseX"
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim TDF As TableDef
Dim QDF As QueryDef
Dim DBS As Database
Set DBS = CurrentDb()
Me.ReportForm.SourceObject = ""
For Each QDF In DBS.QueryDefs
If QDF.Name = "TempQRYX" Or QDF.Name = "DistinctPGNQry" Then
DBS.QueryDefs.Delete QDF.Name
End If
Next
For Each TDF In DBS.TableDefs
If TDF.Name = "ProjectTable" Then
DBS.TableDefs.Delete TDF.Name
Exit For
End If
Next
Application.SetOption "Confirm Action Queries", 1
Application.SetOption "Confirm Document Deletions", 1
Application.SetOption "Confirm Record Changes", 1
DoCmd.SetWarnings True
End Sub
I would comment (rem) out all the code in the Form_Load Event, and put a breakpoint on the
Sub procedure, then open the form to see if the event fires. After I see it fire,
I would begin to "un-rem" each line until the code line affecting the load event
is found.
I would do the same with the "Unload" issue.

Sending email with multiple attachments vb6

Can someone help me.How to send an email with multiples attachments.
I am using cdo and SMTP Send Mail for VB6. Everything works great except I am only able to send one attachment at a time.
here's the code
Public Function SendMail(sTo As String, sSubject As String, sFrom As String, _
sBody As String, sSmtpServer As String, iSmtpPort As Integer, _
sSmtpUser As String, sSmtpPword As String, _
sFilePath As String, bSmtpSSL As Boolean) As String
On Error GoTo SendMail_Error:
Dim lobj_cdomsg As CDO.Message
Set lobj_cdomsg = New CDO.Message
lobj_cdomsg.Configuration.Fields(cdoSMTPServer) = sSmtpServer
lobj_cdomsg.Configuration.Fields(cdoSMTPServerPort) = iSmtpPort
lobj_cdomsg.Configuration.Fields(cdoSMTPUseSSL) = bSmtpSSL
lobj_cdomsg.Configuration.Fields(cdoSMTPAuthenticate) = cdoBasic
lobj_cdomsg.Configuration.Fields(cdoSendUserName) = sSmtpUser
lobj_cdomsg.Configuration.Fields(cdoSendPassword) = sSmtpPword
lobj_cdomsg.Configuration.Fields(cdoSMTPConnectionTimeout) = 30
lobj_cdomsg.Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
lobj_cdomsg.Configuration.Fields.Update
lobj_cdomsg.To = sTo
lobj_cdomsg.From = sFrom
lobj_cdomsg.Subject = sSubject
lobj_cdomsg.TextBody = sBody
If Trim$(sFilePath) <> vbNullString Then
lobj_cdomsg.AddAttachment (sFilePath)
End If
lobj_cdomsg.Send
Set lobj_cdomsg = Nothing
SendMail = "ok"
Exit Function
SendMail_Error:
SendMail = Err.Description
End Function
Private Sub cmdSend_Click()
Dim retVal As String
Dim objControl As Control
For Each objControl In Me.Controls
If TypeOf objControl Is TextBox Then
If Trim$(objControl.Text) = vbNullString And LCase$(objControl.Name) <> "txtAttach" Then
Label2.Caption = "Error: All fields are required!"
Exit Sub
End If
End If
Next
Frame1.Enabled = False
Frame2.Enabled = False
cmdSend.Enabled = False
Label2.Caption = "Sending..."
retVal = SendMail(Trim$(txtTo.Text), _
Trim$(txtSubject.Text), _
Trim$(txtFromName.Text) & "<" & Trim$(txtFromEmail.Text) & ">", _
Trim$(txtMsg.Text), _
Trim$(txtServer.Text), _
CInt(Trim$(txtPort.Text)), _
Trim$(txtUsername.Text), _
Trim$(txtPassword.Text), _
Trim$(txtAttach.Text), _
CBool(chkSSL.Value))
Frame1.Enabled = True
Frame2.Enabled = True
cmdSend.Enabled = True
Label2.Caption = IIf(retVal = "ok", "Message sent!", retVal)
End Sub
Private Sub cmdBrowse_Click()
Dim sFilenames() As String
Dim i As Integer
On Local Error GoTo Err_Cancel
With cmDialog
.FileName = ""
.CancelError = True
.Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html;*.shtml)|*.htm;*.html;*.shtml|Images (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif"
.FilterIndex = 1
.DialogTitle = "Select File Attachment(s)"
.MaxFileSize = &H7FFF
.Flags = &H4 Or &H800 Or &H40000 Or &H200 Or &H80000
.ShowOpen
' get the selected name(s)
sFilenames = Split(.FileName, vbNullChar)
End With
If UBound(sFilenames) = 0 Then
If txtAttach.Text = "" Then
txtAttach.Text = sFilenames(0)
Else
txtAttach.Text = txtAttach.Text & ";" & sFilenames(0)
End If
ElseIf UBound(sFilenames) > 0 Then
If Right$(sFilenames(0), 1) <> "\" Then sFilenames(0) = sFilenames(0) & "\"
For i = 1 To UBound(sFilenames)
If txtAttach.Text = "" Then
txtAttach.Text = sFilenames(0) & sFilenames(i)
Else
txtAttach.Text = txtAttach.Text & ";" & sFilenames(0) & sFilenames(i)
End If
Next
Else
Exit Sub
End If
Err_Cancel:
End Sub
You are only passing in one file. Try passing in an array of files and loop through the array. Or, since it looks like its semicolon delimiting the list of files selected, try to just split the list...
For Each s As String in sFilePath.Split(";"c)
lobj_cdomsg.AddAttachemt(s)
Next
I have no idea how to run a vb 6 app anymore, but if this helps, please mark it so.

How to add a DKIM signature to classic ASP email sent using CDO

Using something like the below, is it possible to add a header for a DKIM signature? From what I've been reading, it doesn't look like it.. Why not?
Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
Const cdoSendUsingPort = 2
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "abc"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Update
End With
With iMsg
Set .Configuration = iConf
.To = recipient
.From = "foo#bar.com"
.Subject = "subject"
.HTMLBody = "body"
.Send
End With
If you already have the signature, you could add it to the Fields property of your iMsg object. I've done something similar before to set the "Reply To" and "Return Receipts" addresses:
.Fields("urn:schemas:mailheader:disposition-notification-to") = "test#example.com"
.Fields("urn:schemas:mailheader:return-receipt-to") = "test#example.com"
You could do the same to add any custom header, in your case the DKIM signature. Your iMsg object becomes:
With iMsg
Set .Configuration = iConf
.To = recipient
.From = "foo#bar.com"
.Subject = "subject"
.HTMLBody = "body"
.Fields("urn:schemas:mailheader:DKIM-Signature") = "YOUR_SIGNATURE_HERE"
.Send
End With
I hope that helps.