SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php email with pdf file attachment

    Hello friends,

    I m using php code to generate pdf file and send as attachment via email.

    Code is

    Code PHP:
    #
    ?php
    #
    // download fpdf class ([url]http://fpdf.org[/url])
    #
    require("/fpd/fpdf.php");
    #
     
    #
    // fpdf object
    #
    $pdf = new FPDF();
    #
     
    #
    // generate a simple PDF (for more info, see [url]http://fpdf.org/en/tutorial/[/url])
    #
    $pdf->AddPage();
    #
    $pdf->SetFont("Arial","B",14);
    #
    $pdf->Cell(40,10, "this is a pdf example");
    #
     
    #
    // email stuff (change data below)
    #
    $to = "target@domain.com";
    #
    $from = "me@domain.com";
    #
    $subject = "send email with pdf attachment";
    #
    $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
    #
    $filename = "example.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: 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);
    #
     
    #
    ?>

    This return me a error

    mail() [function.mail]: Bad parameters to mail() function, mail not sent. in email_pdf.php on line 57

    Can anybody tell me where are i wrong?


    This retun me error

  2. #2
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This might be a long shot, but can the $message parameter be a blank string? Try putting some dummy data in and see what happens.

    Also, you have a lot of variables in your $header, try echoing it all out before the mail is sent to see if the headers are properly formed.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Consider using a mailing library such as swiftmailer or phpmailer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •