0

我正在尝试备份我的数据库并将 zip 文件通过电子邮件发送给我。以下代码运行良好,但不附加 zip。邮件和一切工作正常,并创建了 zip 文件,但我无法附加它。

$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; ?>--";
4

1 回答 1

0

与其尝试使用自己的 MIME 编码,不如使用 phpmailer。易于使用,易于安装 - 只需 3 个 php 文件即可复制到您的目录。见https://github.com/PHPMailer/PHPMailer。您可以将此页面上的示例用作您尝试执行的操作的样板。

于 2013-08-06T13:20:48.903 回答