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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Phishing emails are a popular malware delivery vehicle for attack.  While there are many ways for an attacker to increase the chances of success for their phishing emails, one of the most effective methods involves spoofing the message to appear to …
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

621 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