Emailing with attachment

Discussion in 'PHP' started by rob7676, Sep 29, 2011.

  1. #1
    I have ventured into unknown waters and now my head hurts, I could really use some help. Here is the idea, I have an online form(internal) to report an incident, user submits form, php writes it to db, then builds pdf and emails the pdf to me. I am using FPDF for my pdf generation. Now the problem is, the attachment is empty and gives me an error that the file may not have been decoded correctly or not a supported file type.

    Email code:
    // email stuff (change data below)
    $to = "me@somewhere.com";
    $from = "webmaster@somewhere.com";
    $subject = "Online Event Submission";
    $message = "<p>Please see the attachment.</p>";
     
    // 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
    $setTime = time();
    $setDate = date('M j, Y');
    $filename = "Incident.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: 7bit".$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);
    PHP:
    Any help is appreciated, thanks for taking the time to read.

    Rob
     
    rob7676, Sep 29, 2011 IP