Clicky

20160417

One-click send attachement to email

Yeah, yeah, VBscript is boo, boo and should be banned from the earth. However you can create and use handy scripts without 2 or more academic titles that you need for PowerShell programming or Visual Studio. Also this script does not require any .NET library or Visual Studio runtime installed. It should run from Windows 98 to Windows 10 (W7, W8.1 and W10 were tested).

Sometimes you need to send a file to your own email account. E.g. for the file to be used on another computer, Operating System of even Smartphone. Of course you can start your email program, create a new email to yourself, attach the file and send it. Easier would it be if you just can drag-and-drop the file on an application shortcut or use the Explorer context menu for that.

This post describes a script that using the Collaboration Data Object (CDO) and Gmail to send an attachment to your own email address. I suggest you create a new "VeryComplexNeverUsedBeforeHardToGuess@gmail.com" account for this purpose.

First the script. Edit the lines with your Gmail name, password and your own email address:


    Option Explicit
    dim arg,fso, i, s, objEmail, Subject


    set arg=WScript.Arguments
    set fso=CreateObject("scripting.filesystemobject")

    const GoogleAccount    = "{here your Gmail email name}@gmail.com"
    const GooglePassword   = "{here your Gmail email password}"
    const eMailTo          = "{here you own email address"

'--- Prepare email object variables...

    Set objEmail = CreateObject("CDO.Message")
 objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")      = GoogleAccount

    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")      = GooglePassword
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")        = "smtp.gmail.com"
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl")        = 1
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")  = 1
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")         = 2
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")    = 465

'--- Add attachement(s)...
    s=vbCrLf
    for i=0 to arg.count-1
        objEmail.AddAttachment arg.item(i)
        s=s & "- " & fso.GetFileName(arg.item(i)) & vbCrLf
    next

'--- Alternative subject field?  
    Subject=InputBox("Enter subject","Alternative Subject","Attachement received " & s)
    if s="" then wscript.quit
      
'--- Populate other email fields...
    objEmail.From         = GoogleAccount
    objEmail.To           = eMailTo
    objEmail.Subject      = Subject
    objEmail.TextBody     = "Attached you find the next attachements: " & s
    objEmail.Configuration.Fields.Update

'---- Send the email...
    objEmail.Send
    if Err=0 then
        MsgBox "Successful sent attachment(s): " & s
    else
        MsgBox "Something went wrong sending attachment(s): " & s
    end if


You can use the script in different ways:
  1.     From the command line (nah...)
  2.     Drag-and-Drop a file to the shortcut of the script (nice)
  3.     With the Explorer context menu (best!)


Command line (assuming the script's name is SendAttachment-google.vbs and is in the c:\scripts folder):

    C:\> wscript c:\scripts\SendAttachment-google.vbs {path to whatever file you want to send}


Application shortcut:
Create a shortcut on the desktop to the  SendAttachment-google.vbs script. Maybe change the icon picture for better reading. Now just drag-and-drop the file you want to email to the icon.




Explorer context menu:
Create a registry file like this:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Send to email]
[HKEY_CLASSES_ROOT\*\shell\Send to email\command]
@="wscript c:\\scripts\\SendAttachement-google.vbs \"%1\""

Apply this file to the registry. Now you can right-click the file you want to email and select "Send to email":




A success/failure message is shown when done:



Notes:
  •     There is almost no error checking in the script. Most common errors are:
    •  Wrong/typo in email address or password;
    • Sending a not-allowed file type through Gmail;
    • Wrong path of the script vs. configured path in shortcut or registry.
  •     The Gmail password is in clear text. To mitigate that you can encrypt the script to a VBE file with this method. Remember to change the references from VBS to VBE.
  •     Of course you can configure your own SMTP server. When using port 25 change also objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") to 0.
  • You do not need some "Cloud Service" and client that is a available for a shorter or longer period or not available at all for your purpose.
  • You can send a maximum of 2000 emails per day, so spammers, go home!
    
    
    
    
    
    

No comments :

Post a Comment

Real Time Web Analytics