2013-08-06 2 views
0

데이터베이스를 백업하고 zip 파일을 전자 메일로 보내려고합니다. 다음 코드는 zip을 첨부하지 않는다는 점을 제외하고는 잘 작동합니다. 메일과 모든 것이 잘 작동하고 zip 파일이 생성되었지만 첨부 할 수 없습니다.PHP 메일에 첨부 파일 추가하기

$today = date("d/m/Y"); 
$to  = 'Chris P <[email protected]*****.co.uk>'; 
$subject = 'Back Up File - '.$today; 
$headers = 'From: Backup <[email protected]******.co.uk>' . "\r\n" . 
$headers.= 'Reply-To: ****** (Chris) <[email protected]******.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; ?>--"; 
+0

이미 PHP 코드 안에 ''태그를 사용할 수 없습니다. – JJJ

+0

여기에서 필요한 것을 찾을 수 있습니다 ... http : //webcheatsheet.com/php/send_email_text_html_attachment.php... 참조 용 – sanj

+0

모든 코드를 버리고 phpMailer 클래스를 다운로드하십시오. 많은 시간과 노력을 아끼지 마십시오. – Spudley

답변

0

고유 한 MIME 인코딩을 사용하는 대신 phpmailer를 사용할 수 있습니다. 사용하기 쉽고 설치가 간단합니다. 디렉토리에 복사 할 수있는 3 개의 PHP 파일 만 있습니다. https://github.com/PHPMailer/PHPMailer을 참조하십시오. 이 페이지의 예제를 사용자가하려는 작업의 상용구로 사용할 수 있습니다.

관련 문제