I need to create a very simple script with php. There is a form with only one input field, for the user's email address. They click submit, and the script sends an email to that address, while attaching a pdf file that I specify in the script.

I've found many scripts out there that have this functionality but that are just too feature rich and complicated.

I know how to create a script to send an email, but am having trouble attaching the pdf file. I know I need to use fopen and fread, but just can't get it to work. I felt that I knew php fairly well until I tried to do this seemingly simple task. ;) Any simple ideas?

Thanks, you all are great!

Recommended Answers

All 23 Replies

Thanks, I did find some useful code, and have fixed it to work to my advantage. Here is the code that is working for me.

<?php  
$fileatt = "mypdffile.pdf"; // Path to the file                  
$fileatt_type = "application/pdf"; // File Type  
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment  

$email_from = "sales@mysite.com"; // Who the email is from  
$email_subject = "Your attached file"; // The Subject of the email  
$email_message = "Thanks for visiting mysite.com!  Here is your free file.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  

$email_to = $_POST['email']; // Who the email is to  

$headers = "From: ".$email_from;  

$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file);  

$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" .  
                  "--{$mime_boundary}--\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!");  
}  
?>

This code seems to work just fine.
Thanks!

Actually, I did find one problem with this code. Where I specify the From address in the header doesn't seem to be working. When I receive the email that the script sends, the from address doesn't show as the one I specified, but some address from the web host, like mysite@box175.mywebhost.com

Any way to get it to show the from address that I specified?

i use php code with apache web server . to send mail directly from the script but i'm not able to send my message with images , the problem is i have warrning that is smtp porblem what can i do?

i use php code with apache web server . to send mail directly from the script but i'm not able to send my message with images , the problem is i have warrning that is smtp porblem what can i do?

Well I'm sure someone here might be able to help you, but you might want to search for it first and then try posting a new thread instead of continuing on this one.

Just for whoever is interested, I found out the problem with the email address. My customer told me they wanted the email sent to such and such address, but they didn't tell me that such and such address didn't even exist. I logged into the web host, created the account, and all is well.
Thanks!

Just for whoever is interested, I found out the problem with the email address. My customer told me they wanted the email sent to such and such address, but they didn't tell me that such and such address didn't even exist. I logged into the web host, created the account, and all is well.
Thanks!

Hey i'm facing the same problem , can you help me out of it please

Thanks
Kunal

Please make sure that when adding headers you clear the line:

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

Take note on the \r\n part.

Thanks, I did find some useful code, and have fixed it to work to my advantage. Here is the code that is working for me.

<?php  
$fileatt = "mypdffile.pdf"; // Path to the file                  
$fileatt_type = "application/pdf"; // File Type  
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment  

$email_from = "sales@mysite.com"; // Who the email is from  
$email_subject = "Your attached file"; // The Subject of the email  
$email_message = "Thanks for visiting mysite.com!  Here is your free file.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  

$email_to = $_POST['email']; // Who the email is to  

$headers = "From: ".$email_from;  

$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file);  

$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" .  
                  "--{$mime_boundary}--\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!");  
}  
?>

This code seems to work just fine.
Thanks!

Hi!! when i use this code, it return :

Warning: filesize() [function.filesize]: stat failed for http://www.landcr.net/img/top_print.jpg in /home/landcrn/public_html/html/envio_h.php on line 16

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/landcrn/public_html/html/envio_h.php on line 16
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.

and the picture is not send.... why?

regards

Thanks, I did find some useful code, and have fixed it to work to my advantage. Here is the code that is working for me.

<?php  
?>

This code seems to work just fine.
Thanks!

You, sir, are a genius. Thank you for posting your solution. It worked perfectly for me as well. (had to change it because my page has no "post", but that was the easy part! you supplied the working "base" ...)

Thanks to you and all those who helped you generate that code :)

I really like this thread, I created something earlier to send emails with attachments in php. It backups a mysql database and then attaches it an email, you can set it up on a cron job to auto send the email everyday! It's a nice bit of code.
If anyone is interested see it here: http://www.thephpanswers.com/viewtopic.php?f=11&t=8&start=0

I really like this thread, I created something earlier to send emails with attachments in php. It backups a mysql database and then attaches it an email, you can set it up on a cron job to auto send the email everyday! It's a nice bit of code.
If anyone is interested see it here: http://www.thephpanswers.com/viewtopic.php?f=11&t=8&start=0

consider using scp to copy your backup (securely!) after creating it. you can use authorized keys to pre-auth the user to transfer files and then you can run unattended backups without passing all the data through SMTP.

alternately, you can even create a UUID with php and email that (after creating a link file or saving the backup with the UUID as a filename) so the receiver of the email can directly download the file (but only if they have the UUID, once again it can be secure if you use secure email and https for the file tranfer, and the likelihood of someone "guessing" the filename to download it are ... slim)

consider using scp to copy your backup (securely!) after creating it. you can use authorized keys to pre-auth the user to transfer files and then you can run unattended backups without passing all the data through SMTP.

alternately, you can even create a UUID with php and email that (after creating a link file or saving the backup with the UUID as a filename) so the receiver of the email can directly download the file (but only if they have the UUID, once again it can be secure if you use secure email and https for the file tranfer, and the likelihood of someone "guessing" the filename to download it are ... slim)

Hi William,

Thanks for the comments they are noted :)

I have actually created a little cleanup procedure so the .sql in the folder get deleted every time the script is ran. I appreciate your comments about the security. However like you said, the chances are very slim somebody could guess the URL + with the .htaccess the public cannot browse the directory. The whole idea was to have the backup sent automatically, no requirements for the user - so even though the unique identifier idea is good it wouldnt work well in this scenario.

Regards,
Colin Jensen

File is sending but we are not able to see the content of that file

I just tried it, but it doesn't seem to work, I get the error message back.
Which I think is fine because, how will it send the email if we don't provide it the smtp server and all ?
Please explain.

i use php code with apache web server . to send mail directly from the script but i'm not able to send my message with images , the problem is i have warrning that is smtp porblem what can i do?

can you publish more information about your problem. normally if you are trying to send an email using php mail() function on windows you must install a MTA (Mail Transport Agent) like xmail. cos windows does not have internal mail server like Linux/UNIX

I just tried it, but it doesn't seem to work, I get the error message back.
Which I think is fine because, how will it send the email if we don't provide it the smtp server and all ?
Please explain.

are you using a linux server or windows.. if you are on windows you must install a MTA(Mail Transport Agent).

This code seems to be very simple and precise. Thank you very much..

when the file will send to the user, what will be the URL to download the file ? Can you explain the Concept behind this ? Werther the file will uploaded to our server and accordingly url will added in mail ?

Please explain ..... i really need it.

nope in this method the attachment will be included to your email.i mean technically. it encode the data you attached and then paste the esulting text into the body of the message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.