PHP sends email, but not PDF attachment

Posted on 2008-06-20
Last Modified: 2012-05-05
The following PHP code correctly sends an email with the appropriate variables in the body of the email.  However, it is also supposed to send a PDF attachement called form.pdf.  The PDF file is found in the root directory of the website.

Please help me figure out why the email is sent, but not the PDF attachment.

I have changed the To and From email addresses to fake addresses.  
$emailSubject = 'New Completed PDF';

	$webMaster = '';

	$firstnameEM = $_SESSION['svFirstName'];

	$lastnameEM = $_SESSION['svLastName'];

	$companyEM = $_SESSION['svCompany'];

	$phoneEM = $_SESSION['svphone'];

	$emailEM = $_SESSION['svemail'];

	$dateEM = date('l, F dS Y.');

	$body = <<<EOD


First Name:  $firstnameEM <br><br>

Last Name:  $lastnameEM <br><br>

Company:  $companyEM <br><br>

Phone: $phoneEM <br><br>

Email:  $emailEM <br><br>

Date Completed Form:  $dateEM <br><br>

Find attached PDF of completed Application.


	/*$headers = "From: $emailField\r\n";  */

	$headers = "From:\r\n";


	$headers .= "Content-type: text/html\r\n";


$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";

/********************************************** First File ********************************************/

$fileatt = "form.pdf"; // Path to the file

$fileatt_type = "application/octet-stream"; // File Type

$fileatt_name = "application.pdf"; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');

$data = fread($file,filesize($fileatt));


$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" .









	$success = mail($webMaster, $emailSubject, $body, $headers);

	$theResults = <<<EOD


echo "$theResults";

Open in new window

Question by:fastfind1

Accepted Solution

vercesi earned 500 total points
ID: 21830784
I believe this can help you:


Ricardo Vercesi
LVL 49

Expert Comment

ID: 21832329
If that doesn't help. Try the phpmailer class. It supports about anything you need in regard to mailing and is been used by thousands of sites. Saves you to reinvent the wheel.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now