2010-08-07 66 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