Some document scanners have the capability to send an email with the scanned document attached. Maybe your scanner can only store scanned files on a NAS, network share or in a folder on your computer. In that case, the next script will detect new scans and email them to you.
Yes, it is VBscript and that is boo, boo, as I mentioned before. However, the email method here, although deprecated, is also used by PowerShell users since it can deal with implicit SSL.
This post consists of two parts:
1. The script
2. A method to start the script automatically if the user is not logged on
The script
Copy & paste all the lines between the markers to Notepad or so and save the script as "MailScan.vbs". Then modify the Recipient with your email address. Modify the SMTP parameters. You might want to create a Gmail address specifically for sending scans. Finally, configure the folder where your scanner stores the scans.
--- 8< -------------------------------------
on error resume next
sub SendMail(Recipient, PDFfile)
Dim objEmail
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "scannername@yourdomain.com"
objEmail.To = Recipient
objEmail.Subject = "Scanned document from scanner {scannername/location}"
objEmail.Textbody = now() & ", this email contains a scanned document from scanner {scannername/location}: " & PDFfile
'--- When you host your own mailserver, use this:
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.1.13" ' IP address of your emailserver
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 0
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="smtpaccount@yourdomain.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="{Password}"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'--- Or you can use Google's SMTP service:
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = aGoogleAccount
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = theGooglePassword
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
REM objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objEmail.AddAttachment PDFfile
objEmail.Configuration.Fields.Update
objEmail.Send
SendMail = Err
end sub
'--- Main ----------------------------------------------------------------------------------------------------
const emailReceipient = "your@email_addre.ss"
const intInterval = "5" 'check every 'intInterval' seconds for new file(s)...
dim fso
set fso=CreateObject("scripting.FileSystemObject")
'--- Split up the drive and the scan folder. Needed for WMI...
strDrive = "M:"
strFolder = "\\ProgramData\\MailScan\\"
wscript.echo "Scanning folder for files to arrive: " & replace(strDrive,"\\","\") & replace(strFolder,"\\","\")
'--- Set up event WMI handler...
Set objWMIService = GetObject( "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2" )
strQuery = _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'" _
& " And TargetInstance.Path='" & strFolder & "'"
Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
'--- Loop endless with an interval of 'intInterval'...
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
f=objTargetInst.Name
WScript.Echo "*** New file received: " & f
wscript.echo "*** emailReceipient : " & emailReceipient
wscript.echo "*** Filename: " & f
'--- Check for PDF file and email that...
if Instr(lcase(f),".pdf") > 0 then
call SendMail(emailReceipient,f)
wscript.echo "*** Emailing of PDF file done"
if Err <> 0 then wscript.echo "*** Error sending mail. Errormessage: 0x" & hex(Err) & " - " & Err.Description
end if
'--- You can delete the PDF file after emailing. Uncomment the next lines...
'fso.DeleteFile f,true
'wscript.echo "File deleted: " & f
End Select
Loop
--- 8< -------------------------------------
Testing the script
To test if everything works fine we manually start the script:c:\Scripts> cscript //nologo MailScan.vbs
Make a scan, or just copy a PDF file in the scanfolder, and check the output on the screen. If all goes well you see:
Scanning folder for files to arrive: M:\ProgramData\MailScan\
*** New file received: m:\programdata\mailscan\manual_en - copy (2) - copy - copy - copy.pdf
*** emailTo : your@email_addre.ss
*** Filename: m:\programdata\mailscan\manual_en - copy (2) - copy - copy - copy.pdf
*** Emailing of PDF file ready
If there is an error sending the PDF you will see:
Scanning folder for files to arrive: M:\ProgramData\MailScan\
*** New file received: m:\programdata\mailscan\manual_en - copy (5).pdf
*** emailTo : your@email_addre.ss
*** Filename: m:\programdata\mailscan\manual_en - copy (5).pdf
*** Emailing of PDF file done
*** Error sending mail. Errormessage: 80040211 - The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response
was not available
The Windows Service
You want to have this service 24x7 available, even when you are not logged on to your computer. When we use script in a Windows Service we can run the script automatically in the background. We need a utility to configure this for you: the Non-Sucking Service Manager. Download NSSM here.
Start NSSM and configure the scan service:
C:\Scripts> nssm install MailScan
Configure the next two tabs. Configure in "Arguments" the folder where the script is stored.
When all is entered correctly press the "Install Service" button. We can start the service:
C:\Scripts> net start MailScan
Now make a scan and check your mailbox!
No comments :
Post a Comment