Jump to content

PHP email sent but no attachments??


tmartinez21

Recommended Posts

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 = 'tmartinez21@live.com';

 

/*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";

 

 

 

?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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: jwoods@elpasotimes.com <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]

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.