sábado, 23 de julho de 2011

VB script to backup and send e-mail

Sometimes there is the need to send regularly to a specific e-mail address\person files with updated data. And most of the times it is done at the end of the day, when all the transaction of the day were recorded and ready to send. It can be quite boring if you do it every day, using for example outlook or the webmail, because the steps are always the same: creating the e-mail, adding the email address zipping the content, attaching the content, writing the subject and the body and sending the e-mail. And if you use webmail there are additional steps of logging on and the file uploading. So why not finding a solution that does this automatically with no human intervention.

And it is possible if we combine windows task manager, some vb script, 7z and a Gmail account. You can download the source code at sendMail. The zip file contains 3 files:
  1. sendMail.vbs - vb script
  2. 7z.exe - program to create the zip file
  3. logs.txt - log file to log the time and the events
Some configuration is needed:

homePath =  "C:\xxx\" 'folder where sendMail.vbs, logs.txt, 7z.exe and created zip files are
dataPath = "C:\xxxx\" ' folder that contains the files to zip
attachFile ="zipFile.ZIP"
'created zip file that will be attached to the e-mail

if zipFile("MAP*.?10") = 0 then ' file name of the file to zip or a wildcard containing the files to zip

For sending the e-mail, configure correctly the following parameters with your Gmail account details:

With objEmail
 .from = "xxx@gmail.com"
 .To = "xxx@gmail.com; xxx@yahoo.com.br" .Subject = "Test Mail" .Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog" 

  .Item (schema & "smtpserver") = "smtp.gmail.com"
  .Item (schema & "sendusername") = "xxx@gmail.com"
  .Item (schema & "smtpaccountname") = "xxx@gmail.com"
  .Item (schema & "sendpassword") ="xxxxxx"

You can test it double clicking on the sendMail.vbs file and checking thee events logged on the logs.txt.

Later I will be explaining how run this file regularly from windows task manager.