| | |
send email with attachment
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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!
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!
•
•
Join Date: Nov 2005
Posts: 66
Reputation:
Solved Threads: 0
I've attached .DOC and images files for e-mail using code provided here: http://in2.php.net/manual/en/ref.mail.php
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!
[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!
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?
Any way to get it to show the from address that I specified?
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!
Thanks!
Last edited by nathanpacker; Nov 8th, 2006 at 8:38 am.
•
•
Join Date: Apr 2009
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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!
Thanks
Kunal
Please make sure that when adding headers you clear the line:
Take note on the \r\n part.
php Syntax (Toggle Plain Text)
$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.
•
•
•
•
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
![]() |
Similar Threads
- Can't send email (Windows NT / 2000 / XP)
- Cannot Upload files or send email on wireless (Web Browsers)
- Using PHP to Send Email to a single DB list member every 24 hrs. (PHP)
- Batch file to send email!! (Windows NT / 2000 / XP)
- Not able to send email by "MAIL" (OS X)
- can not send email and cannot send file through instant messaging (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: opening different html pages using a field type in my sql database table
- Next Thread: How to Approve,Edit,Delete
| Thread Tools | Search this Thread |
advanced alerts apache api archive array autosuggest beginner binary broken cakephp checkbox class clients cms code cron curl database date display dynamic echo email emptydisplayvalue eregi error execute explodefunction file files folder form forms function functions google hack href htaccess html if...loop image include insert ip javasciptvalidation javascript joomla keywords library limit link login mail matching menu mlm multiple mysql object oop password paypal pdf php phpincludeissue problem query radio random recursion recursive remote script search searchbox server sessions shot smarty sms source space speed sql syntax system table tutorial update upload url validator variable vbulletin video web website youtube





