USD Logo MySanDiego | Libraries | Bookstore | Find People | A to Z Index | Resources | Jobs
 Prospective Students | Current Students | Alumni | Faculty & Employees | Visitors | International
About USDAdmissionsAcademicsNews and EventsAdministrationAthleticsGiving

Email Form

Email Form

If you have an on-line form and need it sent to your e-mail account, this PHP snippet will do it for you. Your page must be a “PHP” web page: that is, it must end in “.php”. Add the following anywhere in your PHP page:

<? $formRecipient = "youremailaddress"; include("/web/includes/email.php"); ?>

Where you set up your form, set the method to “post” and set the action to be your page itself. If your page is called “assignments.php", your action will be:

<form method="post" action="assignments.php">

Any form data that is sent via the “post” method to your page will then be e-mailed to the address you specified.

Was it sent?

You can give the reader feedback that their information has been successfully sent:

<?IF ($formSent):?> <p>Thank you for your comments.</p> <p>They have been sent to the director.</p> <?ENDIF?>

If you also want to hide your form once the message is sent, you can do so using:

<?IF ($formSent):?> <p>Thank you for commenting.</p> <?ELSE:?> <!-- your form goes here --!> <?ENDIF?>

Order of Form Data

By default, the mailer will send the data in the order that it receives it from the web browser. This may or may not be the order in which it appears on the form. If you want to specify a form order, you can do so with one of the following lines:

<? $formOrder = "alphabetical"; $formOrder = "normal"; //the default $formOrder = array("field1", "field2", "etc."); ?>

If you specify a specific form order, make sure that you include all fields. Any fields you do not specify in the order will be ignored and not sent to you.

Custom Subjects, Senders, and Bodies

You can specify a custom subject, sender, and/or a custom body using:

<? $formSubject = "custom subject"; $formBody = "custom body or file"; $formFrom = "custom sender e-mail"; ?>

If you place “%FIELDNAME%” into the custom subject or body, that will be replaced with the appropriate field’s value. If you place “_SystemInfoName_” into the custom subject or body, that will be replaced with the appropriate system info. The following system info is available:

If you choose to use a filename, the filename cannot contain spaces and the file must be owned by the same account that owns the web page.

BrowserBrowser info of respondent.
DateDate and time message was sent.
IPIP address of respondent’s computer.
MailDateDate and time in a format suitable for an e-mail.
PageURL of the web page the form is on.
ReferrerURL of the web page that referred them to this page.
SQLDateDate and time in a format suitable for a SQL database.
UnixDateUnix timestamp.
UserUsername if respondent is logged in to Single Sign On.
stampDate, page, and IP address of respondent.

For example, the following could be specified as a formBody:

<? $formBody="Using _Browser_, %FirstName% %LastName% said %Comment%."; ?>

If you want to suppress the line-by-line listing of field values, you can also specify:

<? $formNoFields = true; ?>

Make sure that you specify a custom body if you suppress the field list, or no message will be sent.

Multiple recipients

If you want to send to multiple recipients, use an array instead of a string to set $formRecipient:

<? $formRecipient = array("email1", "email2", "email3"); include("/web/includes/email.php"); ?>

POST vs. GET

By default, the mailer will ignore everything except “post” form data. If you need it to also send “get” (bookmarkable) data, add the following line above the include:

<? $formAllowGet = true; ?>

Remembering form data

Sometimes, you’ll want to remember what the reader filled out and place it back in the form, perhaps so that if they want to correct their information, they can do so.

You can do so with formValue('field name'). For example, if you have a form field called “FirstName”, you can use formValue('FirstName') to get what the reader filled out. Here is a very simple form example:

<form method="post" action="getnames.php"> Your First Name: <input type="text" name="FirstName" value="<?formValue('FirstName')?>"> <input type="submit" value="Send Me Your Name"> </form>

If you are using formAllowGet, POST data takes precedence over GET data.

Required fields

Often, you will have fields that must be filled out. If those fields are not filled out, you want a message displayed asking them to do so. Add the following to your page before you include the email.php file:

<? $formRequiredFields = array('fieldname1', 'fieldname2', 'etc.'); ?>

If a required field is empty, the mail will not be sent. The reader will be informed as to which field(s) they need to fill out. The message will appear wherever your “include” is that includes the “email.php” file (see the first step). If they did not yet submit the form or they filled out all fields correctly, there will be no message.

The message is displayed as an H2 headline followed by an unordered list. The message is enclosed in a “<div class="formWarning"> ... </div>” so that you can change the formatting in your own style sheet. Apple style changes to a “div” tag of class “formWarning” or to “h2”, “ul”, and “li” tags within a “div” tag of class “formWarning”.

Numeric fields

You can require that some field or fields be numeric by setting $formNumberFields:

<? $formNumberFields = array('fieldname1', 'fieldname2', 'etc.'); ?>

A field that must be a number can also be empty, unless of course it also appears in $formRequiredFields.

Javascript verification of required fields

If you want a message box to pop up when the reader tries to submit a form with an empty required field or with text in a numeric field, you need to use Javascript. The email.php include adds most of the necessary Javascript for you. All you need to do is add the following to your form’s <form …> tag:

onsubmit="return verifyForm(this);"

For example:

<form method="post" action="contact.php" onsubmit="return verifyForm(this);">

If you use Javascript verification, you will need to provide values for all select list items. There is an issue with Internet Explorer that requires explicity values in select lists when using Javascript to access the selected item.

Form Spam

Some spammers will try to automatically submit their web pages as links or URLs to forms in the hopes that the form automatically posts the message on a blog. Even though your form does not do such a thing, the spam spiders may still add it to their list.

If you want the final message to be ignored if it resembles form spam, add “$formBlockSpam=true;” before you include the email.php file.

  • Any message with six or more URLs in it will be completely ignored.
  • Any message that contains a phpBB-style URL (“[URL=…]”) will be completely ignored.

Please note that it checks the final message, so it will count any URLs that you place into your message as well.

The system will also drop any HTML after it checks for spam, regardless of whether the e-mail is spam, since HTML won’t show up well in a simple e-mail.

Example

Here is a very simple form with one required field and one numbered field. It uses the action “http://www.sandiego.edu/webdev/coding/email/” because that is the filename of this web page (look in your HTTP location bar to see it).

<? $formRecipient = 'jerry'; $formSubject = 'Mail regarding %Subject%'; $formRequiredFields = array('Subject'); $formNumberFields = array('Age'); $formOrder = 'alphabetical'; include('/web/includes/email.php'); ?> <?IF ($formSent):?> <p>Thank you for your comment.</p> <?ELSE:?> <form method="post" action="http://www.sandiego.edu/webdev/coding/email/" onsubmit="return verifyForm(this);"> <p> Subject: <input type="text" name="Subject" value="<?formValue('Subject');?>"><br /> Age: <input type="text" name="Age" value="<?formValue('Age');?>"><br /> Comment:<br /> <textarea rows="16" cols="50" name="Comment"><?formValue('Comment');?></textarea> <input type="submit" value="Send Message"> </p> </form> <?ENDIF?>

Comments

If you have comments or tips for readers of this page, you may post them here. Questions are more appropriately directed to the webmaster. Comment on this page