Jump to content

[SOLVED] PHP Mail - Attach form content as .html file instead of embedding?


anbdesigns

Recommended Posts

I have an online order form that needs to send an email with a fax number in the body, while the actual form results are sent as an attachment(html, txt, pdf, rtf, jpg, etc).

Does this have to be done using fopen, or is there a much simpler way? ???

 

Thanks

So I decided to try attaching the content as a PDF (using FPDF) & after some hours, I finally got it working. ;D

For anyone that needs it, here's a condensed version of what I have.

 

<?php
// download fpdf class (http://fpdf.org)
require("fpd/fpdf.php");

$pdf_content = "Your content here\nAnd it even allows line breaks!";
$pdf_content = stripslashes($pdf_content);

// fpdf object
$pdf = new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","",12); // Can specify font, style, size
$pdf->Multicell(100,5, $pdf_content); // Multicell allows line breaks using \n. The 1st number sets the width of the cell, 2nd sets line spacing - not sure the unit of measure

// email stuff (change data below)
$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = "Email Subject"; 
$message = "1115552222"; // body of message. For me it was just a fax number

// 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;

// Whatever you want the filename to be
$filename = "attachment.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/plain; charset=\"iso-8859-1\"".$eol; // Change plain to html if you want HTML in your message
$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($to, $subject, "", $headers);

?>

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.