HTML5 form using PHP, email not sending

Hello All,

I have created the following HTML5 form (some wording changed for security reasons), I am posting to test.php. I go over to XAMPP and launch my Form which in turn goes to PHP code. I receive message ‘Thank you, your request has been sent!’ but no email is being received. I am not sure what the problem is - my server is setup for SMTP port = 25 and my domain is company name. Any thoughts? Code below…


<head>
<meta charset="UTF-8">
<title>Transfer Form</title>
<link href="css/goods.css" rel="stylesheet" type="text/css">

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('a').click(function() {
        $('#myTable tbody').append('<tr class="child"><td><input type="text" name="stockNumber" class="stocktext"  id="stockNumber" /></td><td><input type="text" name="serialNumber" class="serialtext" id="serialNumber" /></td><td><input type="text" name="description" class="descripttext" id="description"/></td></tr>');

});
});
</script>

<script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
  </script>
</head>

<body>

<form method="POST" action="test.php">

<fieldset>
<legend class="legend"> Transfer Form</legend>

<p class="currentdate">Current Date:
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
//-->
</script></p>

<p>
<table class="table">
    <tr>
      <td><span class="requiredtable">*</span> <span class="fieldtext">Stock Number</span></td>
      <td><span class="requiredtable">*</span> <span class="fieldtext">Serial Number</span></td>
      <td class="descripttd"><span class="descriptiontxt"> Description</span></td>
    </tr>
  </table>

  <table id="myTable">
  <tbody>
    <tr>
      <TD> <input type="text" name="stockNumber" required class="stocktext"  id="stockNumber" tabindex="1" /> </TD>

      <TD> <input type="text" name="serialNumber" required class="serialtext" id="serialNumber" tabindex="2" /> </TD>

      <TD> <input type="text" name="description" class="descripttext" id="description" tabindex="3" /> </TD>
    </tr>
  </tbody>
</table>

 <p><span class="required">*
    </span><span class="fieldtext">Requested by:
    </span>
    <input type="text" name="Requested_by" required id="Requested_by" size="50" maxlength="500" tabindex="4">

      <span class="requestdate"><span class="required">*</span> Requested Date:
<input type="text" name="requestedDate" required id="datepicker" value="Click to Select Date" tabindex="5"></span>
  </p>

  <p><span class="required">*</span>
  <span class="fieldtext">Customer Info:</span>
        <input type="text" name="Cust Info" required id="Cust_Info" size="50" maxlength="500" tabindex="6">
  </p>

 <label for="RequestingBranch">
     <span class="required">*</span>
     <span class="fieldtext"> Requesting Branch:</span>
    </label>

<select class="select" name="R_branch" id="R_branch">
<option value="">Select One:
<option value="R_boston">Boston
<option value="R_Buffalo">Buffalo
<option value="R_Cinncinatti">Cinncinatti
<option value="R_columbia">Columbia
<option value="R_Dallas">Dallas
<option value="R_Fairfax">Fairfax
<option value="R_Kansas City">Kansas City
<option value="R_Los Angeles">Los Angeles
<option value="R_Orlando">Orlando
<option value="R_Raleigh">Raleigh
<option value="R_Toledo">Toledo
<option value="R_Topeka">Topeka
</select>

    <label for="ShippingBranch">
     <span class="required">*</span>
     <span class="fieldtext"> Shipping Branch:</span>
    </label>

   <select class="select" name="S_branch" id="S_branch">
<option value="">Select One:
<option value="S_boston">Boston
<option value="S_Buffalo">Buffalo
<option value="S_Cinncinatti">Cinncinatti
<option value="S_columbia">Columbia
<option value="S_Dallas">Dallas
<option value="S_Fairfax">Fairfax
<option value="S_Kansas City">Kansas City
<option value="S_Los Angeles">Los Angeles
<option value="S_Orlando">Orlando
<option value="S_Raleigh">Raleigh
<option value="S_Toledo">Toledo
<option value="S_Topeka">Topeka
  </select>
    <br> <br>
 <input type="submit">

    <input name="resetbutton" type="reset" id="resetbutton" value="Clear"></p>

  </fieldset>
</form>

</body>



 <?php
sleep(2);

//Sanitize incoming data and store in variable

$stockNumber =  trim(stripslashes(htmlspecialchars ($_POST['stockNumber'])));		
$serialNumber =  trim(stripslashes(htmlspecialchars ($_POST['serialNumber'])));	
$description =  trim(stripslashes(htmlspecialchars ($_POST['description'])));	
$requestedBy = trim(stripslashes(htmlspecialchars ($_POST['requestedBy'])));
$requestedDate =  trim(stripslashes(htmlspecialchars ($_POST['requestedDate'])));	
$customerInfo =  trim(stripslashes(htmlspecialchars ($_POST['customerInfo'])));	
	
