2

I have been trying to get php to send a email with a attached PDF, I have been looking everywhere and came across this post Email PDF Attachment with PHP Using FPDF.

I tried the solution but still no luck, does anyone know where I am going wrong? I get no errors or email?

Basically what this is doing is grabbing values from a form, putting them into a database then sending a email to them.

Any help would be greatly appreciated.

require('fpdf.php');
include("database.php");
require_once('recaptchalib.php');
$publickey = "6LcBYNsSAAAAAKkf5LwKkrhTVTVlxmOblcOn70-r ";
$privatekey = " 6LcBYNsSAAAAAEAa9wFZfyjInRmsWCxpj79Nvqm7";


// email stuff (change data below)
$to = "brentfrench@bigwavemedia.co.uk";
$from = "websupport@bigwavemedia.co.uk"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";


// PDF Create 
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');


// 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 = "test.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."\"";

// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$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."--";

// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];


if ($_POST["recaptcha_response_field"]) {

    $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
    if ($resp->is_valid) {

                            $check = mysql_query("SELECT * FROM voucher WHERE first='$firstname' AND last='$lastname'");
                            if(mysql_num_rows($check) != 0)
                            {
                                echo "<p>Voucher already in use.</p>";
                            }
                            else
                            {   
                                $insert = 'INSERT into voucher(first, last, postcode, email) VALUES ("'.$firstname.'","'.$lastname.'","'.$postcode.'","'.$email.'")';
                                mysql_query($insert);   
                                // send message
                                mail($to, $subject, $message, $headers) or die("Mail Error");                   
                                echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>";
                            }
 }
 else
 {
    echo "The anti spam code you entered is not correct.";
 }

}

Community
  • 1
  • 1
Brent
  • 2,385
  • 10
  • 39
  • 63
  • I would perform your antispam check first - otherwise the server is doing a whole lot of work for what may be a spambot. – Kaiesh Jan 11 '13 at 10:10
  • Why do people keep trying to use PHP's `mail()` function to create complex emails???? For the hundreth time, **use a [decent mailer class like phpMailer](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354) for this kind of thing, and save yourself hours of frustration**. – SDC Jan 11 '13 at 10:58

3 Answers3

4

I would move your antispam check up to the top, but I got the code below to work. I changed your EOL tag to "\r\n" and also added another $eol at the end of the header. Also I changed $message to $body in the mail() call, as all the prep work was done for $body but it wasn't used.

// 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 = "\r\n";

// attachment name
$filename = "test.txt";

// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("I am text file"));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;

// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$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."--";

// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];


echo mail($to, $subject, $body, $headers) or die("Mail Error");                   
echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>\n";
Kaiesh
  • 1,042
  • 2
  • 14
  • 21
  • Replaced $attachment = chunk_split(base64_encode("I am text file")); with chunk_split(base64_encode(file_get_contents($filename))); for better "'copy and paste'ability". – ctrl-alt-dileep Oct 30 '13 at 17:12
2

I have a mail class written. The attachment code is like:

$body .= "--".$separator."\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\";\r\n";
$body .= "Content-Length: ".$filesize.";\r\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $attachment."\r\n";
$body .= "--".$separator."--";

You see some differences: I've Content-Length, 2x $filename and \r\n as EOL.

I hope this helps.

bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • When i try this i get this error ( Fatal error: Using $this when not in object context ) – Brent Jan 11 '13 at 10:40
  • ok sorry..this is from my class. replace `$this->_body` with `$body` and get the `$filesize` with `filesize()`. – bitWorking Jan 11 '13 at 10:43
  • I've edited the answer and replaced `$this->_body` with `$body`. `$this` is needed when you program object oriented. [php oop](http://php.net/manual/en/language.oop5.basic.php) – bitWorking Jan 11 '13 at 11:23
1

There are so many possible break-points in this code, it's impossible to tell exactly what goes wrong. You'll need to do some debugging on your own, eg. using test outputs, to see how far the script gets. Also make sure you activate error reporting.

To send attachments an easier way might be using the SwiftMailer library.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I have tried testing to the best of my knowledge, and everything looks ok. I will take a look thanks – Brent Jan 11 '13 at 10:40