send email with attachment

Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

send email with attachment

 
0
  #1
Nov 7th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 66
Reputation: guideseeq is an unknown quantity at this point 
Solved Threads: 0
guideseeq guideseeq is offline Offline
Junior Poster in Training

Re: send email with attachment

 
0
  #2
Nov 7th, 2006
I've attached .DOC and images files for e-mail using code provided here: http://in2.php.net/manual/en/ref.mail.php
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: send email with attachment

 
0
  #3
Nov 7th, 2006
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]
<?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!");
}
?>
[/PHP]

This code seems to work just fine.
Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: send email with attachment

 
0
  #4
Nov 7th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: adiki is an unknown quantity at this point 
Solved Threads: 0
adiki adiki is offline Offline
Newbie Poster

Re: send email with attachment

 
0
  #5
Nov 8th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: send email with attachment

 
0
  #6
Nov 8th, 2006
Originally Posted by adiki View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: send email with attachment

 
0
  #7
Nov 8th, 2006
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!
Last edited by nathanpacker; Nov 8th, 2006 at 8:38 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 19
Reputation: usang2me is an unknown quantity at this point 
Solved Threads: 0
usang2me usang2me is offline Offline
Newbie Poster

Re: send email with attachment

 
0
  #8
Apr 16th, 2009
Originally Posted by nathanpacker View Post
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: send email with attachment

 
0
  #9
Apr 16th, 2009
Please make sure that when adding headers you clear the line:
  1. $headers = "From: ".$from_address."\r\n";

Take note on the \r\n part.
Last edited by Josh Connerty; Apr 16th, 2009 at 1:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: viccowface is an unknown quantity at this point 
Solved Threads: 0
viccowface's Avatar
viccowface viccowface is offline Offline
Newbie Poster

Re: send email with attachment

 
0
  #10
Jun 20th, 2009
Originally Posted by nathanpacker View Post
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]
<?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!");
}
?>
[/PHP]

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC