HTML Email Sending Form

I got this supposed php script from online, and made changes, but when I go to press submit I get an invalid Email address error. I know nothing about PHP, what am I doing wrong?

<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email="tech@wadsley.us")) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
 
If you are on a unix hosting platform then you can use a formmail script. Here is the one that I use: http://www.scriptarchive.com/formmail.html

It's pretty easy to set up and will do what you want to do. It will not open an email client.

You may want to check with your web host as well. They may have a formmail option built in to your web host control panel.
 
I use a HTML form setup, then pass the form data to a PHP file.

This what the code for my php file looks like:
PHP:
<?
// declare values
$department = $_POST['department'];
$technicalIssue = $_POST['technicalissue'];
$customerService = $_POST['customerservice'];
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$phoneNumber = $_POST['phonenumber'];
$contactEmail = $_POST['email'];
$contactMethod = $_POST['contact'];
$accountNumber = $_POST['accountnumber'];
$contactSubject = $_POST['Subject'];
$contactMessage = $_POST['question'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );

// where to send e-mail to
$to = 'jason.johnson@toplevelcomputers.com';

// e-mail subject
$subject = "Message submitted from Contact Us form";

// e-mail message
$message = "You have received a contact message:\r\n"

."----------------------------------------------------------------\r\n"
."Contact First Name: $firstName\r\n"
."Contact Last Name: $lastName\r\n"
."Contact Phone Number: $phoneNumber\r\n"
."Contact Account Number: $accountNumber\r\n"
."Contact's Prefered Method of Contact: $contactMethod\r\n"
."Submitted: $mydate\r\n"
."Department: $department\r\n"
."Issue: $technicalIssue\r\n"
."Customer Service: $customerService\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contactMessage\r\n";

$headers = "From: $firstName $lastName <$contactEmail>\n"
."Reply-To: $contactEmail\n"
."X-Mailer: PHP/".phpversion();

mail( $to, $subject, $message, $headers );
echo "<h3>Message Sent!</h3><p>Dear $firstName,<br /><br />We will get back to you as soon as possible using $contactEmail or $phoneNumber.</p>";
?>

This is the contact form:
http://www.toplevelcomputers.com/contactus.html

Code for Contact Us form:
Code:
<form method="post" action="includes/sendemail.php">
<table width="700px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="250" align="right">Department to send the E-mail to :</td>
    <td width="450" height="30px" align="left">
      <label>
        <input type="radio" name="department" value="Customer Service" id="customerservice" />
        Customer Service</label>
      <label>
        <input type="radio" name="department" value="Technical Gurus" id="technicalgurus" />
       Technical Gurus</label>
      <label>
        <input type="radio" name="department" value="Sales" id="sales" />
        Sales</label>
    </td>
  </tr>
  </table>
<table width="700px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="250" align="right">First Name :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="firstname" type="text" size="30" maxlength="50" class="contactFields" /></td>
  </tr>
  <tr>
    <td width="250" align="right">Last Name :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="lastname" type="text" size="30" maxlength="50" class="contactFields" /></td>
  </tr>
  <tr>
    <td width="250" align="right">Phone Number :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="phonenumber" type="text" size="30" maxlength="14" class="contactFields" onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);" />&nbsp;</td>
  </tr>
  <tr>
    <td width="250" align="right">E-Mail :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="email" type="text" size="30" maxlength="50" class="contactFields" />&nbsp;</td>
  </tr>
  <tr>
    <td width="250" align="right">Prefered Method Of Contact :</td>
    <td width="450" height="30px" align="left">
      <label>
        <input type="radio" name="contact" value="Phone" id="phone" class="contactFields" />
        Phone
      </label>
      <label>
        <input type="radio" name="contact" value="Email" id="email" class="contactFields" />
        Email
      </label>
    </td>
  </tr>
  <tr>
    <td width="250" align="right">Account # :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="accountnumber" type="text" size="30" maxlength="8" class="contactFields" />
    </td>
  </tr>
  <tr>
    <td width="250" align="right">Question :</td>
    <td width="450" height="30px" align="left">&nbsp;<textarea name="question" cols="60" rows="8"  class="contactFields"></textarea>
    </td>
  </tr>
  <tr>
    <td width="250" align="right"></td>
    <td width="450" height="30px" align="left"><input name="" type="submit" value="Submit" class="contactFields" /><input name="" type="reset" class="contactFields" />
    </td>
  </tr>
