2010-08-07 70 views
1

終於得到了所有的錯誤,現在他們說:「哦,我們需要添加附件......」所以,這個發送一個純文本版本的html郵件,並且只是在膨脹。現在我有附件到達郵件客戶端顯示純文本版本內聯和html版本作爲另一個附件,然後看似空的93字節文件名稱像ATT00248.txt。PHP mail() - 一旦添加文件附件,HTML將作爲附件顯示

任何人都可以從後面打我的頭或告訴我我要去哪裏錯了嗎?我希望在郵件用戶界面中可用的HTML內聯,HTML不可用的純文本版本以及單個附件作爲附件。

任何幫助?

<?php 
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile']; 
ob_start(); 
include($template); 
$html = ob_get_contents(); 
ob_end_clean(); 

if (strlen($html) == 0) { 
    echo "The template at $template did not load."; 
    exit; 
} 

$email = $_SESSION['user']->email; 
$name = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name; 
$from = "$name <$email>"; 
$subject = unslash($_SESSION['subject']); 

$TextMessage = strip_tags(unslash($_SESSION['message'])); 

$notice_text = "This is a multi-part message in MIME format."; 
$plain_text = str_replace('&nbsp;',' ', $TextMessage); 

if ($_SESSION['attachment']) { 
    $fileatt = 'files/' . $_SESSION['attachment']; 
    $file = fopen($fileatt,'rb'); 
    $data = fread($file,filesize($fileatt)); 
    fclose($file); 
    $data = chunk_split(base64_encode($data)); 
    $mailtype = 'mixed'; 

    $fileatt_type = "application/octet-stream"; 
    $fileatt_name = $_SESSION['attachment']; 
} else { 
    $mailtype = 'alternative'; 
} 

$semi_rand = md5(time()); 
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand"; 
$mime_boundary_header = chr(34) . $mime_boundary . chr(34); 

$body = "$notice_text 

--$mime_boundary 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

$plain_text 

--$mime_boundary 
Content-Type: text/html; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

$html 

--$mime_boundary 
"; 

$body .= "Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
"Content-Disposition: attachment;\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" . 
"--$mime_boundary\n"; 



// #1 // 
if ($to = $_SESSION['recipients'][0]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 

    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 


// #2 // 
if ($to = $_SESSION['recipients'][1]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #3 // 
if ($to = $_SESSION['recipients'][2]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #4 // 
if ($to = $_SESSION['recipients'][3]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #5 // 
if ($to = $_SESSION['recipients'][4]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// CC self? // 
if ($_SESSION['cc_me']) { 
    mail($from, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($from) . ".<br />"; 
} 

if ($fileatt) { 
    unlink($fileatt); 
} 

echo "<a href='email_start.php'>Click here</a> to send another email."; 
list($_SESSION['email'], $_SESSION['subject'], $_SESSION['bullets'], $_SESSION['message'], 
    $_SESSION['templateFile'], $_SESSION['template'], $_SESSION['cc_me'], $_SESSION['recipients']) = ''; 
?> 
+1

你有沒有考慮過使用現成的郵件類如Swiftmailer? http://swiftmailer.org/ – 2010-08-07 19:10:53

+0

Pekka-謝謝你!我很喜歡滾動my6自己我有時會讓自己壓力很大! swiftmailer使工作變得簡單和無壓力。謝謝!!!你應該添加這個作爲答案,我會選擇它! – jerrygarciuh 2010-08-08 17:35:38

回答

0

我會張貼此評論,但它太長。

// #1 // 
if ($to = $_SESSION['recipients'][0]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 

    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 


// #2 ... #3 ... #4 ... #5 

將最終執行所有塊,因爲($to = $_SESSION['recipients'][0])將始終爲真。即使mail()失敗,它也會顯示「發送給...的電子郵件」。

你想要的是:

if (in_array($to, $_SESSION['recipients'])) { 
    if (mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header)) { 
     echo "Email sent to " . htmlentities($to) . ".<br />"; 
    } 
} 

或者,如果你真的想大家郵寄或

foreach ($_SESSION['recipients'] as $to) { 
    if (mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header)) { 
     echo "Email sent to " . htmlentities($to) . ".<br />"; 
    } 
} 
+0

感謝您的回覆。我應該已經發布了一個鏈接: http://stackoverflow.com/questions/3419858/php-mail-sends-empty-content-when-looping-over-array-of-addresses 作爲解釋爲什麼我重複了我的做法。 – jerrygarciuh 2010-08-07 20:07:24

+0

標題應該用「\ r \ n」分隔。這是smtp標準的一部分,不可選。 rfc 5321:2.3.8。行 – ThatGuy 2011-07-25 17:32:04