Jump to content

Why does this generate two identical emails?


Shamrox

Recommended Posts

Can anyone tell me why my code sends two identical emails to the recipient and what I'd need to change to only send one?

[code]
<?PHP

if(@$_REQUEST['form'] == "" || @$_REQUEST['registrarid'] == ""){
echo "<p>This page cannot be called directly.</p>";
exit();
}
if(@$_REQUEST['step'] != "two"){
ob_start();
if($_REQUEST['form'] == "agreement"){
include("trainingagreement.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
                 }elseif($_REQUEST['form'] == "pdfagreement"){
include("trainingagreement.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "confirmation"){
include("trainingconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "pdfconfirmation"){
include("trainingconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
include("rescheduledconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "pdfrescheduledconfirmation"){
include("rescheduledconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "reminderconfirmation"){
include("reminderconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "pdfreminderconfirmation"){
include("reminderconfirm.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "confirmation2"){
include("trainingconfirm2.php");
$to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
}elseif($_REQUEST['form'] == "po"){
include("purchaseorder.php");
$to = $vendor['vendoremail'];
}elseif($_REQUEST['form'] == "survey"){
include("studentsurvey.php");
$to = $student_contact['email'];
}elseif($_REQUEST['form'] == "invoice"){
include("invoice.php");
$to = $row_Recordset1['billemail']; //change this
}
ob_end_clean();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Send <?PHP echo $_REQUEST['form']; ?></title>
</head>

<body>

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="registrarid" value="<?PHP echo $_REQUEST['registrarid']; ?>">
<input type="hidden" name="form" value="<?PHP echo $_REQUEST['form']; ?>">
<input type="hidden" name="step" value="two">
<p>
Send <?PHP echo $_REQUEST['form']; ?> to: <input type="text" name="send_to" value="<?PHP echo $to; ?>" size="50"> (Example: [email protected], [email protected])<br>
<input type="submit" name="submit_button" value="Send <?PHP echo $_REQUEST['form']; ?>">
</p>
</form>

</body>
</html>

<?PHP
exit();
}elseif(@$_REQUEST['step'] == "two"){
ob_start();

if($_REQUEST['form'] == "agreement"){
include("trainingagreement.php");
    }elseif($_REQUEST['form'] == "pdfagreement"){
include("trainingagreement.php");
}elseif($_REQUEST['form'] == "confirmation"){
include("trainingconfirm.php");
}elseif($_REQUEST['form'] == "pdfconfirmation"){
include("trainingconfirm.php");
}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
include("rescheduledconfirm.php");
}elseif($_REQUEST['form'] == "pdfrescheduledconfirmation"){
include("rescheduledconfirm.php");
}elseif($_REQUEST['form'] == "reminderconfirmation"){
include("reminderconfirm.php");
}elseif($_REQUEST['form'] == "pdfreminderconfirmation"){
include("reminderconfirm.php");
}elseif($_REQUEST['form'] == "confirmation2"){
include("trainingconfirm2.php");
}elseif($_REQUEST['form'] == "po"){
include("purchaseorder.php");
}elseif($_REQUEST['form'] == "survey"){
include("studentsurvey.php");
}elseif($_REQUEST['form'] == "invoice"){
include("invoice.php");
}

$message=ob_get_contents();
ob_end_clean();
$success = "Email sent successfully!";
$failure = "Failed to send email!!!";
if($_REQUEST['form'] == "agreement"){
$success = "Agreement sent successfully.";
$failure = "Failed to send agreement email!!";
$from = "[email protected]";
$subject = "Training Agreement - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
}elseif($_REQUEST['form'] == "confirmation"){
$success = "Confirmation sent successfully.";
$failure = "Failed to send confirmation email!!";
$from = "[email protected]";
$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
}elseif($_REQUEST['form'] == "rescheduledconfirmation"){
$success = "Rescheduled Confirmation sent successfully.";
$failure = "Failed to send rescheduled confirmation email!!";
$from = "[email protected]";
$subject = "Rescheduled Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
}elseif($_REQUEST['form'] == "reminderconfirmation"){
$success = "Reminder Confirmation sent successfully.";
$failure = "Failed to send reminder confirmation email!!";
$from = "[email protected]";
$subject = "Reminder Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
}elseif($_REQUEST['form'] == "confirmation2"){
$success = "Confirmation sent successfully.";
$failure = "Failed to send confirmation email!!";
$from = "[email protected]";
$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
}elseif($_REQUEST['form'] == "po"){
$success = "Purchase order sent successfully.";
$failure = "Failed to send purchase order email!!";
$from = $stbrep['stb_email'];
$subject = "Purchase Order - " . $student_contact['lastname'] . " - " . $vendor['Company_Name'] . "";
}elseif($_REQUEST['form'] == "survey"){
$success = "Survey sent successfully.";
$failure = "Failed to send survey email!!";
$from = $stbrep['stb_email'];
$subject = "Survey - " . $student_contact['lastname'] . " - " . $vendor['Company_Name'] . "";
}elseif($_REQUEST['form'] == "invoice"){
$success = "Invoice sent successfully.";
$failure = "Failed to send invoice email!!";
$from = $row_Recordset1['stb_email'];
$subject = "Invoice - " . $row_Recordset1['invoicenumber'] . " - " . $row_Recordset1['studentlast'] . " - " . $row_Recordset1['coursecode'] . " - " . $row_Recordset1['coursename'] . "";
}

if ($_REQUEST['form'] == 'pdfconfirmation') {
$from = "[email protected]";
$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
$message = 'Attached you will find your confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.5555.<br /><br />
Thank you,<br /><br />
Education Services<br />
Company Name';
$success = "PDF confirmation sent successfully.";

include 'createpdf.php';

$results = true;
}
elseif ($_REQUEST['form'] == 'pdfrescheduledconfirmation') {
$from = "[email protected]";
$subject = "Reschedule Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
$message = 'Attached you will find your reschedule confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.5555.<br /><br />
Thank you,<br /><br />
Education Services<br />
Company Name';
$success = "PDF Reschedule confirmation sent successfully.";

include 'createpdfreschedule.php';

$results = true;
}
elseif($_REQUEST['form'] == "pdfagreement"){
                                $success = "PDF Agreement sent successfully.";
$failure = "Failed to send agreement email!!";
$from = "[email protected]";
$subject = "Training Agreement - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
                                $message = 'Attached you will find your agreement and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.5555.<br /><br />
Thank you,<br /><br />
Education Services<br />
Company Name';
include 'createpdfagreement.php';

$results = true;
}
elseif ($_REQUEST['form'] == 'pdfreminderconfirmation') {
$from = "[email protected]";
$subject = "Confirmation - " . $student_contact['lastname'] . " - " . $course['course_num'] . " - " . $course['ctitle'] . "";
$message = 'Attached you will find your reminder confirmation and other pertinent details to your upcoming training event.  If you have any questions regarding your enrollment, please call us toll free at 1.888.555.5555.<br /><br />
Thank you,<br /><br />
Education Services<br />
Company Name';
$success = "PDF Reminder confirmation sent successfully.";

include 'createpdfreminder.php';

$results = true;
}
else {

$send_to = $_POST['send_to'];
$message = str_replace("../invoice.css", "http://www.domain.com/invoice.css", $message);
$message = preg_replace("/\.?\.?\/images\/logo_hi-res_sm.gif/", "http://www.domain.com/images/logo_hi-res_sm.gif", $message);
//$message = str_replace("/images/logo_hi-res_sm.gif", "http://www.domain.com/images/logo_hi-res_sm.gif", $message);
$message = str_replace("/images/gray_dot.gif", "http://www.domain.com/images/gray_dot.gif", $message);
$message = str_replace("/images/shared_dot_clear.gif", "http://www.domain.com/images/shared_dot_clear.gif", $message);

$sep = "\n";
$boundary = md5(uniqid("",true));
$b1 = "b1---$boundary";
$headers  = "From: $from$sep";
$headers .= "To: $send_to$sep";
$headers .= "Return-Path: $from$sep";
$headers .= "MIME-Version: 1.0$sep";
$headers .= "Content-Type: multipart/alternative; boundary=\"$b1\"$sep";
$headers .= "$sep"; 

//now set up the message
$messageHeader = "--$b1$sep";
//default message
$defaultmessage = "Go get yourself an html mail client";
$messageHeader .= $defaultmessage.$sep;
$messageHeader .= "$sep"; 
//html message
$messageHeader .= "--$b1$sep";
$messageHeader .= 'Content-Type: text/html; charset="iso-8859-1"'.$sep;
$messageHeader .= "Content-Transfer-Encoding: 8bit$sep";
$messageHeader .= "$sep";
$messageHeader .= $message;
$messageHeader .= "$sep";
$messageHeader .= "--$b1--";
$messageHeader .= "$sep";

$results = @mail($send_to, $subject, $messageHeader, $headers);
}
if($results){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Message Sent</title>
</head>

<body>

<p><a href="dashboard-new.php">Dashboard</a> | <a href="registrar_new.php?registrarid=<?PHP echo $_REQUEST['registrarid']; ?>">Registrar</a></p>
<p><?PHP echo $success; ?></p>
<p>Message sent to: <?PHP echo $send_to; ?></p>
<p>The following message was sent:</p>
<hr>
<?PHP echo $message; ?>

</body>
</html>

<?PHP
}else{
echo $failure;
}
exit();
}
?>
<p>This page cannot be called directly.</p>


[/code]
I don't see what is causing that, but you might want to learn to use the switch function instead of all those elseifs. In fact there are some ways to really make your code much easier to write and understand. For example you could name the include file the same as the $_REQUEST['form'] value and just include it appending ".php". The first section of elseifs could be rewritten to this

[code]<?php
include($_REQUEST['form'].".php"); //this assumes files and values are named the same
switch ($_REQUEST['form']) {

    case "po":
        $to = $vendor['vendoremail'];
        break;

    case "invoice":
        $to = $row_Recordset1['billemail']; //change this
        break;

    case "survey":
        $to = $student_contact['email'];
        break;

    default:
        $to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];
        break;
}
?>[/code]

EDIT: After a closer look I see that in the most common case you are sending the email to three different addresses:
    $to = $client_contact['email'].", ".$student_contact['email'].", ".$stbrep['stb_email'];

if any of those addresses are the same, the recipient will receive multiple emails.
To find the source of your original problem you need to add some debuggin code. Just before the mail() call add this:

echo "Send To: $send_to\nSubject: $subject\nMessage $message\nHeader: $headers";

Does that section get printed twice?
Ok, odd thing just happened. I echo'd what you said, I used myself as test email address, it echo'd my address once along with the course name, etc. That seemed fine. I received one email (first time I haven't gotten two or more), but when i looked at the To: line inside the email, my address was listed twice.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.