</table>
</form>
 
I use a HTML form setup, then pass the form data to a PHP file.

This what the code for my php file looks like:
PHP:
<?
// declare values
$department = $_POST['department'];
$technicalIssue = $_POST['technicalissue'];
$customerService = $_POST['customerservice'];
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$phoneNumber = $_POST['phonenumber'];
$contactEmail = $_POST['email'];
$contactMethod = $_POST['contact'];
$accountNumber = $_POST['accountnumber'];
$contactSubject = $_POST['Subject'];
$contactMessage = $_POST['question'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );

// where to send e-mail to
$to = 'jason.johnson@toplevelcomputers.com';

// e-mail subject
$subject = "Message submitted from Contact Us form";

// e-mail message
$message = "You have received a contact message:\r\n"

."----------------------------------------------------------------\r\n"
."Contact First Name: $firstName\r\n"
."Contact Last Name: $lastName\r\n"
."Contact Phone Number: $phoneNumber\r\n"
."Contact Account Number: $accountNumber\r\n"
."Contact's Prefered Method of Contact: $contactMethod\r\n"
."Submitted: $mydate\r\n"
."Department: $department\r\n"
."Issue: $technicalIssue\r\n"
."Customer Service: $customerService\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contactMessage\r\n";

$headers = "From: $firstName $lastName <$contactEmail>\n"
."Reply-To: $contactEmail\n"
."X-Mailer: PHP/".phpversion();

mail( $to, $subject, $message, $headers );
echo "<h3>Message Sent!</h3><p>Dear $firstName,<br /><br />We will get back to you as soon as possible using $contactEmail or $phoneNumber.</p>";
?>

This is the contact form:
http://www.toplevelcomputers.com/contactus.html

Code for Contact Us form:
Code:
<form method="post" action="includes/sendemail.php">
<table width="700px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="250" align="right">Department to send the E-mail to :</td>
    <td width="450" height="30px" align="left">
      <label>
        <input type="radio" name="department" value="Customer Service" id="customerservice" />
        Customer Service</label>
      <label>
        <input type="radio" name="department" value="Technical Gurus" id="technicalgurus" />
       Technical Gurus</label>
      <label>
        <input type="radio" name="department" value="Sales" id="sales" />
        Sales</label>
    </td>
  </tr>
  </table>
<table width="700px" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="250" align="right">First Name :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="firstname" type="text" size="30" maxlength="50" class="contactFields" /></td>
  </tr>
  <tr>
    <td width="250" align="right">Last Name :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="lastname" type="text" size="30" maxlength="50" class="contactFields" /></td>
  </tr>
  <tr>
    <td width="250" align="right">Phone Number :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="phonenumber" type="text" size="30" maxlength="14" class="contactFields" onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);" />&nbsp;</td>
  </tr>
  <tr>
    <td width="250" align="right">E-Mail :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="email" type="text" size="30" maxlength="50" class="contactFields" />&nbsp;</td>
  </tr>
  <tr>
    <td width="250" align="right">Prefered Method Of Contact :</td>
    <td width="450" height="30px" align="left">
      <label>
        <input type="radio" name="contact" value="Phone" id="phone" class="contactFields" />
        Phone
      </label>
      <label>
        <input type="radio" name="contact" value="Email" id="email" class="contactFields" />
        Email
      </label>
    </td>
  </tr>
  <tr>
    <td width="250" align="right">Account # :</td>
    <td width="450" height="30px" align="left">&nbsp;<input name="accountnumber" type="text" size="30" maxlength="8" class="contactFields" />
    </td>
  </tr>
  <tr>
    <td width="250" align="right">Question :</td>
    <td width="450" height="30px" align="left">&nbsp;<textarea name="question" cols="60" rows="8"  class="contactFields"></textarea>
    </td>
  </tr>
  <tr>
    <td width="250" align="right"></td>
    <td width="450" height="30px" align="left"><input name="" type="submit" value="Submit" class="contactFields" /><input name="" type="reset" class="contactFields" />
    </td>
  </tr>
</table>
</form>

I appreciate the coding, but am having trouble adapting that to my HTML I have set up. How do I define the variables in my HTML for my PHP?
 
I appreciate the coding, but am having trouble adapting that to my HTML I have set up. How do I define the variables in my HTML for my PHP?

HTML is client-side only. PHP is server-side, which is why when you view a PHP file you don't see the PHP code, you see the HTML code that PHP passed along.

