0

I am trying to back up my database and have the zip file emailed to me. The following code works well except it doesn't attach the zip. The mail and everything works fine and the zip file is created but I can't attach it.

$today = date("d/m/Y");
$to      = 'Chris P <chris@*****.co.uk>';
$subject = 'Back Up File - '.$today;
$headers = 'From: Backup <backup@******.co.uk>' . "\r\n" .
$headers.= 'Reply-To: ****** (Chris) <chris@******.co.uk>' . "\r\n" .
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1 \r\n";
$random_hash = md5(date('r', time()));
$attachment = chunk_split(base64_encode(file_get_contents($fileName)));
ob_start(); //Turn on output buffering


$message2  = "<font face='verdana' size='-2'>Hey Chris,<p>";
$message2 .= "Here is the back up of the database, taken on the <strong>$today.</strong>\n\n"; 
$message2 .= "<p>The archive has the name of:  <strong>$fileName</strong> and it's file-size is <strong>$fileSize.</strong>\n\n"; 
$message2 .= "Please find the file attatched.\n\n";
$message2 .= "<p>****** BackUp Generator ";

$message2 .= "--PHP-mixed- $random_hash;";
$message2 .= "Content-Type: application/zip; name=\"$filename\""; 
$message2 .= "Content-Transfer-Encoding: base64"; 
$message2 .= "Content-Disposition: attachment";
$message2 .= $attachment;
$message2 .= "--PHP-mixed-<?php echo $random_hash; ?>--";
JJJ
  • 32,902
  • 20
  • 89
  • 102
Chris P
  • 51
  • 2
  • 6
  • For one, you can't use `` tags inside what's already PHP code. – JJJ Aug 06 '13 at 13:18
  • u can find what you need here...http://webcheatsheet.com/php/send_email_text_html_attachment.php...just for reference – sanj Aug 06 '13 at 13:20
  • Throw away all that code, and download the phpMailer class. Save yourself a whole lot of time and effort. – Spudley Aug 06 '13 at 13:20
  • Thanks Sanj, that link doesn't work. Juhana will edit my code now and try again. – Chris P Aug 06 '13 at 13:21

1 Answers1

0

Instead of trying to roll your own MIME encoding, you might want to use phpmailer. Easy to use, simple to install - just 3 php files to copy to your directory. See https://github.com/PHPMailer/PHPMailer. You can use the example on this page as a boilerplate for what you are trying to do.

mti2935
  • 11,465
  • 3
  • 29
  • 33