2012-03-25 2 views
1

Ich teste eine automatische Antwort Multipart E-Mail mit PHP. Das Problem ist, wenn ich die E-Mail in meiner hotmil erhalte, erscheint der gesamte Inhalt (einschließlich HTML und zufälliger Hashes) als Rohtext. Hier ist mein Code:PHP Text/HTML Multipart E-Mail erscheint als roher Text in Hotmail

$cname = "test"; 
$to = "[email protected]"; 
$autoReplyTo = "[email protected]"; 
$autoReplySubject = "Enquiry"; 

$mime_boundary = md5(date('U')); 

$autoReplyheaders = "From: XXXXX <" . $to . ">" . "\r\n" . 
        "MIME-Version: 1.0" . "\r\n" . 
        "Content-Type: multipart/alternative; boundary=$mime_boundary" . 
        "Content-Transfer-Encoding: 7bit". "\r\n"; 


$plain_text = "Dear " . $cname . ".\r\n"; 
$plain_text = "Thank you for contacting us.\r\n"; 
$plain_text .= "We are currently processing your query and will contact you shortly. We appreciate the time and interest you have shown in our company.\r\n"; 
$plain_text .= "Sales Team\r\n"; 
$plain_text .= "Note: This is an auto-generated email, please do not reply.\r\n"; 


$html_text = '<html><head><title>AUTO REPLY</title></head><body>'; 
$html_text .= '<p><img src="http://www.xxxxxx.xx/images/logo.png" /></p>'; 
$html_text .= '<p>Dear '.$cname.',<br /> 
    Thank you for contacting us.<br /> 
    We are currently processing your query and will contact you shortly.<br /> 
    We appreciate the time and interest you have shown in our company.</p>'; 

$html_text .= '<p><b>Sales Team</b></p>'; 
$html_text .= '<p><i>Note: This is an auto-generated email, please do not reply.</i></p>'; 
$html_text .= '</body></html>'; 


$autoReplyMessage = "Auto reply" . "\r\n\r\n". 
"--" . $mime_boundary. 
"Content-Type: text/plain; charset=\"iso-8859-1\"". 
"Content-Transfer-Encoding: 7bit". "\r\n\r\n". 
$plain_text. 
"--" . $mime_boundary. 
    "Content-Type: text/html; charset=\"iso-8859-1\"". 
    "Content-Transfer-Encoding: 7bit". "\r\n\r\n". 
$html_text. 
"--".$mime_boundary."--"; 


mail($autoReplyTo, $autoReplySubject, $autoReplyMessage, $autoReplyheaders); 

Was mache ich falsch?

+2

Wenn Sie nicht alle RFCs lesen möchten - warum verwenden Sie keine Bibliotheken (wie phpmailer), die eine wohlgeformte und korrekte E-Mail senden können? – zerkms

+0

check out hier: http://StackOverflow.com/a/9860369/1104695 – guybennet

Antwort

1

Dies kann oder kann nicht das einzige Problem sein, aber Sie vermissen Zeilenumbrüche nach den Content-Type Header in der Ebene und HTML Sektionen:

$autoReplyMessage = "Auto reply" . "\r\n\r\n". 
"--" . $mime_boundary. 
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n". 
// ----------------------------------------------^^^^^ 
// Added \r\n 
"Content-Transfer-Encoding: 7bit". "\r\n\r\n". 
$plain_text. "\r\n". 
//---------^^^^^^^^^ 
// Added another linebreak before MIME boundary 
"--" . $mime_boundary. 
    "Content-Type: text/html; charset=\"iso-8859-1\"\r\n". 
// ----------------------------------------------^^^^^ 
// Added \r\n 
    "Content-Transfer-Encoding: 7bit". "\r\n\r\n". 
$html_text."\r\n\r\n". 
// ---------^^^^^^^^^^ 
"--".$mime_boundary."--"; 

Anstatt zu versuchen, Nachrichten multipart/Mime manuell Handwerk, viele von uns hier auf SO würde eine Mailing-Bibliothek wie PHPMailer empfehlen, die dies viel einfacher handhabt. Das manuelle Erstellen von Nachrichten neigt dazu, ziemlich fehleranfällig zu sein, und unterliegt Inkonsistenzen zwischen Implementierungen durch den SMTP-Server oder Unterschieden zwischen nativen Plattform-Zeilenumbrüchen.

+0

hmm .. zusätzlichen Zeilenumbruch .. immer noch das gleiche Problem. Werde versuchen PHPMailer auszuprobieren. Danke – Shahid

+0

Auch mein Gastgeber läuft PHP 4.4.7. Ich frage mich, ob das ein Problem sein wird. – Shahid

+0

@Shahid Wenn ich nochmal drüber schaue, sehe ich auch, dass Sie nur einen Zeilenumbruch hinter dem '$ plain_text'-Teil hatten, der Teil der Zeichenkette' $ plain_text' war. Ich fügte einen weiteren Zeilenumbruch vor dem '- $ mime_boundary' hinzu, der auf' $ plain_text' folgt. Das ist wahrscheinlich der Grund ... Außerdem benötigt man zwei Zeilenumbrüche nach '$ html_text' vor dem letzten' - $ mime_boundary - ' –

Verwandte Themen