You need to create a file called "sendmail.php" paste the PHP code in that.

Create a HTML file, and post the HTML file in that. Change the following:
Code:
action="includes/sendemail.php"
to where the file is located. I keep processing files in my includes folder, so you can do the same, or put it in your root folder, upto you.

Edit: Read that wrong.

The variables I use for PHP are for the PHP only.

PHP:
$department = $_POST['department'];
This is defining a variable.
PHP:
$department
is the variable.

PHP:
$_POST['department']
Is saying "Use the information that was posted in the 'department' field from the form."

Create an HTML form, and define the name. Once you have that, create your variables. If your passing information from a form to a PHP file, use $_POST, if your using a PHP file and passing it to itself, change it $_GET

PHP:
<input name="first_name" type="text" size="30" maxlength="50" class="contactFields" />
You'll want to create a PHP variable off of the name of the input field. Something like:
PHP:
$firstname = $_POST['first_name'];
I don't use capitalization in PHP and HTML unless I have to as PHP is case sensitive, so if you accidentally wrote:
PHP:
."Contact First Name: $firstName\r\n"
It wouldn't work as the variable defined is:
PHP:
$firstname
not
PHP:
$firstName
 
Last edited:
I'm with you to that point. How do I define the ids in the HMTL?

This is the HTML I have for my form, I thought I had the ID's defined, but I guess not.

I can't tell you how much I appreciate your help. I understood bits and pieces of this, but now its all coming together :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Business.css" />
<title>Wadsley Tech Services Appointments</title>
<meta name="title" content="Wadsley Tech Services" />
<meta name="keywords" content="computer help, computer geek, PC technician, James Wadsley, computer repair, Wadsley Tech Services" />
<meta name="description" content="PC Consulting by James Wadsley" />
<meta name="author" content="James M Wadsley" />
</head>
<body>
<div id="container">
<center>
<img src="Newlogo-4.jpg" width="50%" alt="Logo"/>
</center>

<ul id="nav">
<li><a href="index.html">Home</a></li>
<li><a href="Services.html">Services</a></li>
<li><a href="Contact.html">Contact</a></li>
<li><a href="Appointment.html">Appointments</a></li>
</ul>
<center>
<h1>Make An Appointment</h1>
<h2>Please fill in all areas of the form for a faster response</h2>
<form method="post" action="mail1.php">
<h3>

First Name: <Input type="text" name="firstname" />&nbsp;&nbsp;&nbsp;&nbsp;Last Name: <Input type="text" name="lastname" />&nbsp;&nbsp;&nbsp;&nbsp;
Contact # / Email: <Input type="text" name="number" />&nbsp;&nbsp;&nbsp;&nbsp;Best Time to Contact: <Input type="text" name="time" /><br><br>

Type of Problem:&nbsp;&nbsp;<Input type="checkbox" name="problem" value="Computer" />Computer&nbsp;<Input type="checkbox" name="problem" value="Perephial" />Mouse, Keyboard, External Hard Drive, Etc&nbsp;<Input type="checkbox" name="problem" value="Internet" />Internet Connection&nbsp;<Input type="checkbox" name="problem" value="Printer" />Printing Issues&nbsp;<Input type="checkbox" name="problem" value="Other" />Other<br><br>

Priority:&nbsp;&nbsp;<Input type="radio" name="priority" value="High" />High&nbsp;<Input type="radio" name="priority" value="Medium" />Medium&nbsp;<Input type="radio" name="priority" value="Low" />Low<br><br>

<p>
Description of Problem:
</p>
<textarea rows="10" cols="30"></textarea>
<br>
<br>
<input type="submit" value="submit">
<input type="reset" value="reset"
</h3>
</form>
 
Although I would still like to know how to do this, it appears Godaddy thinks this message is Spam. Here is the reply I get

Warning: mail() [function.mail]: SMTP server response: 554 The message was rejected because it contains prohibited virus or spam content in D:\Hosting\7203825\html\business\sendmail.php on line 37

Message Sent!
Dear Jane,

We will get back to you as soon as possible using or none.


Am I out of luck on this?
 
Although I would still like to know how to do this, it appears Godaddy thinks this message is Spam. Here is the reply I get

Warning: mail() [function.mail]: SMTP server response: 554 The message was rejected because it contains prohibited virus or spam content in D:\Hosting\7203825\html\business\sendmail.php on line 37

Message Sent!
Dear Jane,

We will get back to you as soon as possible using or none.


