• Report an IT Security Incident
  • Request Help
  • Help & Support
    • Working/Learning Remotely
    • Request Help
    • Live Chat
    • Track a Ticket
    • Knowledge Base
    • Getting Started
    • Contact Us
    • ITS Alerts Sign Up
  • Service Catalog
    • Accounts & Access
    • Communications & Collaboration
    • Computers & Software
    • Data, Reporting & Analytics
    • Network & Internet Services
    • Pilot Services
    • Professional Services
    • Research Computing
    • Teaching & Learning
    • University Applications
    • Web, Servers & Hosting
  • About ITS
    • Our Team
    • Strategic Plan
    • Annual Reports
    • Awards
    • Student Employment
  • Working with ITS
    • Architectural Standards Review Board
    • Projects & Project Management
    • Technology Investment Request Lifecycle
    • Governance Committees
    • Business Intelligence
    • Enterprise Data Integration Services (EDIS)
    • GMU-TV
    • IT Security Office
  • Alerts & Bulletins
    • Unplanned Outages
    • Phishing Alerts
    • Maintenance
    • Bulletins
    • Spotlights
    • After Action Reports
    • Archives
  • Report an IT Security Incident
  • Request Help
X Hide

Information Technology Services

Skip to content
  • Help & Support
    • Working/Learning Remotely
    • Request Help
    • Live Chat
    • Track a Ticket
    • Knowledge Base
    • Getting Started
    • Contact Us
    • ITS Alerts Sign Up
  • Service Catalog
    • Accounts & Access
    • Communications & Collaboration
    • Computers & Software
    • Data, Reporting & Analytics
    • Network & Internet Services
    • Pilot Services
    • Professional Services
    • Research Computing
    • Teaching & Learning
    • University Applications
    • Web, Servers & Hosting
  • About ITS
    • Our Team
    • Strategic Plan
    • Annual Reports
    • Awards
    • Student Employment
  • Working with ITS
    • Architectural Standards Review Board
    • Projects & Project Management
    • Technology Investment Request Lifecycle
    • Governance Committees
    • Business Intelligence
    • Enterprise Data Integration Services (EDIS)
    • GMU-TV
    • IT Security Office
  • Alerts & Bulletins
    • Unplanned Outages
    • Phishing Alerts
    • Maintenance
    • Bulletins
    • Spotlights
    • After Action Reports
    • Archives

Knowledge Base

Home > Help & Support > Knowledge Base > Web, Servers & Data > Web Dev > How to Set Up an Email Form

How to Set Up an Email Form

The following PHP script allows a web site owner to collect information via a web form and have that information be submitted to an email address.

Requirements

  • The web site must reside on www.gmu.edu or some other web server that has PHP enabled and the capability to send mail. This will NOT work on mason.gmu.edu.
  • The file must have a .php extension.

How to Set Up an Email Form

Step 1

Create a new file. This can be either an empty file or a page using your existing web template.

Step 2

Copy the code below into the file.

  • If you’re using Dreamweaver, be sure to do this in Code View.
  • If you are inserting the code into an existing template, make sure you put it in a place on the page where it makes sense for a Form Sent! message to appear.

Step 3

Edit the first few lines appropriately:

  • $to
  • $from
  • $subject
  • $requiredFields

Step 4

Save the file.

Step 5

Create your web form. The form’s method should be POST (as opposed to GET), and the action should point to the file you just made. Each of the form fields should have a unique name that properly describes the field.

Step 6

Upload the files to the server. To make sure the script is working do a test submit. If the form data arrives in the intended recipient’s email box, the script is working.

Source Code

<?php
/* ---------------------------------------------
EMAIL FORM by Josh Hughes (josh@deaghean.com)
Free to use and adjust as needed
--------------------------------------------- */

/* Change these values as needed ($to determines who receives the form data) */

$to = "username@gmu.edu";
$from = "do_not_reply@gmu.edu";
$subject = "Email Form";

/* If you'd like to make sure certain fields have been filled out,
enter a comma-delimited list of required field names.
Example: $requiredFields = "field_name1, field_name2"; */

$requiredFields = "";

/* ------- ADVANCED EDITING BELOW THIS LINE ------- */

/* Make sure a form was submitted */

if (!$_POST)
$missingFields = true;
else
$missingFields = false;

/* Check required fields */

if ((trim($requiredFields) != '') and ($missingFields != true))
{
$checkFields = explode(',', $requiredFields);
for ($i = 0; $i < count($checkFields); $i++)
{
if (trim($_POST[trim($checkFields[$i])]) == '')
$missingFields = true;
}
}

/* If there are missing fields, print an error */

if ($missingFields)
{
print "<h1>Missing Fields</h1>
<p>Please go back and fill out all of the required fields.</p>";
}

else
{
$from = sprintf("From: %s", $from);
$message = "Submitted Form Values:\n\n";

/* The message is a list in the following format:
Field_name: Value of the Field */

foreach($_POST as $key => $value)
$message .= sprintf("%s: %s\n\n", $key, htmlentities($value));

/* Send the message */

$success = mail($to, $subject, $message, $from);

/* Print a success or error message */

if ($success)
print "<h1>Form Sent!</h1>
<p>Thank you for your submission!</p>";
else
print "<h1>Error!</h1>
<p>The form was <strong>NOT</strong> sent! There seems to be some sort of malfunction.</p>";
}
?>

Last modified: September 25, 2020

Previous

How to Password Protect a Directory

Next

How to Request a Virtual Host (Shorter Web Address)

Related articles

  • How to Install PuTTY SSH for Windows
  • How to Set Up a Mason Website
  • What is the mason.gmu.edu server?
  • How to Upload Files with CyberDuck (for Mac)
  • What is the www.gmu.edu server?

Get Started at Mason

Whether you are a student, faculty, staff member, or a guest visiting the Mason campus, we have resources to help get you started.

Read More

Quick Links

Patriot Pass Password Reset
Submit a Ticket
Track a Ticket
PuTTY
Cisco AnyConnect VPN

Read More

Explore IT Services

A B C D E F G
H I J K L M N
O P Q R S T U
V W X Y Z #

© 2022 George Mason University
4400 University Drive
Fairfax, Virginia 22030

Latest tweet

George Mason ITS Follow

The official Twitter page for George Mason University's Information Technology Services

GeorgeMasonITS
George Mason ITS @georgemasonits ·
23 Jan

After October 14, 2025, Microsoft will no longer provide security updates or support for Windows 10 systems. As a result, all Windows 10 systems must be upgraded to Windows 11 or removed from the Mason network. Learn more ⬇️

https://its.gmu.edu/bulletins/microsoft-windows-11-announcement/

Reply on Twitter 1617560494176620547 Retweet on Twitter 1617560494176620547 Like on Twitter 1617560494176620547 Twitter 1617560494176620547
View All
ITS Support Center
703-993-8870
support@gmu.edu
live chat
Monday – Thursday, 8 a.m. – 9 p.m.
Friday, 8 a.m. – 5 p.m.
Saturday, Closed
Sunday, 1 p.m. – 9 p.m.
(closed Thursday 3 p.m. – 4 p.m. for a team meeting) Modified Winter Hours Monday –Thursday,
8 a.m. – 7 p.m. Friday, 8 a.m. – 5 p.m. Saturday & Sunday,
Closed Normal business hours will resume on Sunday, January 22 at 1:00 p.m.
  • Webmaster