note 78208 rejected from ref.mail by ezyang
| From: | ezyang@php.net | Date: | Wed, 03 Oct 2007 23:29:52 +0000 |
| Subject: | note 78208 rejected from ref.mail by ezyang | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131058@lists.php.net to get a copy of this message | ||
Note Submitter: brottmayer at gmail dot com
----
I am trying to send an e-mail with an attachment and I am clueless as to what might be the problem!
I have the following code, that works to send the e-mail as well as show that there is an
attachment, but the attachment is a blank file and not the one uploaded. How can I do this?
Here's the code:
<?php
//define the receiver of the email
$to = 'brottmayer@gmail.com';
//define the subject of the email
$subject = Trim(stripslashes($_POST['subject']));;
//define the sender of the email
$from = Trim(stripslashes($_POST['from']));;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
//Userfile information
$filename = $_FILES["userfile"]["name"]; // gives you the name of the file they
uploaded
$filetype = $_FILES["userfile"]["type"]; // gives you the size of the upload in
bytes
$filetemp = $_FILES["userfile"]["tmp_name"]; // gives you the temporary name of
the file on the server until it is renamed
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$data = base64_encode($filename);
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
Address is: <?php echo $_POST["address"]; ?>
Phone Number is: <?php echo $_POST["phone"]; ?>
Birthdate is: <?php echo $_POST["bdate"]; ?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
<h1>Address is: <?php echo $_POST["address"]; ?></h1>
<p>Phone Number is: <?php echo $_POST["phone"]; ?><br />
Birthdate is: <?php echo $_POST["bdate"]; ?></p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: $filetype; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $data; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail
failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>