PDF Mailer Script: Determine the email address of the invoice recipient from your print data and send it automatically by mail
We would like to show you with our example VBScript Macro "pdfmailer.vbs" shown here, how you can send an automatic invoice print from e.g. an ERP system directly to recipients by email, and the necessary email address of the invoice recipient is obtained from the print data. The VBScript of our PDFMailer reads the required e-mail of the invoice recipient from the print data, and sends the previously generated invoice PDF fully automatically as an e-mail. The example shown here should help you to implement your requirements yourself. Depending on the requirements and circumstances, the example shown here must be adapted. But the start should be made with it. For our example a usual mail server (SMTP) is used in our script. A fictitious example calculation is used for the calculation.
Sample calculation as DOC file as well as the PDFMailer VBScript can be downloaded below for free. So you can test once yourself!
To do this, first place the PDF Mailer VBScript macro in the InstallRoot of the PDF printer and there in the Macros directory, i.e. usually under
Now it is important to adjust the SMTP credentials in the macro with an editor like Notepad++ to its mail server and save the adapted and modified VBScript file afterwards. Attention, you may need admin rights because they are located under
Then open our sample invoice with Microsoft Word to simulate an invoice printout. Here you should now adjust the recipient email address to your wishes (colored red).
Now print the sample calculation via 7-PDF Printer! The print dialog appears, you specify a PDF save path and create the PDF. At this moment an eMail will be sent to the recipient. And this is fully automatic...
The example shown here is intended to show you how you can achieve a high level of automation of your processes even with 7-PDF Printer and a few lines of VBScript code. The way to digitalization is not difficult!
Pay attention to printer driver
7-PDF Printer can be installed as a central PDF network printer in addition to a classic installation as a desktop application or a local PDF printer installation (default). The installation-parameter for this is
After installation as a PDF network printer, it may be necessary to change the printer driver selected after installation to 7-PDF Printer, as shown in the screenshot below, to extract print data as shown here. For higher compatibility, a Postscript driver of the operating system may be used. But in case of extracting print data we need direct access to it, and this is only possible if the PDF network printer instance (in the screenshot "PDF-EXP-Prt") is running under the printer driver "7-PDF Printer". So you should have a look here, that the printer instance also uses the correct printer driver.
In the macro below, the print data is read out by means of the
extracttext setting. The relevant code line here is:
Context("Config")("extracttext") = "yes"
Note: Starting with Windows 10, you must temporarily disable printer sharing for the duration of the driver change, otherwise you will receive an operating system error message.
Let's get to the actual code of our example which we have stored in the directory Macros in the VBS file pdfmailer.vbs as described above.
Rem -- global vars Dim parsed_email Rem -- This script will illustrate how to extract and process the text Rem -- of the printed output. Sub OnConfigLoaded() Rem -- MsgBox "OnConfigLoaded" Rem -- Modify the configuration to extract text from the printer Rem -- output. Context("Config")("extracttext") = "yes" End Sub Sub OnPreprocessText() Rem -- MsgBox "ProcessText" Const ForReading = 1 Dim fn, f, fso Dim l, a, c, z Rem -- Get the name of the text file from the context object fn = Context("TextFileName") parsed_email = "-" Rem -- Parse Text from PDF line by line and find the E-Mail: part! Set fso = CreateObject("Scripting.FilesystemObject") Set f = fso.OpenTextFile(fn, ForReading) While Not f.AtEndOfStream Rem -- Read line-by-line from the text inside the pdf l = trim(f.ReadLine()) Rem -- We're looking for the E-Mail: ... line here. a = Mid(l,1,7) If a = "E-Mail:" Then c = Split(l) for each z in c If z <> "E-Mail:" Then Rem -- Split line by space! Rem -- Fill "parsed_email" with email. parsed_email = z End If Next End If Wend f.Close Rem -- Clean up Set fso = Nothing End Sub Sub OnMacrosApplied Rem -- MsgBox parsed_email Rem -- Make sure the configuration isn't reloaded because of an option set selection. Context("Config")("rememberlastoptionset") = "no" Rem -- Don't like to open/show the generated pdf here after mail sending... Context("Config")("showpdf") = "no" End Sub Sub OnAfterPrint() Rem -- After the pdf is successfull generated, we send them via SMTP to Rem -- the recipient that we have parsed before... Rem -- Send mail. Context("Config")("email") = "yes" Context("Config")("emailclienttype") = "smtp" Rem -- send email to the mail address inside the pdf text! Context("Config")("emailfrom") = "FROM@YOURSERVER.COM" Context("Config")("emailto") = parsed_email Context("Config")("emailsubject") = "Your Invoice!" Rem -- we would like html as body text to format it. Context("Config")("emailbodytype") = "html" Context("Config")("emailbody") = "Attached you will find your invoice as PDF!<br><b>Regards from XYZ Company!</b>" Rem -- Send per SMTP Server Context("Config")("emailserver") = "SMTP.YOURSERVER.COM" Rem -- We use port 465 for SSL Context("Config")("emailport") = "465" Rem -- Login Data for your SMTP Server Context("Config")("emailusessl") = "yes" Context("Config")("emailuser") = "YOUR_SMTP_USERNAME" Context("Config")("emailpassword") = "YOUR_SMTP_PASSWORD" MsgBox "The invoice should have been sent by mail, now!" End Sub
Example file download
You can download the sample code (pdfmailer.vbs.zip) but also the sample invoice (invoiceexample.doc.zip) here for free.
|Download code sample (pdfmailer.vbs.zip)||1.90 KB|
|Download sample invoice (invoiceexample.doc.zip)||20.10 KB|