// Array for the R_emails option from form
$R_emails = array(
    'R_boston' => 'boston@test.com',
	'R_buffalo' => 'buffalo@emailhere.com',
	'R_cinncinatti' => 'cinncinatti@email.com',
	'R_columbia' => 'columbia@test.com',
    'R_dallas' => 'dallas@emailhere.com',
    'R_fairfax' => 'fairfax@emailhere.com',
	'R_kansas' => 'kansas@email.com',
    'R_la' => 'la@emailhere.com',
	'R_orlando' => 'orlando@email.com',
	'R_raleigh' => 'raleigh@emailhere.com',
	'R_toledo' => 'toledo@emailhere.com',
    'R_topeka' => 'topeka@emailhere.com',
);

// get receiving email and turn in the the R_email variable
$R_email = $R_email[ $_POST['R_branch'] ];	

// Array for the S_emails option from form
$S_emails = array(
    'S_boston' => 'boston@test.com',
	'S_buffalo' => 'buffalo@emailhere.com',
	'S_cinncinatti' => 'cinncinatti@email.com',
	'S_columbia' => 'columbia@test.com',
    'S_dallas' => 'dallas@emailhere.com',
    'S_fairfax' => 'fairfax@emailhere.com',
	'S_kansas' => 'kansas@email.com',
    'S_la' => 'la@emailhere.com',
	'S_orlando' => 'orlando@email.com',
	'S_raleigh' => 'raleigh@emailhere.com',
	'S_toledo' => 'toledo@emailhere.com',
    'S_topeka' => 'topeka@emailhere.com',
		
);

// get receiving email and turn in the the S_email variable
$S_email = $S_emails[ $_POST['S_branch'] ];	

//Prepare information from form to be sent
$to = 'sparkymom78@gmail.com';
$from = 'sharepoint@company.com';
$subject = 'Online Order Request';
$headers = 'MIME-VERSION: 1.0' . '\
';
$headers .= 'From: $from' . '\
';
"CC: testing123@company.com";
$body = 'Stock Number: ' .$stockNumber . PHP_EOL;
$body .= 'Serial Number: ' .$serialNumber . PHP_EOL;
$body .= 'Description: ' .$description . PHP_EOL;
$body .= 'Requested By: ' .$requestedBy . PHP_EOL;
$body .= 'Requested Date: ' .$requestedDate . PHP_EOL;
$body .= 'Customer Info: ' .$customerInfo . PHP_EOL;

// Form data was successful so we will now send admin email and return message to the user
$success = mail( implode(',', array( $R_email, $S_email )), $subject, $body, $headers , '-f user123@company.com' );
echo 'Thank you, your request has been sent!';	

?>

Thanks in advance,
SparkyMom

Hi SparkyMom,

I’ve got a recent copy of XAMMP on my machine, and it seems that by default it’s setup to save emails to disk instead of sending them. Check for a folder, inside your XAMPP folder, called mailoutput.

Yeah, I just found them there. Now the question is how do I get them out of there? And have them actually email to correct recipients?

Thanks,
Sparkymom

Open up php/php.ini and comment out this line:

sendmail_path = "\\xampp\\mailtodisk\\mailtodisk.exe"

and uncomment this one:

sendmail_path = "\\"\\xampp\\sendmail\\sendmail.exe\\" -t"

Then open up sendmail/sendmail.ini and fill in the details for an email account that you can use to send the email.

[B]I have done what is suggested here and my emails are still getting hung up in the Apache/mailoutput folder.

Enclosed are my php.ini and sendmail.ini - What do I need to do to get the emails to actually send?[/B]

php.ini

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = ip address for exchange server here
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = sharepoint@companyname.com

; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesC:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.

; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = “\“C:\xampp\sendmail\sendmail.exe\” -t”

; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
;sendmail_path = “C:\xampp\mailtodisk\mailtodisk.exe”

