Clicky

20160828

Print PDF email attachements with hMailserver and Sumatra PDF

There are situations that you send an email with one purpose only and that is to print a PDF attachment.

In this case we have created a hMailserver script that:
- Checks for a specific email address ("pdfprint@youremaildomain.com");
- Checks a specific Subject field ("[pdfprint]");
- Checks for PDF attachment and if there is one, extract and print it.

To print the PDF, you need to have an app that renders PDFs and prints them. We are using Sumatra PDF, which has a command line interface that exactly does the job.

Steps:
  1. Create a new hMailserver user that will be used as a stub to receive the PDF print emails. Enter a long random password and forget it;
  2. Install Sumatra PDF. Take care of the location of the x86 or x64 version;
  3. Modify Eventhandler.vbs of hMailserver with the code below.
Notes:
  • Emails to "pdfprint@youremaildomain.com" will be discarded (not stored). You will see:
    "APPLICATION"    2284    "2016-08-28 08:48:37.699"    "SMTPDeliverer - Message 119900: Message deleted. Action was taken by script subscribing to OnDeliveryStart."
    " in the hMailserver eventlog;
  • You can address multiple printers (e.g. 2-side, b/w, color)) by adding additional Subject field markers and catch those in the script. Read the Samatra PDF documentation how to connect to specific printers;
  • You can add other attachment extensions (e.g. PNG, BMP, JPG, TXT, DOCX etc.) but then you need an app with a command line interface that is able to print the (extracted) file;
Tips:
  • You can set a rule to forward your emails that have the PDF print Subject marker ("[pdfprint]"). The email will stay in your email box while the PDF attachments are printed anyway
  • Now you can use even your tablet (Android, iPad) or Smartphone (BB, WP8, WP10, Android, iPhone) to print PDF right from your email app;
  • You can have much more checks like attachment size and filename , sender's email domain, etc. Let your fantasy flow!
---8<-------------------------
   Sub OnDeliveryStart(oMessage)
 
        dim wsh, fso, i
        dim AttachmentName, AttachmentType, tempAttachementName, tempFolder, objAttachement
        set wsh=CreateObject("wscript.shell")
        set fso=CreateObject("scripting.filesystemobject")


        tempFolder=fso.GetSpecialFolder(2)
  
        if oMessage.Attachments.Count > 0 then

            For i = 0 To oMessage.Attachments.Count-1

                set objAttachement = oMessage.Attachments(i)

                AttachmentName=tempFolder & "\" & objAttachement.Filename
                objAttachement.SaveAs(AttachmentName)
              
                AttachmentType=fso.GetExtensionName(AttachmentName)
                Select Case lcase(AttachmentType)
                Case "pdf"
                    Call WriteLogfile("Processing PDF attachment: " & AttachmentName)
                  
                    if lcase(oMessage.To)    =    "pdfprint@yourdomain.com"         and _
                        Instr(lcase(oMessage.Subject),"[pdfprint]") > 0      then                       
                        Call WriteLogFile("Printing PDF file for: " & oMessage.From & ", PDF file:'" & AttachmentName)
                        s="""C:\Program Files\SumatraPDF\SumatraPDF.exe"" -print-to-default " & chr(34) & AttachmentName & chr(34) & " -silent -exit-when-done"
                        wsh.run s,0,true
                        Call WriteLogfile("PDF print message processed.")                      
                    end if
                    fso.DeleteFile AttachmentName ,true
                End Select  
            next  
        end if

result.value=1   
End Sub
---8<-------------------------
   

2 comments :

  1. Hi. where is the sub for the WriteLogFile?

    ReplyDelete
    Replies
    1. Sub WriteLogFile(s)
      on error resume next

      const LogFile="c:\log\hMailserverEvents.log"

      set fso=CreateObject("scripting.filesystemobject")
      if not fso.FileExists(LogFile) then fso.CreateTextFile LogFile,1

      set f=fso.OpenTextFile(LogFile,8,false)
      if ERR=0 then
      f.writeline now() & " - " & s
      f.close
      end if
      on error goto 0

      End Sub

      Delete

Real Time Web Analytics