PHP send email with attachment

Posted on 2010-01-11
Medium Priority
Last Modified: 2013-12-17
I am setting http://www.humanityalive.org/if_ambassador.php the form on the page is submitted to thanks2.php which sends 2 emails

1 to info@humanityalive.org that has details of the visitor

& 2. to the person who has filled the form with attachment.

Part 1 is working without problem

part 2 is also working & sends email but also give following error

Warning: fopen(/public_html/ebooks/InformationPack.pdf) [function.fopen]: failed to open stream: No such file or directory in /home/emmavis1/public_html/thanks2.php on line 48

Warning: filesize() [function.filesize]: stat failed for /public_html/ebooks/InformationPack.pdf in /home/emmavis1/public_html/thanks2.php on line 49

Warning: fread(): supplied argument is not a valid stream resource in /home/emmavis1/public_html/thanks2.php on line 49

Warning: fclose(): supplied argument is not a valid stream resource in /home/emmavis1/public_html/thanks2.php on line 50
You file has been sent
to the email address you specified.
Make sure to check your junk mail!
Click here to return to mysite.com.
require('template.php'); ?>
$ptitle='Our Core Mission';
$pageheadtitle='<H1><A href="http://www.humanityalive.org/">Passing On The Core Values Of Humanity To The Next Generation</A></H1>';
$pageheadmsg='<A href="http://www.humanityalive.org/">"Humanity Alive Foundation is a non-profit organization whose focus is to create stronger more humanitarian communities that will be maintained for future generations. We believe that there exists in the world too much hatred and chaos that plagues all people spanning from local communities to entire regions of the world. Our mission is to ensure that our children and our childrens children inherit a world that is free from unnecessary hatred and is full of moral values and moral responsibilities among all people. We will create a world in which our future generations can live in a sustainable healthy, positive, and promising society."</A>';
$pagecontent = '
<h1>Thank You</h1><br><br>

We shall get back to you soon!


 $email = $_REQUEST['emailaddress'] ;
$sp=" : " ;
 $message =FullName . $sp . $_REQUEST['fullname'] . "\r\n" . EmailAddress . $sp . $_REQUEST['emailaddress'] . "\r\n" . Address  . $sp . $_REQUEST['address'] . "\r\n" .  Zipcode . $sp . $_REQUEST['zipcode'] . "\r\n" . ContactNumber . $sp . $_REQUEST['contactnumber'];

$to = "info@humanityalive.org";
$subject = "IF Ambassador";
$from = $_REQUEST['emailaddress'] ;
$headers = "From: $from";


$fileatt = "/public_html/ebooks/InformationPack.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "InformationPack.pdf";  // Filename that will be used for the file as the attachment

$email_from = "info@humanityalive.org"; // Who the email is from
$email_subject = "Welcome To IF Ambassador Program"; // The Subject of the email
$email_message = "Welcome To IF Ambassador Program! Here is your Information pack.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it

$email_to = $email; // 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" .

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "<font face=verdana size=2><center>You file has been sent<br> to the email address you specified.<br>
Make sure to check your junk mail!<br>
Click <a href=\"#\" onclick=\"history.back();\">here</a> to return to mysite.com.</center>";

} else {
die("Sorry but the email could not be sent. Please go back and try again!");



Open in new window

Question by:Rajesh Joshi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Accepted Solution

Kalpan earned 1000 total points
ID: 26290793
Please correct the path of the pdf file which is stored on the server...

$fileatt = "/public_html/ebooks/InformationPack.pdf";

it should be

$fileatt = "/home/emmavis1/public_html/InformationPack.pdf";

that will remove all the errors....make sure you have the chmod 777 InformationPack.pdf to read the file from server...

hope that will solve the issue...




Assisted Solution

alex_savin earned 1000 total points
ID: 26290999
Clearly the path of that pdf file is not correct. Try the previous expert comment or:

$fileatt = $_SERVER['DOCUMENT_ROOT']."/ebooks/InformationPack.pdf";

this way it is relative to the server's public html path ($_SERVER['DOCUMENT_ROOT']).

Author Comment

by:Rajesh Joshi
ID: 26291196
Thanks Alex & Kalpan,


Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Suggested Courses

762 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