The following script does everything I want except email the text in $message.
Based on a few variables it will send an email to a user with a file that is attached and located on the server's root directory (cannot be downloaded via HTTP).
What is wrong with the following script?
Do I have to change the content type to "text/html" or is that already done correctly? Also, if I do, how do I do it?
Thanks.
<?php
// define the url to this script
$script_url = "index.php?act=page&pg=download";
// rename id variable
$id = $_GET['id'];
// determine whether to send email or show link
if(isset($id))
{
// rename member id
$member = $GLOBALS['ibforums']->member['id'];
// define query to get file details
$email_sql = "SELECT * FROM `ibf_download` WHERE `downloadid` = '".$id."' LIMIT 1";
$email_query = mysql_query($email_sql) or die(mysql_error());
// define query to get member details
$member_sql = "SELECT * FROM `ibf_members` WHERE `id` = '".$member."' LIMIT 1";
$member_query = mysql_query($member_sql) or die(mysql_error());
// parse file details into an array
while($email_row = mysql_fetch_array($email_query))
{
// rename file details
$dir = $email_row['dir'];
$name = $email_row['name'];
$size = $email_row['size'];
}
// parse member details into an array
while($member_row = mysql_fetch_array($member_query))
{
// rename member details
$to = $member_row['email'];
$username = $member_row['name'];
}
// define email settings
$subject = "Attachment: $name";
$from = "Da Bricks <webmaster@dabricks.org>";
$message = "Hello, <br><br>Attached is the file you have requested from Da Bricks.<br><br>";
$message .= "If no file is attached please reply back to this email and the file will be email manually.";
$message .= "<br><br>Thanks, <br>Da Bricks";
$message .= "\r\n\r\n";
$border_random = md5(time());
$mail_boundary = "x{$border_random}x";
$body = "";
$headers = "From: {$from}\r\n";
$headers .= "To: {$to}\r\n";
$headers .= "Reply-to: {$from}\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"{$mail_boundary}\"\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n\r\n";
$headers .= "--{$mail_boundary}\r\n";
$headers .= "Content-type: text/plain; charset=\"iso-8859-1\"\r\n";
$headers .= "Content-Transfer-Encoding:7bit\r\n\r\n";
$headers .= "{$message}\r\n\r\n";
$file_type = "application/x-shockwave-flash";
$file_name = basename($dir);
$file_size = filesize($dir);
$fp = fopen($dir,"rb");
$data = fread($fp,$file_size);
fclose($fp);
$base64 = base64_encode($data);
$file = chunk_split($base64);
$headers .= "--{$mail_boundary}\r\n";
$headers .= "Content-type: {$file_type}; name=\"{$file_name}\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"{$file_name}\"\r\n\r\n";
$headers .= $file."\r\n\r\n";
$headers .= "--{$mail_boundary}--\r\n";
// Send email to member with attachment
$email_file = mail($to,$subject,$body,$headers);
if(!$email_file)
{
// Output failure status
echo "To: $to <br>";
echo "Subject: $subject <br>";
echo "Message: $message <br>";
echo "Headers: $headers";
exit;
}else{
// Output success status
echo "Successfully emailed $file_name to <a href='index.php?showuser={$member}'>{$username}</a> at <a href='mailto:{$to}'>$to</a>.";
echo "<br>Please check your inbox for it.";
}
}else{
// show links
// define query
$links_sql = "SELECT * FROM `ibf_download`";
$links_query = mysql_query($links_sql) or die(mysql_error());
// Parse into array
while($links_row = mysql_fetch_array($links_query))
{
echo "<a href='{$script_url}&id={$links_row['downloadid']}'>{$links_row['name']}</a><br>";
}
}
?>