Page 1 of 1

Send Email Attachment

Posted: Sun Mar 12, 2006 2:49 am
by transfield
Hello,
My ultimate goal is to browse a particular folder, select a file & send it as an email attachment to someone. With that in mind, I have designed a form. The form works fine. I've included the code here just to give you the total picture of what I'm doing.

The form action points towards another file called email_sender.php where all the magic happens. This is where I need help. I've included the code below as well.

My question is:-
How do I send the selected file as an email attachment?

I'm a newbie who does not understand much technical jargon. I've seen quite a number of samples on many forums but I don't understand the technical jargon. This code I wrote is actually based on my copy & paste activities so I don't actually understand how it works, but it works :-) Thank you for your guidance.

The code for the form:-

Code: Select all

<?php
           if ($dir = @opendir("/home/someone/domains/someone.com/file_emailer")) 
           {
               while (($file = readdir($dir)) !== false) 
               {
                   if($file != ".." && $file != ".")
                   {
                       $filelist[] = $file;
                   }
               } 
               closedir($dir);
           }
?> 
<form name="form1" method="post" action="email_sender.php">
<form>
           <select name="selected_dir" > 

<?php 
           asort($filelist);
           while (list ($key, $val) = each ($filelist)) 
           {
               echo "<option>$val</option>";
           }
?>
  </select>
           <label>
           <input name="email1" type="text" id="email1" value="Enter your email here" />
  </label>
           <label>
           <input name="Submit1" type="submit" id="Submit1" value="Submit" />
  </label>
</form>
</form>
The code for email_sender.php:-

Code: Select all

<html>

<head>

<title>New Page 1</title>
</head>

<body>
<?php
$username="abc123";
$password="abc123";
$database="abc123";
$host="localhost";
$selected_dir=$_POST['selected_dir'];
$email1=$_POST['email1'];
$Submit1=$_POST['Submit1'];
if(isset($Submit1))
{
		
		mysql_connect ("$host","$username","$password");
		mysql_select_db($database) or die( "Where's the database man?");
		$query=mysql_query("SELECT * FROM customers WHERE email = '$email1'");
		$numrows=mysql_num_rows($query);
		if ($numrows>0)
		{
			$data=mysql_fetch_array($query);
			$from ='someone@someone.com';
			$to = $data["email"];
			$name = $data["name"];
			$subject = "Your file request";
			$headers = "$selected_dir";//I suspect more stuff needs to be added here.
			$body="Hello ". $name .",\nHere's your file
			\nBest of luck to you in all your appointments tommorrow :-)
			\nWarm Regards\n\nsomeone\nsomeone";
			mail($to,$subject,$body,$headers,"-fsomeone@someone.com");
			echo "Your e-mail has been sent";
		}
		else
		{
			echo "Your subscription has probably expired.";
		}
}
?>
</body>

</html>

Posted: Sun Mar 12, 2006 6:59 am
by Chris Corbyn
Use a multipart/mixed mime-type message with base64 encoding:

Code: Select all

$attachment_file = 'foo.zip';

$file_data = file_get_contents($attachment_file);
$file_base64 = base64_encode($file_data);

$message = <<<MESSAGE
Content-type: multipart/mixed; boundary=SOME-RANDOM-STRING

--SOME_RANDOM_STRING
Content-type: text/plain

Your main message here

--SOME-RANDOM-STRING
Content-type: application/zip
Content-Transfer-Encoding: base64

$file_base64

--SOME-RANDOM-STRING--
MESSAGE;

Attachments

Posted: Sun Mar 12, 2006 9:22 am
by eXpertPHP
Ok, if the attachment file it is your problem, then you should try a simple example using XPertMailer:

Code: Select all

<?php 

set_time_limit(0);

// path to XPertMailer class file
require_once 'XPertMailer.php';

$mail = new XPertMailer;

// 'upload' is the name of the input type file from your HTML form, optionaly, you can put
// 'filename.zip' as the new name of the file, you will see this name in the recived e-mail attachment
$mail->attach(array('filename.zip' => $_FILES['upload']['tmp_name']));

$mail->from('my@address.net');

$send = $mail->send('toclient@domain.com', 'subject', 'text/plain message');

echo $send ? "Done." : "Error.";

?>

Posted: Sun Mar 12, 2006 12:02 pm
by transfield
Thanks for your reply d11wtq. How do I insert this code into my page?