View Full Version : MailScripts OH YAY!!

06-15-2010, 12:57 PM
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:

<?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/E-mail: <br /><input name="name" 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">

Thanks for the help!

06-15-2010, 06:06 PM
Tough one to troubleshoot from where I'm sitting, but I have kind of another solution you may want to try out. There's an open source email library you should check out. It's called PHP Mailer and it makes the task of sending emails with attachments really easy, almost disgustingly so. It takes literally 15 minutes to install and write the code for a simple email with attachment test.

So... you can reinvent the wheel... again... or take advantage of the hard work of others... your choice :p


06-15-2010, 06:39 PM
Well the thing, is I tried playing around with that, and another mail script, but it's been giving me trouble as well. So it'll still take me sometime to get working on those.

So if you can think of any possible solutions, or if you need me to provide other information, lemme know. I'll try the phpmailer as well.

06-16-2010, 10:13 AM
Just used phpmailer, took me sometime to get it all set. I'm still curious as to the answer for this problem, but I won't be checking the thread anymore.

Anyone figures it out, send me a PM, please.