Cliff T
Cliff T

Reputation: 227

php mail() sending two different messages on submit to different parties

I modified the following script. Everything seems to work except the person who submits the form('to2') gets an email with ('message2' - with single space lines) followed by ('message' - with double space lines).

The recipients of ('message') works as it should in that they only get ('message' - with single space lines).

MY OBJECTIVE is for ('to2') only to receive ('message2') without being followed by ('message'). I tried positioning mail('message2') string in different locations of the scripts' logic, but, I keep getting the same results or broken. Any help with logic is appreciated.

Here is the script...

<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=500">
<title>EXAMPLE</title>
</head>
<body>

<?php

if(isset($_POST['Submit'])) {
$ip_address = $_SERVER['REMOTE_ADDR'];
    $Subject = 'TEST';
    $Subject2 = 'SUCCESS';
    $A1 = $_POST['A1'];
    $Name = $_POST['Name'];
    $Phone = $_POST['Phone'];
    $Email = $_POST['Email'];
    $message = $_POST['message'];
    $message2 = $_POST['message2'];

// Type in your Email address to receive the mail
$to =  '[email protected]';
$to2 =  $Name.' <'.$Email.'>';

    if($Name == "" or $Phone == "" or $Email == "" ) {
        echo 'One or more fields has not been filled out.<br>
        Click on your browser back button once and try again.';
    }
    elseif(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
        echo 'The Email address could not be validated.<br>
        Click on your browser back button once and verify your Email address.';
    }
    else { // All checks passed

        $headers = "From: ".$Name." <".$Email.">\r\n";
        $headers .= "Bcc: [VENDOREMAILS]\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=utf-8\r\n";
        $headers2 = "From: Support <[email protected]>\r\n";
        $headers2 .= "MIME-Version: 1.0\r\n";
        $headers2 .= "Content-Type: text/html; charset=utf-8\r\n";
        $message = "
Subject: $Subject<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
";
        $message2 = "
Subject: $Subject2<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
"
.nl2br($message);
        $sendMail = mail($to, $Subject, $message, $headers);
        if($sendMail) {
            echo "THANK YOU FOR YOUR SUBMISSION";
            mail($to2, $Subject2, $message2, $headers2);
        }
        else {
            echo "An error occured and the mail could not be sent.<br>
Please try again";
        }
    }
}
else {
    header("location:example.html");
}    
?>

</body>
</html>

Upvotes: 1

Views: 467

Answers (1)

Chris Lear
Chris Lear

Reputation: 6742

Remove the code

.nl2br($message);

from the end of the definition of $message2. I think that should do it.

Upvotes: 2

Related Questions