I will admit I don't know much about PHP. I found this script to send emails and it works just fine for one of my other pages but now it isn't working and I don't know why. The only thing I changed was that I added the 'if' statements in the middle of the script to handle the emails so the emails were kept private. Anyone have any ideas?
<?php
/*if(!isset($_GET['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}*/ //This error seems to just get in the way. I am cutting it out.
$mailTo = $_GET['to'];
$subject = $_GET['subject'];
$name = $_GET['name'];
$visitor_email = $_GET['email'];
$message = $_GET['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
//If statements to change value from html to actual email addresses
switch($mailTo) {
case "ED": $mailTo = "example@example.com";
break;
case "AS": $mailTo = "example@example.org";
break;
case "OM": $mailTo = "example@example.org";
break;
case "VC": $mailTo = "example@example.org";
break;
case "Pres": $mailTo = "example@example.com";
break;
case "VP": $mailTo = "example@example.net";
break;
case "Sec": $mailTo = "example@example.com";
break;
case "Treas": $mailTo = "example@example.com";
break;
case "Dir1": $mailTo = "example@example.com";
break;
case "Dir2": $mailTo = "example@example.com";
break;
case "Dir3": $mailTo = "example@example.net";
break;
case "Dir4": $mailTo = "example@example.com";
break;
case "Dir5": $mailTo = "example@example.com";
break;
case "Dir6": $mailTo = "example@example.gov";
break;
case "Dir7": $mailTo = "example@example.com";
break;
case "Dir8": $mailTo = "example@example.com";
break;
case "Dir9": $mailTo = "example@example.com";
break;
}
$email_from = $visitor_email;//<== update the email address
$email_subject = $subject;
if($subject==="Please sign me up for the example Newsletter")
{
$email_body = "$name has requested to be subscribed to the example
Newsletter\n";
$email_body .= "$name's email address is $visitor_email\n";
}
else
{
$email_body = "*** The following message is from the user ***\n";
$email_body = $message;
}
if($message!=="")
$email_body .= "They have also added a custom message:\n $message";
$to = $mailTo;//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
if(mail($to,$email_subject,$email_body,$headers))
echo "Mail submitted successfully";
else
echo "Mail not sent";
//done. redirect to thank-you page.
//header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
In my console, I don't get any errors, I'm using AJAX and I don't get any errors, the return value is always that it was submitted successfully, and the query string that is being submitted is valid. I've narrowed it down to where it has to be something in the PHP that is messing up and making the email invalid.
Here is the JS for the AJAX function. My validation function calls the AJAX which then sends it over to the PHP. All the JS has been debugged and is working.
function ajaxFunction(caller)
{
'use strict';
console.log("1. *** BEGINNING AJAX FUNCTION ***");
var ajaxRequest;
try{
// Real browsers
ajaxRequest = new XMLHttpRequest();
} catch(e) {
// IE browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
// Something went wrong
alert("Your browser broke");
return false;
}
}
}
// Function that will recieve data sent from the server
ajaxRequest.onreadystatechange = function() {
console.log("2. The current ready state is: " + ajaxRequest.readyState);
if(ajaxRequest.readyState === 4){
var errorP = document.getElementById("errorP");
console.log("Response Text is " + ajaxRequest.responseText);
errorP.innerHTML = ajaxRequest.responseText;
}
}
// Create the date object and send it to the server
var to;
var subject;
var select = document.getElementById("select");
var subj = document.getElementById("subject");
var name = document.getElementById("name");
var nameVal = name.value;
var email = document.getElementById("email");
var emailVal = email.value;
var message = document.getElementById("message");
var messageVal = message.value;
if(caller==="newsletter")
{
to = "newsletter@al-van.org";
subject = "Please sign me up for the Al-Van Newsletter";
}
else if(caller==="contact")
{
to = select.value;
subject = subj.value;
}
var queryString = "?name=" + nameVal + "&email=" + emailVal + "&message=" + messageVal + "&to=" + to + "&subject=" + subject;
console.log("6. The query string is: " + queryString);
ajaxRequest.open("GET", "form-to-email.php" + queryString, true);
ajaxRequest.send(null);
}