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.

Example calculation with included eMail address of the recipient (in red)
Example calculation with included eMail address of the recipient (in red)

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 C:\Programs\7-PDF\PDF Printer\Macros.

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 C:\Programs.

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!

PDF Mailer Plugin by 7-PDF

If you lack VBScript knowledge and need a standard solution for integration into your reporting from WaWi, ERP, or CRM without the need for custom development, the 7-PDF Mailer Plugin is the perfect choice. By integrating it into your 7-PDF Printer, you significantly enhance its functionality. This allows the integration of external commands (control codes) for precise control of your printing processes. After successfully connecting the plugin to an instance of your 7-PDF Printer, you have the option to use these control codes in your print jobs. These codes can be inserted directly into the print text or print forms originating from systems like ERP, CRM, or WaWi. With just a few simple control codes, you can, for example, send print jobs directly via email and make individual email messages and other customizations. A comprehensive overview of all available control codes, including @@to, can be found in the subsequent documentation.

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 /SHARE.

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.

The setting 'extracttext' requires the printer driver 7-PDF Printer
The extracttext setting requires the 7-PDF Printer 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.

Top