tmartinez21 Posted August 1, 2011 Share Posted August 1, 2011 Hello everyone, I am new to the PHP scene and just from my short experience it is one amazing scripting language. But i'm having an issue sending an email out with attachments. I can receive the emails but no attachments so I know I must be overlooking something. Here is the code and hopefully someone can shed some light on what I am doing wrong. Here is my code, thanks. <?php /*Subject and Email Variables*/ $emailSubject = 'Digital Online Form'; $webMaster = '[email protected]'; /*Gathering Date Variables*/ $email = $_POST['email']; $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $newspaper = $_POST['newspaper']; $advertiser = $_POST['advertiser']; $adSize = $_POST['adSize']; $adType = $_POST['adType']; $attachment = $_POST['attachment']; $comment = $_POST['comment']; $body = <<<EOD <BR><br><br> Email: $email <Br> First Name: $firstName <br> Last Name: $lastName <br> Newspaper: $newspaper <br> Advertiser: $advertiser <br> Ad Size: $adSize <br> Ad Type: $adType <br> Comment: $comment <br> EOD; $headers = "From: $email\r\n"; $success = mail($webMaster, $emailSubject, $body, $headers, $attachment); // Obtain file upload vars $attachment = $_FILES['attachment']['tmp_name']; $attachment_type = $_FILES['attachment']['type']; $attachment_name = $_FILES['attachment']['name']; if (file($attachment)) { // Read the file to be attached ('rb' = read binary) $file = fopen($attachment,'rb'); $data = fread($file,filesize($attachment)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $attachment = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $attachment . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $attachment .= "--{$mime_boundary}\n" . "Content-Type: {$attachment_type};\n" . " name=\"{$attachment_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$attachment}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } /*Results render as HTML*/ $theResults = <<<EOD <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> Thank you for your request. Your online/spec ad should be available in 24hrs or less. You may close this window at any time. </body> </html> EOD; echo "$theResults"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/ Share on other sites More sharing options...
DavidAM Posted August 1, 2011 Share Posted August 1, 2011 You've got a couple of problems here. $success = mail($webMaster, $emailSubject, $body, $headers, $attachment); 1) This is the line that sends the email. You have this line before you load the attachment, so you have sent the email without any attachments; 2) Attachments are sent as part of the body of the message. The fifth parameter to the mail call is for additional parameters to the sendmail call. Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1250321 Share on other sites More sharing options...
tmartinez21 Posted August 1, 2011 Author Share Posted August 1, 2011 Thanks for your reply. I moved the mail() line towards the bottom of the script. When I receive the email, it showed up like below but again without attachment. Attached is the code with the change I made. Again thanks for helping me out on this. Thoughts about anything else I might be doing wrong?? MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x65f6789e763d68cb2d16133286096ee0x" <BR><br><br> Email: [email protected] <Br> First Name: Joe <br> Last Name: Woods <br> Newspaper: Alamogordo Daily News <br> Advertiser: Alamo City Supply <br> Ad Size: 300x250 <br> Ad Type: Static <br> Comment: Cars. <br> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1250352 Share on other sites More sharing options...
Nodral Posted August 2, 2011 Share Posted August 2, 2011 Hi Save yourself loads of time and effort. Download a prebuilt mailer. I use Rmail, which can be found from Google. It will do everything you want and save you hours and hours. Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1250563 Share on other sites More sharing options...
DavidAM Posted August 2, 2011 Share Posted August 2, 2011 2) Attachments are sent as part of the body of the message. The fifth parameter to the mail call is for additional parameters to the sendmail call. That is why the attachments are not in the mail you received. Also, to get the part you ARE receiving to be displayed as HTML instead of showing the tags, you need to give that part the Content-Type header for HTML. So basically, the part you now have as the BODY becomes the FIRST part of your MULTI-PART MIME message. Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1250670 Share on other sites More sharing options...
IrOnMaSk Posted August 2, 2011 Share Posted August 2, 2011 it always pain in the behind trying to get php send the attachment if you don't know how and just use somebody else's code... the easieast way is to send it without outlook or yahoo or gmail, but i guess you have to use php, don't you!!! use a already build phpmailer... works exactly same and no scripting needed just some configuration but that's easy... but if you need to feel geeky it's possible to send mail with attachment purely using php... I've tried and gave up... I use sendmail instead Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1250695 Share on other sites More sharing options...
tmartinez21 Posted August 3, 2011 Author Share Posted August 3, 2011 Thanks everyone for your input, I finally made the changes and now im receiving my attachments with my emails. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1251081 Share on other sites More sharing options...
IrOnMaSk Posted August 3, 2011 Share Posted August 3, 2011 fantastic, can you share that code? i wanna give it a try again, ty Quote Link to comment https://forums.phpfreaks.com/topic/243503-php-email-sent-but-no-attachments/#findComment-1251293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.