The mailform model is used to send web forms via email. When a web visitor fills out a form you often want to send the form details to certain company representatives. This is done using mailforms. You can also define a webform to send an email to a customer or prospect.
Before creating a mailform definition refer to the help for email recipients.
Repurposing Web Forms as Email Content
One of the powerful features of forms and layouts is that they allow content to be reformatted and re-purposed. Consider the following web form used on a contact page:
Let's learn how we can send the same content entered on the form to a site owner using a mailform definition.
Changing Form Fields into Text
One of the built-in features of all forms is that you can ask for the form in "report mode". In this mode all HTML controls are replaced by the value property of the control. In this mode certain controls, such as buttons, are not shown at all. There are a couple of ways that you can change a form into report mode:
Using Config Settings
In this case we have set the config at the layout level so whenever the form is rendered using this layout the report mode option will be set.
Adding report=true to the Querystring
A second method for activating report mode is to add the report setting to the querystring.
When emailing form content you generally want to use a specialized layout so that content such as menus, banners and footers don't appear in the email content. Special layouts are supplied and used for this purpose. These layouts set the report option to true and limit the content to just the form information.
Removing Preamble and Postamble Content
Recall that all pages can define preamble and postamble text to go before and after the main page content. Often you do not want this extra information to be shown in the sent email content. This can be achieved by adding skip_preamble=true and/or skip_postamble=true to the querystring. Both of these options (as well as the optional wrapper tag) can be removed by adding skip_embellishments=true to the query string.
Let's look at the contents of the page shown earlier when we use the querystring to override the layout and skip the preamble and postamble.
Notice the HTML controls are not shown (we don't actually see the values because we are showing the form using a GET request rather than a POST request so the form fields do not have values). Also notice that some addition information about the web user has been provided by the email_to_site_owner layout.
It should be noted that we did not have to enter any values to override the layout or querystring in the mailform parameters. This is because the default layout for text only emails is text_only_email and the default layout for html emails is email_to_site_owner. Also, skip_embellishments=true is set by default, if you want to include the preamble or postamble text in the email you can supply a querystring value to allow this. For example, show_preamble=true.
Assembling Information from Multiple Forms
Although the most common use case of mailforms is to use the form that was submitted as the basis for the email content, this does not have to be the case. You may have a situation where several forms are used to collect the user input and you want to assemble all of this information in your email. In such a case you can just create a new form for the purpose of emailing the submitted content. This form can gather all of the fields and/or subforms that you want to send.
If the format of the existing forms is suitable you can even create a "super form" by simply aggregating the individual forms by using $page functions which reference the individual forms that you want to aggregate.
Configuring Mailform Email Addresses
When emails are sent, there are two important email addresses in the header of the email. One is the sender email, the address of the sender must coincide with the primary domain (mail server) under which the email was sent. Otherwise, your emails are likely going to bounce back or land in the spam folder of the recipient. For this reason, the sender email address is set in the config file of the server.
The second important email address is the reply-to. In most cases, this email address is associated with the (add-on) domain from which the email was initiated. The reply-to name and address are assigned using these mailform parameters:
These values can include dollar functions if you wish to set the values from post or session variables.
Any number of files can be sent as attachments along with the email. The files to be sent must exist on the server. The following sequence of folders are checked for the files to be attached:
Here we see an example:
First the system will resolve the dollar function. Suppose invoice number is 12345 and the site directory name is mysite.
First the system will look for file [base]/private_data/mysite/data/mailform/receipts/12345.html
If the file is not found it will check for [base]/private_data/mysite/data/receipts/12345.html
If the file is still not found, "receipts" will be passed to the site class method get_site_path and will resolve to directory [base]/private_data/mysite/receipts and the system will check for file [base]/private_data/mysite/receipts/12345.html. This third method would be used for files that are stored under the docs public_html folder. For example, Attach File name "docs/instructions.pdf" would be converted to [base]/public_html/mysite/docs/instructions.pdf.
Calling Mailform Programmatically
Let's suppose your mailform definition appears as follows:
In most cases your mailform object will be linked to a transaction object so that no programming is needed to send the email. Occasionally, you may also want to call a mailform object programmatically. If your mailform definitions contain references to session variables, you will need to make sure these are assigned before calling the send_mail method of the mailform object. Here we show an example of this: