Hi,
I have a php mailer script (not phpmailer!) and it successfully sends an email… however I need to attach a file to the email rather than a generic ‘attachment.txt.’ file.
I have a html form that the user can enter an email address, file_name and message - the message and email both work ok. My problem is that the file_name does not work.
Here is my current code;-
[php]<?php
if (isset($_POST[‘submit’]))
{
$file_name = $_POST[‘file_name’];
$doc_email = $_POST[‘doc_email’];
$doc_message = $_POST[‘doc_message’];
//define the receiver of the email
$to = $_POST[‘doc_email’];
//define the subject of the email
$subject = ‘Email with attachment’;
//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: [email protected]\r\nReply-To: [email protected]”;
//add boundary string and mime type specification
$headers .= “\r\nContent-Type: multipart/mixed; boundary=“PHP-mixed-”.$random_hash.”"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents(‘uploads/$_POST[$file_name’])));
//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.
User wrote:-
<?php echo $doc_message; ?>–PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset=“iso-8859-1”
Content-Transfer-Encoding: 7bit
Hi there
,This is something with HTML formatting.
User wrote:-
<?php echo $doc_message; ?>
–PHP-alt-<?php echo $random_hash; ?>–
–PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/txt; name=“attachment.txt”
Content-Transfer-Encoding: base64
Content-Disposition: attachment
–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 ); //redirect user after success header("Location: index.php"); } ?>[/php]the $_POST[file_name] would be something like ’ 123.pdf’ within a sub folder called uploads.
Any ideas please?