PHP Forums Archive

PHP Mail Attachment Script - sends 0 bytes

Tags: php, mail, html, attach

PopNBrown posted on 2010-06-14 10:35:07 #

So, I've been working on this PHP Mail Attachment script. I sort of figured out what's going on, and mostly copied code from all over the place (including finalwebsites.com)

<?php require_once("java/Java.inc");

  $filename = $_FILES['file']['name'];
  $path = $_FILES['file']['tmp_name'];
  $to = "me@mydomain.com"
  $from_mail = $_POST['email'];
  $from_name = $_POST['name'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];

  $content = chunk_split(base64_encode(file_get_contents($path.$filename)));
  $uid = md5(uniqid(time()));
  $name = basename($file);

  $ext = explode('.', $filename);
  $ext = $ext[1];

  if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
  $mime_type = "image/jpeg";
  }
  elseif($ext == "gif" || $ext == "GIF") {
  $mime_type = "image/gif";
  }

  $header = "From: ".$from_name." <".$from_mail.">\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  $header .= "This is a multi-part message in MIME format.\r\n";
  $header .= "--".$uid."\r\n";
  $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  $header .= $message."\r\n\r\n";
  $header .= "--".$uid."\r\n";
  $header .= "Content-Type: ".$mime_type."; name=\"".$filename."\"\r\n"; // use different content types here
  $header .= "Content-Transfer-Encoding: base64\r\n";
  $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  $header .= $content."\r\n\r\n";
  $header .= "--".$uid."--";

  mail($to, $subject, "", $header)
?>

So this script successfully emails, along with an attachment. However, the problem is the attachment is 0 bytes. Any ideas?

I'm running this through Tomcat 6.0 (using PHP/JavaBridge) on CentOS 5, with PHP 5.3 (the latest).

The following is the HTML Code which calls the above script:

<form name="contribute" action="contribute.php" method="post" enctype="multipart/form-data">
 Name: <br /><input name="name" size="30"><br /><br />
 E-mail: <br /><input name="email" size="30"><br /><br />
 Subject:     <br /><input name="subject" size="30"><br /><br />
 Attachment:  <br /><input type="file" name="file" id="file" size="30"><br /><br />
 Message: <br />
 <textarea name="message" COLS=50 ROWS=10 wrap="soft"></textarea><br /><br />
  <input type="submit" value="Submit">
</form>

Thanks for the help!
Sravan

Comments / discussions

Olaf posted on 2010-06-14 10:38:39 #

Hi,

check first if the mail() function is not disabled and than try this mail script

If you need a solution for SMTP check this review:
http://www.web-development-blog.com/archives/php-mail-scripts-using-smtp-transport-a-guide-for-beginners/

PopNBrown posted on 2010-06-15 08:18:14 #

Hey,

I'm not sure how to check to see if the mail() function is disabled. I looked all over the config files, couldn't find anything with mail. But the thing is, it does send a mail, just the attachment is 0 bytes.

Also, the mail script isn't sending an e-mail out (don't know why!) but processmailer() returns false.

Anyways, since I already have a script, I was wondering if you could just diagnose that, it would be much quicker for me, instead of having to try to implement a brand new script.

Thanks,
Sravan

Olaf posted on 2010-06-15 09:58:29 #

Ask your provider or check with phpinfo() which functions are disabled.

PopNBrown posted on 2010-06-15 10:53:31 #

phpinfo() doesn't indicate that mail() is disabled.

Also I'm running this on localhost through Tomcat. Decided to get it first developed before I get a domain.

Olaf posted on 2010-06-15 11:50:07 #

I never did something with tomcat, to send mail from your local server you need to install a mail server (or use some external smtp connector).

try this simple code

if (!mail('some@mail.com, 'a subject', 'this is my message')) echo 'mail doesn't work!';

PopNBrown posted on 2010-06-16 05:56:13 #

So it sends me a mail and it doesnt echo "mail doesn't work"
In fact I put it an else statement and the else statement works.

Do i still need to install a mail server if the mail() is working?

Olaf posted on 2010-06-16 06:06:30 #

okay now you know that you're able to use the mail() function ;)

Sending mail messages with attachments is not easy, that a mail is not send (or that message is empty) is related to many circumstances. Your code snippet is great for understanding and learning the function but if you need a mail solution use the bigger projects PHPmailer is my favorite and very easy to use. If you start with the tutorial I have posted before your script is ready in less than 5 minutes. (btw. your code is not safe, one reason more to use a better script)

PopNBrown posted on 2010-06-16 08:15:33 #

Took your advice, just used phpmailer (someone else recommended it as well). It's up and running now. I guess, I'll just let me quest to learn die out, since it's too complicated off a problem.

Thanks for all the help!

Olaf posted on 2010-06-16 08:27:26 #

You're welcome.
Mail headers are a kind of mystery, it took me a lot of hours to get those headers working in my mail php script.

At the end it's important that most of the messages are delivered into each mail inbox

PopNBrown posted on 2010-06-16 12:02:52 #

So...I guess I'm not done yet.

So I'm doing this project for my uncle...who wants to start a website for his non-profit organization. So mostly informational stuff. So that's why he wants to make the feedback form top notch. anyways, getting to the point.

His e-mail address is from www.aol.in and everytime I use this script or even just the basic mail() function, it doesn't seem to go through.

Checked the settings, and made sure to allow all messages. Still doesn't seem to come through.

Any idea what the problem might be?

Other than that it works with gmail, my school mail, yahoo...pretty much everything except aol.in

Olaf posted on 2010-06-16 19:16:07 #

This not related to the script but to AOL. Try to send the mail messages via SMTP

PopNBrown posted on 2010-06-17 08:32:55 #

See that's the problem. I can't use SMTP because this is a feedback form. Which means user enters their name/email and uploads an attachment and it comes to his aol address.

I mean I can just send it from myself and have the Name/Email as content, but itd be nice if it would work as though it were from that person's email.

Olaf posted on 2010-06-17 09:24:16 #

Hi,

You're missing something, "sending via SMTP" is only about how the e-mail transport is done.
Check this contact form tutorial which describes a complete solution.