使用PHPMailer发送纯文本电子邮件

时间:2009-07-14 07:46:33

标签: php email formatting phpmailer plaintext

使用PHPMailer发送纯文本电子邮件时遇到问题。

我有从文本文件中读取的文本,并通过PHPMailer将其邮寄给邮件收件人

当收件人收到实际的电子邮件时,邮件的格式与文本文件中的格式不同,所有内容都在一行中,我发送的电子邮件中不包含新行和标签。文字包装完全关闭。

代码:

        $mail->ContentType = 'text/plain'; 
        $mail->IsHTML(false);
        $address = "test@test.com";
        $mail->AddAddress($address, "John Doe");

        $mail->SetFrom(EMAIL_TEST_FROM);

        $mail->AddReplyTo(EMAIL_TEST_REPLY);



        $mail->Subject = $action." REGISTRATION ".$formName.$tld;
        $mail->From = EMAIL_TEST;  

        $mail->MsgHTML(file_get_contents($newFile));


        if($mail->Send()){
            return true;
        }

3 个答案:

答案 0 :(得分:23)

您正在将$mail->MsgHTML()设置为纯文本邮件,并且由于HTML中忽略了空白格式,因此您将获得内嵌文本。

我暂时没有使用PHPMailer,但是从内存中尝试:

$mail->Body = file_get_contents($newFile); 

答案 1 :(得分:10)

    $mail->ContentType = 'text/plain'; 
    $mail->IsHTML(false);
    $address = "test@test.com";
    $mail->AddAddress($address, "John Doe");

    $mail->SetFrom(EMAIL_TEST_FROM);

    $mail->AddReplyTo(EMAIL_TEST_REPLY);



    $mail->Subject = $action." REGISTRATION ".$formName.$tld;
    $mail->From = EMAIL_TEST;  

    // Very important: don't have lines for MsgHTML and AltBody 
    $mail->Body = file_get_contents($mailBodyTextFile);  
    // $mail->Body = $_POST["msg"];  //If using web mail form, use this line instead.


    if($mail->Send()){
        return true;
    }

答案 2 :(得分:0)

尝试以下可正常运行的代码:

        try {
            $mail->AddAddress('jitpal@domain.com', 'Jit Pal');
            $mail->SetFrom('testuser@domain.com', 'Test User');
            $mail->Subject = "All machine's tests.";
            $mail->Body = "All machine's tests working fine.";
            $mail->Send();
            echo "<br/>Message sent successfully...<br/><br/>\n";
        } catch (phpmailerException $e) {
            echo $e->errorMessage();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
相关问题