Suppose you have a fillable PDF form and you want to make it easy for the end user to return the completed form by email. You can add a button to the form that sends the form as an email attachment to a specific email address, or multiple email addresses. You can also program the following email content:
To
Cc
Bcc
Subject
Body
Any of the above can be dynamically pulled from the form’s content or the computer system.
Examples of Dynamic Content In The Email
The recipient’s email address pulled from a dropdown list of names or departments, with the corresponding email address as the export value of the dropdown field.
The current date inserted into the subject line or the email body.
The user’s name pulled from a form field and inserted into the email body (Dear John,)
One Method For Sending a Form by Email
The submitForm( ) method can be used with the email address at the cURL parameter, and the bPDF parameter set to true:
this.submitForm({cURL:"mailto:email@info.com", bPDF: true});
The submit form method has 25 potential parameters. If bPDF is set to true, all parameters except cURL are ignored. bPDF has been deprecated for the cSubmitAs parameter:
this.submitForm({cURL: "mailto:email@info.com", cSubmitAs: "PDF"});
submitForm( ) does not accommodate cc, bcc, subject and body so it is not the preferred method for sending a form by email.
A Better Method For Sending a Form by Email
mailDoc( ) is the preferred method for sending a completed PDF fillable form by email. It takes up to six optional parameters:
bUI - This parameter determines whether the email system user interface is displayed by setting it to true or false. The default is true. A false setting will be ignored unless the method is triggered in a privileged context (console, Action, or trusted function in a folder level script). This setting is not suitable for an email button on a PDF fillable form, unless it calls a function from a folder level script installed on the user’s workstation.
cTo - A semi-colon delimited list of recipient email addresses.
cCc - A semi-colon delimited list of CC (carbon copy) recipient email addresses.
cBcc - A semi-colon delimited list of BCC (blind carbon copy) recipient email addresses.
cSubject - The subject of the email message.
cMsg - The content (body) of the email message.
Example (Parameters Named)
this.mailDoc({bUI:true, cTo:"123@hotmail.com;456@hotmail.com", cCc:"info@info.com", cBcc:"789@gmail.com", cSubject:"Email Subject", cMsg: "My application is attached. Thank you."});
Example (Parameters Unnamed)
With any Acrobat method you can exclude the curly brackets and parameter names, as long as you maintain the correct order and don’t miss any mandatory parameters. Here’s the same script with the parameter names excluded:
this.mailDoc(true, "123@hotmail.com;456@hotmail.com", "info@info.com", "789@gmail.com", "Email Subject", "My application is attached. Thank you.");
Dynamic Content In The mailDoc Method
Supposed you want the email subject to say "Please respond by (the date that is 5 days from today)", and the date is calculated automatically from the day the email was sent. First define a variable from the calculated date, then use that variable in the cSubject parameter:
var date=new Date();
date.setDate(date.getDate()+5);
var expDate=util.printd("mm/dd/yyyy", date);
this.mailDoc({cSubject:"Please respond by "+expDate, cTo:"info@email.com"});
The email address, or any other data, can be pulled from a form field:
this.mailDoc({cTo:this.getField("EmailAddress").value, cSubject:"Here's the form"});
Or a list of email addresses:
var ccc="";
var sep;
for(var i=1;i<6;i++)
{
if(i==5){sep=""}else{sep=";"}
ccc+= this.getField("Email"+i).value + sep;
}
this.mailDoc({cTo:this.getField("EmailAddress").value, cCc:ccc});
You can define variables for all your inputs if it makes sense. The email body is often long and contains line breaks, so these must be programmed into the parameter using \r or \n like this:
var body=
"Dear: "+this.getField("First Name").value +",\r\r"+
"Attached is the completed PDF form that xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx\r\r"+ "xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx\r\r"+ "xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx"+ "xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx"+ "xxxxx xxxxx\r\r"+
"Sincerely,\r\r\r\r"+
"John H Smith";
this.mailDoc({cTo:"email@info.com",cMsg:body,cSubject:"Returned Form"});
One mistake that is made frequently is the use of the word subject as a variable to define the cSubject parameter. A lot of words should not be used as variables because they are already defined in application. The word color, for example is the color object that is already defined in the application JavaScript. subject is a document property (you can see the document properties by pressing Ctrl + d). So if you define the variable subject as a string, it will change the document subject to that string. When cSubject is defined as subject in the mailDoc( ) method it will pull the document property "subject". In Acrobat Pro, it will function as intended because you can write the document subject property with a script and then pull the document property into the mailDoc( ) method dynamically. However, this will not work with Reader and an error will occur because Reader does not allow writing document properties. A simple trick to avoid using variables that might be restricted, without having to memorize all of them, is to simply remove the vowels from the word - so use sbjct instead of subject as the variable for the cSubject parameter.
Limitation
One limitation of the mailDoc( ) method is that the email will always be sent in plain text. There is no HTML option, so fancy signatures with colors and graphics are out.
Emailing Silently
A script can be used to send emails "silently", that is, without the email system interface popping up and the user having to click send. To recap, the bUI parameter must be set to false and the script must be executed from a privileged context. This is handy for form filling automation and mass emailing of completed forms, or creating an Action that emails all the forms in a folder and pulls the recipient email address from the form itself. I have already outlined how to pull the address from a form field. The email address can also be pulled from the static text of the document.
Whether emailing silently, or using an email button on a form with bUI set to true, if any parameters are dynamic, a check should be done in the script first to make sure all the information is there. For example, if email addresses are pulled from form fields, the script should check to ensure the fields are completed and warn the user if they aren’t. Email address can also be validated for correct structure. In my next post I will outline how to do this.