sendmail.ini

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS’s “pickup” directory. (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS’s pickup directory cause sendmail to
; run quicker, but you won’t get error messages back to the calling
; application.

smtp_server=ip address for exchange server here

; smtp port (normally 25)

smtp_port=25

If your emails are still ending up in the mailoutput folder, it sounds like you’ll need to restart XAMPP for the changes in php.ini to take effect.

I restarted Xampp and even rebooted the server - emails still stuck. :frowning:

Is there something else I need to do on the server?

Are they still being saved to mailoutput? Can you check to see if you have another php.ini file in your \xampp\apache\bin folder

yes, they are still going to mail output and no I do not have another php.ini file in my \xampp\apache\bin folder

In your php.ini, try commenting out the following lines:


[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = ip address for exchange server here
; smtp_port = 25

and check if you need to add a username and password for your exchange server in sendmail.ini:


auth_username=
auth_password=

I attempted the first part with the code and it didn’t work. Waiting on Admin for second part.

I have another question, I have been trying to figure out how to validate my code. Do I put the php validation code within the HTML form? Or at the top of the send.php code? I am so confused on what to do and there is not anything real clear out there.

Then the next question is how to start it.

Thanks

It’s probably easiest to put the validation at the top of the form page. If the validation is successful, the form gets emailed and then you can display a message or redirect to another page. If the validation fails, the form is redisplayed with the values pre-filled and the error messages displayed to the user so they can correct the problem.

Here’s a basic example, showing how you could do that for a field called email.


<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $validForm = TRUE;
    $errors = array();

    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

    if (FALSE == filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $validForm = FALSE;
        $errors['email'] = "Email address is not valid";
    }

    if ($validForm) {
        // Send email
        // Redirect to thank you page
    }
}
?>

<form method="post">
    <input name="email" value="<?php echo $email ?>">
    <?php if (isset($errors['email'])): ?><p class="error"><?php echo $errors['email'] ?></p><?php endif; ?>
</form>

The standard mail function in php really sucks. In that it is quite limited in its capabilities and error handling. I would recommend using a library like SwiftMailer that provides an stmp transport. The stmp transport provides increasingly portable and predictable funcionality across different environments. Also, it provides very good feedback for errors and such.

Since I couldn’t get email to send through PHP via SMTP by making changes to my php.ini and sendmail.ini, I decided to setup PHPMailer.

After the install, I was able to successfully receive the test mail from PHPMailer BUT I’m not sure what all I need to do to get the email to send properly via my send.php code.

** I shouldn’t have to have the form setup in the PHPMailer folder to work properly - right?

My error messages:

Warning: require(class.phpmailer.php): failed to open stream: No such file or directory in C:\xampp\htdocs\ ransferform\send.php on line 3

Fatal error: require(): Failed opening required ‘class.phpmailer.php’ (include_path=‘.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\ ransferform\send.php on line 3

Not sure what PEAR has to do with PHPMailer. I have PHPMailer installed in htdocs\PHPMailer

//Top of my send.php code:

<?php 
sleep(2)/
require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "ip address"; // SMTP server

//bottom of code:
$success = mail( implode(',', array( $R_email, $S_email )), $subject, $body, $headers, '-f [email]user123@company.com[/email]' ); 
echo 'Thank you, your request has been sent!';	
	
?>

[COLOR=“#0000FF”]** I tried to add the following statement
require(“php\PEAR\class.phpmailer.php”);

** As a test, I copied class.phpmailer.php into my folder that contains my html form and send.php files and I didn’t receive any error messages but I also didn’t get the email. [/COLOR]ARRRGGGHHH… :confused:

Thanks

If your form is in the file C:\\xampp\\htdocs\ ransferform\\send.php and you’ve got PHPMailer in C:\\xampp\\htdocs\\PHPMailer, try changing the require call at the top of you form like this:

require("../PHPMailer/class.phpmailer.php");

Edit: What this does it tell require “go up one level from my current directory (transferform) and into the PHPMailer directory, and include the file class.phpmailer.php from there”.

I have tried the following and keep getting errors:

require(“xampp/htdocs/PHPMailer/class.phpmailer.php”);
require(“/PHPMailer/class.phpmailer.php”);
require(“…/PHPMailer/class.phpmailer.php”);

Do I need to make edits to the class.phpmailer?

OR is this code supposed to go into my html form at the beginning?

<?php
sleep(2)/
require(“…/class.phpmailer.php”);

$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = “ip address”; // SMTP server

?>

What did you do to receive the test email? Is it a script that’s included with PHPMailer? If so, what folder did you run it from?

I have this working without any errors

require(“…/PHPMailer/class.phpmailer.php”);

but I’m still not receiving emails, they are going into the Apache/mailoutput folder

I created a test file in the PHPMailer folder.

 
?php

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "ipaddress"; // SMTP server

$mail->From     = "sharepoint@company.com";
$mail->FromName = "user123";
$mail->AddAddress("user123@company.com");

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \
\
 This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

Can you re-post the code of your send.php file please? I expect you’re still getting mails in the mailoutput folder due to the mail() function still being called at the bottom of the file.

Edit: Nevermind, I’ve just seen your latest message. In your send.php file, change this part:


//Prepare information from form to be sent  
$to = 'sparkymom78@gmail.com'; 
$from = 'sharepoint@company.com'; 
$subject = 'Online Order Request'; 
$headers = 'MIME-VERSION: 1.0' . '\
'; 
$headers .= 'From: $from' . '\
'; 
"CC: testing123@company.com"; 


// Form data was successful so we will now send admin email and return message to the user 
$success = mail( implode(',', array( $R_email, $S_email )), $subject, $body, $headers , '-f user123@company.com' );  
echo 'Thank you, your request has been sent!'; 

for this:


$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "ipaddress"; // SMTP server

$mail->From     = "sharepoint@company.com";
$mail->FromName = "user123";
$mail->AddAddress("user123@company.com");

$mail->Subject  = "Online Order Request";
$mail->Body     = $body;
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Thank you, your request has been sent!';
}

Note: don’t forget to make sure you’ve the require("../PHPMailer/class.phpmailer.php"); line at the top of your script.