2010-08-07 3 views
1

마지막으로 모든 버그를 확인한 후 "오, 첨부 파일을 추가해야합니다 ..."라고 말했습니다. 플레인 텍스트 버전의 html 메일을 보냈을뿐입니다. 이제 메일 클라이언트에 도착한 첨부 파일이 일반 텍스트 버전으로 표시되고 html 버전이 다른 첨부 파일로 표시되고 ATT00248.txt와 같은 이름의 93 바이트 파일이 표시됩니다.PHP 메일() - 파일 첨부가 추가되면 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/ –

+0

Pekka- 고마워요! 나는 my6 롤링에 열중하고있어, 때때로 나는 스트레스를 받는다! swiftmailer는 일을 간단하고 스트레스없이 해주었습니다. 고맙습니다!!! 이것을 답으로 추가해야하고 그것을 선택하겠습니다! – jerrygarciuh

답변

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])은 항상 true이므로 모든 블록이 실행됩니다. 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

+0

머리글은 "\ r \ n"으로 구분해야합니다. 이것은 smtp 표준의 일부이며 OPTIONAL이 아닙니다. rfc 5321 : 2.3.8. 라인 – ThatGuy

관련 문제