Hi,
I have the following script which I use to generate a PDF and then email it out. For some reason it keeps sending the email twice. Any ideas why?
<?php
session_start(); //start using sessions
require('dbconnection.php');
?>
<STYLE TYPE='text/css'>
BODY{ font-family:sans-serif; font-size:small; font-weight:normal; }
DIV{ background-color:#FF8C8C; border-color:#FFFFFF; border-style:dashed; border-width:thin; padding:6; }
</STYLE>
<?
$quoteID = $_GET["quoteID"];
$schoolID = $_GET["schoolID"];
$timestamp=time();
$query="SELECT * FROM quotations WHERE ID='$quoteID'";
$result=mysql_query($query);
$quoteStatus=mysql_result($result,0,"Status");
$contactID=mysql_result($result,0,"clientContactID");
$clientPremium=mysql_result($result,0,"clientPremium");
$teachingFTE=mysql_result($result,0,"TeachingFTE");
$supportFTE=mysql_result($result,0,"SupportFTE");
$otherFTE=mysql_result($result,0,"OtherFTE");
$teachingDB=mysql_result($result,0,"TeachingDB");
$supportDB=mysql_result($result,0,"SupportDB");
$otherDB=mysql_result($result,0,"OtherDB");
$teachingEX=mysql_result($result,0,"TeachingED");
$supportEX=mysql_result($result,0,"SupportED");
$otherEX=mysql_result($result,0,"OtherExcess");
$maternity=mysql_result($result,0,"Maternity");
$stress=mysql_result($result,0,"Stress");
$originalStaffID=mysql_result($result,0,"Staff");
if ($quoteStatus == "Quotation") {
} else {
echo "<a href='index.php?page=module3&s=" . $schoolID . "' target='_top'>[X] Close</a><br><br>";
echo "<div><b>Error!</b> You are unable to email this quote as its status is currently: " . $quoteStatus . ".</div>" ;
die();
}
?>
<head>
<script type="text/javascript">
<!--
function delayer(){
parent.location = "index.php?page=module3&s=<?php echo $_GET["schoolID"]; ?>"
}
-->
</script>
<link href="loadingpage.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body onLoad="setTimeout('delayer()', 1)">
<span class="text"><center><img src="images/loading.gif" width="400" height="230"><br>
Sending Quote...</center></span></body>
<?
$query="SELECT * FROM contacts WHERE ID='$contactID'";
$result=mysql_query($query);
$to=mysql_result($result,0,"Email");
$saluation=mysql_result($result,$i,"Saluation");
$firstname=mysql_result($result,$i,"FirstName");
$lastname=mysql_result($result,$i,"LastName");
//check if contact is 'No Contact Set' if so clear variables.
if ($saluation == "No") {
$saluation="";
$firstname="";
$lastname="";
}
$query="SELECT * FROM clients WHERE ID='$schoolID'";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
$schoolName=mysql_result($result,0,"Name");
$address1=mysql_result($result,0,"Address1");
$address2=mysql_result($result,0,"Address2");
$address3=mysql_result($result,0,"Address3");
$city=mysql_result($result,0,"City");
$county=mysql_result($result,0,"County");
$postcode=mysql_result($result,0,"PostCode");
$schoolEmail=mysql_result($result,0,"Email");
$schoolNameLength=strlen($schoolName);
$query="SELECT * FROM users WHERE ID='$originalStaffID'";
$result=mysql_query($query);
$staffFirstName=mysql_result($result,0,"firstName");
$staffSurname=mysql_result($result,0,"surname");
$date = date("jS F Y", $timestamp);
//check to see if contact has email address
//if not use schools
if ($to == "#") {
$to=$schoolEmail;
}
if ($to == "") {
$to=$schoolEmail;
}
require("pdfgenerator/fpdf.php");
// fpdf object
$pdf = new FPDF();
// generate a PDF
$pdf->AddPage();
$pdf->Image('images/apquotelogo.png',70,5,70);
$pdf->SetY(25);
$pdf->SetX(0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(210,6,'test',0,1,'C');
[.....more code which generates PDF]
// email stuff (change data below)
$from = "test@test.com";
$subject = "Test email";
ob_start();
?>
<head>
<style type="text/css">
<!--
.footerText {
font-family: Calibri;
}
.footerTextSmall {
font-size: 11px;
font-family: Calibri;
}
.footerTextRight {
text-align: right;
font-family: Calibri;
}
-->
</style>
</head>
<body>
<span class="footerText">
<? echo $saluation . ' ' . $lastname; ?> <br>
<? echo $schoolName; ?><br>
<? echo $address1; ?><br>
<? echo $address2; ?><br>
<? echo $postcode; ?><br>
<br>
</span><span class="footerTextRight"><? echo date("jS F Y", $timestamp); ?><br>
<br>
Our Ref: <?php echo $schoolID . '-' . $quoteID; ?></span><span class="footerText"><br>
<br>
Dear <? echo $saluation . ' ' . $lastname; ?>,<br>
<b>Re: Test</b><br>
Test Email<br>
<br>
Yours sincerely,
<br><br>
Joe Bloggs</span>
<br>
</body>
</html>
<?php
include('emailfooter.php');
$message = ob_get_clean();
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "QUO-" . $schoolID . "-" . $quoteID . ".pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
$mail_sent = @mail( $to, $subject, $message, $headers );
//set emailStatus
$emailStatus = $mail_sent ? "Sent" : "Failed";
//Update quote as email sent
$query = "UPDATE quotations SET Email='Yes' WHERE ID='$quoteID'";
mysql_query($query);
//Add note with email details
$query = "INSERT INTO notes VALUES ('','$schoolID','System','$timestamp','Quotation sent: $quoteID','Status: $emailStatus <br> Subject: $subject <br> Timestamp: $timestamp <br> UserID: $originalStaffID - $staffFirstName $staffSurname <br> From: $from <br> To: $to <br> Printer: $printerStatus')";
mysql_query($query);
?>