6

Ok guys, this is my first thread, and I have searched online but with no luck. I'm doing an internship, and I'm working on a project that requires me to create a webpage that generates a pdf file when a user submits his/her information. As soon as a customer clicks on the submit button, 3 things need to happen:

  1. Store information to the database(done),
  2. Send the staff an email with the new customer information (done), and
  3. Send the customer a "thank you message" email with a pdf file attachment (not working).

I mean, the customer does receive an email, but when he/she opens the pdf file, I get the following error message:

"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."

Please keep in mind this is my fisrt time doing a project on creating a pdf file attachment. If someone could help me resolve this problem, that would be great. Thanks!

Here is my code:

<?php
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
        $msg = "Name: " .$_POST['name'] . "\n"
            ."Email: " .$_POST['email'] . "\n"
            ."Phone: " .$_POST['telephone'] . "\n"
            ."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
            ."Date Of Reunion: " .$_POST['date'];
        $staffEmail = "staffinfo";

        mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)

        //once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
        $pdf=new FPDF();
        $pdf->AddPage();
        $pdf->SetFont("Arial", "B", 16);
        $pdf->Cell(40, 10, "Hello World!");

        // email information
        $to = $_POST['email'];
        $from = $staffEmail;
        $subject = "Thank you for your business";
        $message = "Thank you for submitting your information!";

        // 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 = "yourinformation.pdf";

        // encode data (puts attachment in proper format)
        $pdfdoc = $pdf->Output("", "S");
        $attachment = chunk_split(base64_encode($pdfdoc));

        // encode data (multipart mandatory)
        $headers = "From: ".$from.$eol;
        $headers .= "MIME-Version: 1.0".$eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
        $headers .= "Content-Transfer-Enconding: 7bit".$eol;
        $headers .= "This is a MIME encoded message.".$eol.$eol;

        // message
        $headers .= "--".$separator.$eol;
        $headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
        $headers .= $message.$eol.$eol;

        // attachment
        $headers .= "--".$separator.$eol;
        //$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
        $headers .= "Content-Type: application/zip; 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, $message, $headers);

    }
}
?>
ghoti
  • 45,319
  • 8
  • 65
  • 104
Paolo Scamardella
  • 245
  • 2
  • 6
  • 14
  • I noticed that you have application/zip. Try application/pdf instead. – Raisen Dec 12 '11 at 16:45
  • I did that too, but it won't work. – Paolo Scamardella Dec 12 '11 at 16:50
  • Not had a proper think about it yet but IIRC the eol for MIME headers is always "\r\n" the actual value in `PHP_EOL` varies depending on the OS; that might put a spanner in the works in your $headers var; though according to the spec it should be tolerant: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html (19.3) – CD001 Dec 12 '11 at 17:01
  • 2
    I recommend you use a mailing library like swiftmailer or phpmailer. Hand crafting mime emails is the pits. – goat Dec 12 '11 at 17:46
  • possible duplicate of [php send e-mail with attachment](http://stackoverflow.com/questions/3092821/php-send-e-mail-with-attachment) – nikc.org Dec 13 '11 at 13:12
  • Ok nikc, if the answer below doesn't work, I will look at the link you have sent me. Thanks! – Paolo Scamardella Dec 13 '11 at 13:25
  • Did you try opening the attachment in notepad to see whether an PHP error is disrupting your output stream? – Ben Dec 13 '11 at 19:36

2 Answers2

2
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
        $msg = "Name: " .$_POST['name'] . "\n"
            ."Email: " .$_POST['email'] . "\n"
            ."Phone: " .$_POST['telephone'] . "\n"
            ."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
            ."Date Of Reunion: " .$_POST['date'];
        $staffEmail = "staffemail";

        mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)

        //once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
        // creating a pdf file
        $pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
        $pdf=new FPDF();
        $pdf->AddPage();
        $pdf->SetFont("Arial", "B", 16);
        $pdf->Cell(40, 10, "Title");
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(15, 10, "Name:");
        $pdf->SetFont("Arial", "I", 12);
        $pdf->Cell(15, 10, $_POST['name']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(15, 10, "Email:");
        $pdf->SetFont("Arial", "I", 12);
        $pdf->Cell(15, 10, $_POST['email']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(15, 10, "Phone:");
        $pdf->SetFont("Arial", "I", 12);
        $pdf->Cell(15, 10, $_POST['telephone']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "Number of Guests:");
        $pdf->SetFont("Arial", "I", 12);
        $pdf->Cell(40, 10, $_POST['numberOfGuests']);
        $pdf->Ln();
        $pdf->SetFont("Arial", "", 12);
        $pdf->Cell(40, 10, "Date Of Reunion:");
        $pdf->SetFont("Arial", "I", 12);
        $pdf->Cell(40, 10, $_POST['date']);
        // if file doesn't exists or if it is writable, create and save the file to a specific place 
        if(!file_exists($pdf_filename) || is_writable($pdf_filename)){
            $pdf->Output($pdf_filename, "F");
        } else { 
            exit("Path Not Writable");
        }

        // using the phpmailer class
        // create a new instance called $mail and use its properties and methods.
        $mail = new PHPMailer();
        $staffEmail = "staffemail";
        $mail->From = $staffEmail;
        $mail->FromName = "name";
        $mail->AddAddress($_POST['email']);
        $mail->AddReplyTo($staffEmail, "name");

        $mail->AddAttachment($pdf_filename);
        $mail->Subject = "PDF file attachment";

        $mail->Body = "message!";

        // if mail cannot be sent, diplay error message
        //if(!$mail->Send()){
            //echo "<div id=\"mailerrors\">Message could not be sent</div>";
            //echo "<div id=\"mailerrors\">Mailer Error: " . $mail->ErrorInfo . "</div>";
        //} else { // else...if mail is sent, diplay sent message
            //echo "<div id=\"mailerrors\">Message sent</div>";
        //}

        // delete the temp file
        unlink($pdf_filename);
    }
}     
yakatz
  • 2,142
  • 1
  • 18
  • 47
