Solved

PHP sends email, but not PDF attachment

Posted on 2008-06-20
2
364 Views
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 = 'fake@email.com';
	$firstnameEM = $_SESSION['svFirstName'];
	$lastnameEM = $_SESSION['svLastName'];
	$companyEM = $_SESSION['svCompany'];
	$phoneEM = $_SESSION['svphone'];
	$emailEM = $_SESSION['svemail'];
	$dateEM = date('l, F dS Y.');
	$body = <<<EOD
<br><hr><br>
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.
 
EOD;
 
	/*$headers = "From: $emailField\r\n";  */
	$headers = "From:  fake@email.com\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));
fclose($file);
 
 
$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" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);
 
	
	
	$success = mail($webMaster, $emailSubject, $body, $headers);
	$theResults = <<<EOD
 
EOD;
echo "$theResults";

Open in new window

0
Comment
Question by:fastfind1
2 Comments
 
LVL 1

Accepted Solution

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

http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

Regards

Ricardo Vercesi
0
 
LVL 49

Expert Comment

by:Roonaan
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 43
Wordpress Security 29 47
Need to remove %20 from url in get 17 36
Unable to print PDF document on Mac laptop 6 21
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

790 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