HTML5 form using PHP, email not sending

here it is, notice I changed my require path - I’m not getting any errors and email is sitting in Apache/mailoutput folder.


  <?php 
sleep(2)/
require('C:\\xampp\\htdocs\\PHPMailer\\class.phpmailer.php');

$mail = new PHPMailer();

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

//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'])));	
$R_branch = trim(stripslashes(htmlspecialchars ($_POST['R_branch'])));
$S_branch = trim(stripslashes(htmlspecialchars ($_POST['S_branch'])));

$R_branch = ucfirst(substr($R_branch, 2));
$S_branch = ucfirst(substr($S_branch, 2));
	
// 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!';	

?>


I’ve put the modified code for the whole file here: https://gist.github.com/LordZicon/68240442483f02877732

Thank you so VERY, VERY much Fretburner.

I’m finally receiving emails, I even have CC working but how do I get it to go to my 2 arrays? $R_mail and $S_mail.

I have tried a couple of ways but they don’t seem to work I even googled it.

$mail->AddAddress($R_email, $S_email);
$mail->AddCC(“user123@company.com”);

You’re welcome, I’m glad it’s working :slight_smile:

I’ve taken a quick look at the PHPMailer docs, and it looks like the second parameter for AddAddress is a ‘friendly’ name, so to add two recipients you’d have to call it twice:


$mail->AddAddress($R_email, 'Branch One'); // With a friendly name (optional)
$mail->AddAddress($S_email); // Without name

Fretburner helped me a lot, this coming from a beginner PHP developer.

I’m posting the final output to share with everyone because I could not find a good inclusive set of code anywhere to help me out. This has been a huge project with an HTML form, posting to send.php (which is attached below) using PHPMailer.


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

$mail = new PHPMailer();

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

//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'])));	
$R_branch = trim(stripslashes(htmlspecialchars ($_POST['R_branch'])));
$S_branch = trim(stripslashes(htmlspecialchars ($_POST['S_branch'])));

$R_branch = ucfirst(substr($R_branch, 2));  // removes the R_ and capitalizes first letter of branch in final output to email
$S_branch = ucfirst(substr($S_branch, 2));  // removes the S_ and capitalizes first letter of branch in final output to email
	
// 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_email[ $_POST['S_branch'] ];    
 
//Prepare information from form to be sent 
$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;
$body .= 'Requesting Branch: ' .$R_branch . PHP_EOL;
$body .= 'Shipping Branch: ' .$S_branch . PHP_EOL;
 
// Form data was successful so we will now send admin email and return message to the user
$mail = new PHPMailer();
 
$mail->IsSMTP();
$mail->Host     = "ip address"; 

$mail->From     = "sharepoint@company.com";
$mail->FromName = "Excited";
$mail->AddAddress($R_email, 'R_branch');
$mail->AddAddress($S_email, 'S_branch');
$mail->AddCC("user345@company.com");
 
$mail->Subject  = "Order Request Form";
$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!';
}
	
?>

From everything I have Googled and read about you cannot have 2 php actions in a form. I don’t want my HTML code to be any longer than it currently is so I thought it might be a great idea to incorporate the original send.php with processform.php (validation code) .

The problem is if my fields are empty when I submit the form I don’t receive any error messages and emails go out blank like so - and I receive Thank you message.
Stock Number:
Serial Number:
Description:
Etc.

I tested my processform.php by itself for the first 5 fields, which are text fields and did receive errors when blank. However, when I added the drop downs I didn’t get errors.

I thought why not try to merge the two php files together but that isn’t working either. Hmmmm – I’m at a loss and I can’t seem to attach code, I keep getting 500 Internal error code.

<?php
/*

  • BEGIN CONFIG - processform validation code
    */

// The page you want the user to be redirected if there are no errors.
$thankYouPage = ‘thanks.html’;

// Define which values we are to accept from the form. If you add additional
// fields to the form, make sure to add the form name values here.
$allowedFields = array(
‘stockNumber’,
‘serialNumber’,
‘description’,
‘requestedBy’,
‘requestedDate’,
‘custInfo’,
‘R_branch’,
‘S_branch’,
);

// Specify the required form fields. The key is the field name and the value
// is the error message to display.
$requiredFields = array(
‘stockNumber’ => ‘Stock Number is required.’,
‘serialNumber’ => ‘Serial Number is required.’,
‘description’ => ‘Description is required.’,
‘requestedBy’ => ‘Requested By is required.’,
‘requestedDate’ => ‘Please select a Requested Date.’,
‘custInfo’ => ‘Customer Information is required.’,
‘R_branch’ => ‘Please select Requesting Branch.’,
‘S_branch’ => ‘Please select Shipping Branch.’,

);

/*

  • BEGIN FORM VALIDATION
    */

$errors = array();

// We need to loop through the required variables to make sure they were posted with the form.
foreach($requiredFields as $fieldname => $errorMsg)
{
if(empty($_POST[$fieldname]))
{
$errors = $errorMsg;
}
}

// Loop through the $_POST array, to create the PHP variables from our form.
foreach($_POST AS $key => $value)
{
// Is this an allowed field? This is a security measure.
if(in_array($key, $allowedFields))
{
${$key} = $value;
}
}

/*

  • END FORM VALIDATION
    */

// Were there any errors?
if(count($errors) > 0)
{
$errorString .= ‘<ul>’;
foreach($errors as $error)
{
$errorString .= “<li>$error</li>”;
}
$errorString .= ‘</ul>’;

// display the errors on the page
?&gt;

<html>
<head>
<title>Error Processing Form</title>
</head>
<body>
<h2>Error Processing Form</h2>
<p>There was an error processing the form.</p>

&lt;p&gt;&lt;a href="transfer.html"&gt;Go Back to the Form&lt;/a&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;