Paolo Scamardella
  • 245
  • 2
  • 6
  • 14
  • 1
    This is a late comment, but you probably should use `tempnam` to generate a unique file name so you do not risk having a problem with two customers hitting the script at once. – yakatz Sep 02 '12 at 02:16
0

Try this:

<?php

  // once there are no errors, as soon as the customer hits the submit button,
  // it needs to send an email to the staff with the customer information
  $msg = "Name: " .$_POST['name'] . "\n"
       . "Email: " .$_POST['email'] . "\n"
       . "Phone: " .$_POST['telephone'] . "\n"
       . "Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
       . "Date Of Reunion: " .$_POST['date'];
  $staffEmail = "staffinfo";
  mail($staffEmail, "You have a new customer", $msg);

  // once the customer submits his/her information, he/she will receive a thank
  // you message attach with a pdf file.
  $pdf = new FPDF();
  $pdf->AddPage();
  $pdf->SetFont("Arial", "B", 16);
  $pdf->Cell(40, 10, "Hello World!");

  // email information
  $to = $_POST['email'];
  $from = $staffEmail;
  $subject = "Thank you for your business";
  $message = "Thank you for submitting your information!";

  // a random hash will be necessary to send mixed content
  $separator = '-=-=-'.md5(microtime()).'-=-=-';

  // attachment name
  $filename = "yourinformation.pdf";

  // Generate headers
  $headers = "From: $from\r\n"
           . "MIME-Version: 1.0\r\n"
           . "Content-Type: multipart/mixed; boundary=\"$separator\"\r\n"
           . "X-Mailer: PHP/" . phpversion();

  // Generate body
  $body = "This is a multipart message in MIME format\r\n"
        . "--$separator\r\n"
        . "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
        . "\r\n"
        . "$message\r\n"
        . "--$separator\r\n"
        . "Content-Type: application/pdf\r\n"
        . "Content-Transfer-Encoding: base64\r\n"
        . "Content-Disposition: attachment; filename=\"$filename\"\r\n"
        . "\r\n"
        . chunk_split(base64_encode($pdf->Output("", "S")))."\r\n"
        . "--$separator--";

  // send message
  mail($to, $subject, $body, $headers);
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • I waiting for the mail to come in, but it seems that the php mail function takes too long to send the message. The first email always takes so long. After the first email, it takes seconds to get the email. As soon as I receive the first email, I will confirm if it works or not. Thank you! – Paolo Scamardella Dec 13 '11 at 13:54
  • 1
    Nothing worked for me, so I changed everything, and I ended up using the phpmailer class. Thank all for your help!! – Paolo Scamardella Dec 13 '11 at 16:52