[PHP] Email Attachment – PDF In Email Attachment

Hi Friends , yesterday I was working with email attachment and I wanted to send pdf file as email attachment but It was giving me so many problems. I have searched so many code for PDF attachment but no one was working So I start tp debug code and finally made working pdf attachment code by pdf attachment in email worked successfully.

Below is code which I have used for PDF email attachment:


$fileatt = "test.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "test.pdf"; // Filename that will be used for the file as the attachment
$email_from = "noreply@hackingethics.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting HackingEthics.com! <br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it
$email_to = "xyz@gmail.com"; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
$sent = @mail($email_to, $email_subject, $email_message, $headers);
if($sent) {
echo "Your email attachment has been Worked.";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
Replace your email address in $email_to and filename in $fileatt and $fileatt_name.
I am PHP Freelancer and WordPress Developer So If you have any projects related to PHP or any Open Source then contact me.
  • UGG

    good very good!!

  • Hey Ankur why r u not updating ur blog

  • I have a small php script

    i need to send a file mediapack.pdf with the email the file is already on the server;
    can anyone suggest a way to do this, i really dont want a link to the file within the email

  • Santhosh

    Quick Books, one of three flagship products offered by California-based Intuit, was originally patterned after personal accounting package Quicken. Intuit executives wanted to provide a package that would offer small business owners the flexibility of an easy-to-use software package and the structure and compliance required to run a profitable business.
    At its launch in 1998, QuickBooks was very popular with business owners with no formal accounting background. Unfortunately, although the package was easy to use and offered significant benefit to its customers, many accounting professionals felt that it lack the security, audit trail and robustness really needed. These concerns were exacerbated because many business owners used QuickBooks, which quickly established an 80% market share, for day-to-day bookkeeping – but relied on professional accountants for month-end and year-end services.

  • John Henry

    We have used something similar. You can also easily add the API from BlinkSend to see reports of the pdf and get feedback from people. The API is free too. Think it is a bonus to your email blog here. Very useful I think.

    – keep hacking

  • Erica

    Great code – thanks :)

  • Thanks for this PDF email attachment..
    best hosting service | best web hosting company

  • I am geting very much confused with the PHP codings.As I am new in Magento, i am taking little time to understand the codings.Thanks for sharing your article.This case is also important for me.

  • I was looking for this functionality, and this is the first that works fine, i.e. sending the attachment and the text. Thanks. I’m experienced PHP programmer, but this topic is about the mail format (header, body, subject) and I’m trying to send more than one file… any tip? Thanks for the script.

  • pratziyer

    Im finding difficulty in formatting the email_message.if my email body has more than one line the pdf file is not attached instead its showing some ascii characters.i tried using n and some problem exists..pls help