-2

I have looked for a solution to my problem all over but cant seem to get it to work.

I have a form that gets data from the user and then emails it to me when they click submit.

I would also like them to receive an confirmation email to the address they provided, and this is where I am stuck.

If the form is required please ask and I will send, but here is the PHP...

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "webhost@compumodsa.com";
$email_subject = "CompuMod Web-site Order";

function died($error) {

// your error code can go here
echo "Something was entered incorrectly…";
echo "Please correct the following.<br /><br />";
echo $error."<br /><br />";
echo "After correcting, please try again.<br /><br />";
die();
}

// validation expected data exists
if(
!isset($_POST['site']) ||
!isset($_POST['page']) ||
!isset($_POST['domain']) ||
!isset($_POST['host']) ||
!isset($_POST['service']) ||
!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['company']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['idnumber']) ||
!isset($_POST['address']) ||
!isset($_POST['suburb']) ||
!isset($_POST['city']) ||
!isset($_POST['province']) ||
!isset($_POST['postcode']) ||
!isset($_POST['comments'])) {
died('Something was entered incorrectly…'); 
}

$site = $_POST['site']; // required
$page = $_POST['page']; // required
$domain = $_POST['domain']; // required
$host = $_POST['host']; // required
$service = $_POST['service']; // required
$firstname = $_POST['firstname']; // required
$lastname = $_POST['lastname']; // required
$company = $_POST['company']; // not required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$idnumber = $_POST['idnumber']; // required
$address = $_POST['address']; // required
$suburb = $_POST['suburb']; // not required
$city = $_POST['city']; // required
$province = $_POST['province']; // required
$postcode = $_POST['postcode']; // required
$comments = $_POST['comments']; // not required

$error_message = "";
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$site)) {
$error_message .= 'Your “Base Site Design” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$page)) {
$error_message .= 'Your “Extra Pages” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$domain)) {
$error_message .= 'Your “Domain Registration” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$host)) {
$error_message .= 'Your “Host Server Size” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$service)) {
$error_message .= 'Your “Service Package” was not Selected.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'Your “First Name” was not entered or entered incorrectly.<br />';
}
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'Your “Last Name” was not entered or entered incorrectly.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'Your “E-Mail” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$telephone)) {
$error_message .= 'Your “Phone/Cell” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$idnumber)) {
$error_message .= 'Your “SA ID Number” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$address)) {
$error_message .= 'Your “Street Address” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$city)) {
$error_message .= 'Your “City” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$province)) {
$error_message .= 'Your “Povince” was not entered or entered incorrectly.<br/>';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$postcode)) {
$error_message .= 'Your “Postal Code” was not entered or entered incorrectly.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Client request details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

//request email variables
$email_message .= "Base Site Design: - ".clean_string($site)."\n";
$email_message .= "Extra Pages: - ".clean_string($page)."\n";
$email_message .= "Domain Registration: - ".clean_string($domain)."\n";
$email_message .= "Host Server Size: - ".clean_string($host)."\n";
$email_message .= "Service Package: - ".clean_string($service)."\n";
$email_message .= "First Name: - ".clean_string($firstname)."\n";
$email_message .= "Last Name: - ".clean_string($lastname)."\n";
$email_message .= "Company: - ".clean_string($company)."\n";
$email_message .= "E-Mail: - ".clean_string($email)."\n";
$email_message .= "Phone/Cell: - ".clean_string($telephone)."\n";
$email_message .= "SA ID Number: - ".clean_string($idnumber)."\n";
$email_message .= "Address: - ".clean_string($address)."\n";
$email_message .= "Suburb: - ".clean_string($suburb)."\n";
$email_message .= "City: - ".clean_string($city)."\n";
$email_message .= "Province: - ".clean_string($province)."\n";
$email_message .= "Postal Code: - ".clean_string($postcode)."\n";
$email_message .= "Extra Requests: - ".clean_string($comments)."\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);

//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email, $subject, $email_message, $headers);

header('Location: http://www.compumodsa.com/index.php/component/k2/item/21');

?>
<!-- include your own success html here --> 
<?php
}
?>

The form details gets emailed to me with...

//request email variables
$email_message .= "Base Site Design: - ".clean_string($site)."\n";
$email_message .= "Extra Pages: - ".clean_string($page)."\n";
$email_message .= "Domain Registration: - ".clean_string($domain)."\n";
$email_message .= "Host Server Size: - ".clean_string($host)."\n";
$email_message .= "Service Package: - ".clean_string($service)."\n";
$email_message .= "First Name: - ".clean_string($firstname)."\n";
$email_message .= "Last Name: - ".clean_string($lastname)."\n";
$email_message .= "Company: - ".clean_string($company)."\n";
$email_message .= "E-Mail: - ".clean_string($email)."\n";
$email_message .= "Phone/Cell: - ".clean_string($telephone)."\n";
$email_message .= "SA ID Number: - ".clean_string($idnumber)."\n";
$email_message .= "Address: - ".clean_string($address)."\n";
$email_message .= "Suburb: - ".clean_string($suburb)."\n";
$email_message .= "City: - ".clean_string($city)."\n";
$email_message .= "Province: - ".clean_string($province)."\n";
$email_message .= "Postal Code: - ".clean_string($postcode)."\n";
$email_message .= "Extra Requests: - ".clean_string($comments)."\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);

The conformation email witch is suppose to go to the user...

//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email, $subject, $email_message, $headers);

no email is being received by the user.

All help would be so much appreciated...

C-GuL
  • 3
  • 1
  • 6
  • If there is anything unnecessary in there please let me know to. – C-GuL Mar 16 '17 at 12:44
  • 1
    what seems to be the problem? does it work? does it mail out or not? are there any errors and are you checking for them? if the form doesn't use a POST method and that the inputs are not named, that could make your code come to a stop. – Funk Forty Niner Mar 16 '17 at 12:50
  • All the `if(isset())` part at the beginning could be replaced by putting a "required" in the HTML form. – Twinfriends Mar 16 '17 at 12:54
  • 1
    @Twinfriends No, it is correct to perform server-side validation of all inputs. Never trust the client side. – BA_Webimax Mar 16 '17 at 12:58
  • [edited] I stated the problem a little clearer... – C-GuL Mar 16 '17 at 14:36

1 Answers1

0

Not sure what you are trying to do here, but this would likely be where your problem lies...

if($_POST['email'] == "post")
{ 
    //email variables
    $subject = "Indie Rally - Welcome & Thanks!";
    $email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
    $email_message .= "A Compumod representative will contact you as soon as posable.\n";

    // create email headers
    $headers = 'From: webhost@compumodsa.com' . "\r\n" .
                'Reply-To: $email' . "\r\n" .
                'X-Mailer: PHP/' . phpversion(); 

    mail($email, $subject, $email_message, $headers); 

}

Assuming that $_POST['email'] is the email address of the person filling out the form, seeing if it equals the obviously invalid email address of "post" is either A) always going to fail the conditional check or B) pass the conditional check and pass an invalid "To:" address to the PHP mail function.

Since you have already performed the tests previously to ensure that the email address is both existing and valid, you should not require that conditional around the auto-responder functionality at all. Just send the email.

This will leave you with this...

/*
 * Notice the removal of the `if()` statement
 */
//email variables
$subject = "Indie Rally - Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

// create email headers
$headers = 'From: webhost@compumodsa.com' . "\r\n" .
            'Reply-To: $email' . "\r\n" .
            'X-Mailer: PHP/' . phpversion(); 

mail($email, $subject, $email_message, $headers); 
BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
  • Sorry to sound like a total noob (witch I am at this). There is a problem with the code, as you stated. Could you help me with a correction? – C-GuL Mar 16 '17 at 14:39
  • @C-GuL I have tried to clarify what I was saying. Look at the bottom of the answer. – BA_Webimax Mar 16 '17 at 16:28
  • I have edited the last code box on my question. Still no mail is going to the user. If I replace the @mail($email); with $email_to it sends to the webhost address. Now it seems to me like it is not getting the mail address from the input. – C-GuL Mar 17 '17 at 09:58
  • It works to ather adresses but not to a gmail address. I comes up as an unknown sender, witch mite be why the gmail account is not receiving it. – C-GuL Mar 17 '17 at 11:34