Am I out of luck on this?

Talk to your hosting provider and see what's going on.

What does your PHP file look like? For me, line 37 is a blank line.
 
It's probably the domain listed in the headers. Try this:

Code:
$headers = "From: $firstName $lastName <[COLOR=Red][B]tech@wadsley.us[/B][/COLOR]>\n"
."Reply-To: <$contactEmail>\n"
."X-Mailer: PHP/".phpversion();
I'm assuming wadsley.us is your domain.
 
It's probably the domain listed in the headers. Try this:

Code:
$headers = "From: $firstName $lastName <[COLOR=Red][B]tech@wadsley.us[/B][/COLOR]>\n"
."Reply-To: <$contactEmail>\n"
."X-Mailer: PHP/".phpversion();
I'm assuming wadsley.us is your domain.

Why would he put his own email address? It was coded with $contactEmail so that the email would be sent from the user, and replied to if need be.
 
I use a HTML form setup, then pass the form data to a PHP file.

This what the code for my php file looks like:
PHP:
<?
// declare values
$department = $_POST['department'];
$technicalIssue = $_POST['technicalissue'];
$customerService = $_POST['customerservice'];
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$phoneNumber = $_POST['phonenumber'];
$contactEmail = $_POST['email'];
$contactMethod = $_POST['contact'];
$accountNumber = $_POST['accountnumber'];
$contactSubject = $_POST['Subject'];
$contactMessage = $_POST['question'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );

// where to send e-mail to
$to = 'jason.johnson@toplevelcomputers.com';

// e-mail subject
$subject = "Message submitted from Contact Us form";

// e-mail message
$message = "You have received a contact message:\r\n"

."----------------------------------------------------------------\r\n"
."Contact First Name: $firstName\r\n"
."Contact Last Name: $lastName\r\n"
."Contact Phone Number: $phoneNumber\r\n"
."Contact Account Number: $accountNumber\r\n"
."Contact's Prefered Method of Contact: $contactMethod\r\n"
."Submitted: $mydate\r\n"
."Department: $department\r\n"
."Issue: $technicalIssue\r\n"
."Customer Service: $customerService\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contactMessage\r\n";

$headers = "From: $firstName $lastName <$contactEmail>\n"
."Reply-To: $contactEmail\n"
."X-Mailer: PHP/".phpversion();

mail( $to, $subject, $message, $headers );
echo "<h3>Message Sent!</h3><p>Dear $firstName,<br /><br />We will get back to you as soon as possible using $contactEmail or $phoneNumber.</p>";
?>

I hate to throw a spanner in the works, but you're REALLY going to want to sanitise that user input. Using raw user input is just asking for trouble, and definitely the quickest way to get your website hacked.

Take a look at http://net.tutsplus.com/tutorials/php/sanitize-and-validate-data-with-php-filters/ if you're running PHP 5.2+

Plus, I'd wrap the message sent notice in an if statement to check that it actually was sent... Something like:

PHP:
if(mail( $to, $subject, $message, $headers ))
{
	echo "<h3>Message Sent!</h3><p>Dear $firstName,<br /><br />We will get back to you as soon as possible using $contactEmail or $phoneNumber.</p>"; 
}
else
{
	echo "<h3>Error!</h3><p>Your message was not sent. Please try again.</p>";
}
 
I hate to throw a spanner in the works, but you're REALLY going to want to sanitise that user input. Using raw user input is just asking for trouble, and definitely the quickest way to get your website hacked.

Take a look at http://net.tutsplus.com/tutorials/php/sanitize-and-validate-data-with-php-filters/ if you're running PHP 5.2+

Plus, I'd wrap the message sent notice in an if statement to check that it actually was sent... Something like:

PHP:
if(mail( $to, $subject, $message, $headers ))
{
	echo "<h3>Message Sent!</h3><p>Dear $firstName,<br /><br />We will get back to you as soon as possible using $contactEmail or $phoneNumber.</p>"; 
}
else
{
	echo "<h3>Error!</h3><p>Your message was not sent. Please try again.</p>";
}

It's been in the works of being reworked, I never really put effort into sanitizing it since it's not an extremely popular website. There's nothing of value to be lost if something does happen as all of my client data is stored off site, so I'm not worried.
 
I changed my hosting over to Linux, and it appears to have worked but the email never comes thru to my account.

Def something wrong with my coding, but no idea what since I am brand new to this PHP thing
 
Back
Top