Reputation: 4740
I have this piece of PHP code, although it works, but the problem is that the email message content is sent as an attachment along with the intended attachment which means that when the email arrives, the body is blank but there's two attachments. One is the intended attachment and the other is the email content that is supposed to be displayed in the the email. Is there something I'm doing wrong?
Here's the full source code:
<?php
require('fpdf/fpdf.php');
include('./config.php');
$pdf = new FPDF('P', 'pt', 'Letter');
$pdf->SetAutoPageBreak(true, $margin);
$pdf->AddPage();
$pdf->SetFont('arial','',12);
if(isset($_POST['accept'])){
$id = $_POST['id'];
$to = '[email protected]';
$subject = urldecode($_POST['subject']);
$message = urldecode($_POST['message']);
$data = '';
$result = mysql_query("select * from applications where id = $id");
$row = $data_array = mysql_fetch_assoc($result);
$data .= "Name: " . $row['name'] . " \n\n";
$data .= "Surname: " . $row['surname'] . "\n\n";
$data .= "Age: ". $row['age'] . "\n\n";
$data .= "Dob: ". $row['dob'] . "\n\n";
$data .= "Height: ". $row['height'] . "\n\n";
$data .= "Add1 : ".$row['address1'] . "\n\n";
$data .= "Add2: ".$row['address2'] . "\n\n";
$data .= "Add3: ".$row['address3'] . "\n\n";
$data .= "Postcode: ".$row['postcode'] . "\n\n";
$data .= "Town: ".$row['town'] . "\n\n";
$data .= "County: ". $row['county']. "\n\n";
$pdf->SetX(140);
$pdf->Ln(2);
$pdf->MultiCell(0, 15, $data);
$from = "[email protected]";
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "form.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if(mail($to, $subject, $body, $headers)){
echo "<b>Email successfully sent</b>";
}
else{
echo "Your message could not be sent.";
}
}
?>
Any help will be highly appreciated.
Upvotes: 0
Views: 129
Reputation: 5450
I actually have some experience with single-handedly writing this kind of thing from scratch. After years of maintaining a home-brewed PHP e-mail builder, my advice is that you shouldn't. It certainly takes less time and resources to research and find the most well-supported, mainstream solution today (whenever today is) than to write your own (and to pay for all the inherent bugs with your time and/or resources).
Upvotes: 1