Sending Plain text emails using PHPMailer
Asked Answered
A

3

21

I have a problem sending plain text emails using PHPMailer.

I have text that I read from a text file and mail it to mail recipient via PHPMailer

When the recipient gets the actual email, the formatting of the mail is not like in the text file, everything is in one line, no new lines and tabs are included in the email that I send. Text wrapping is totally off.

Code:

        $mail->ContentType = 'text/plain'; 
        $mail->IsHTML(false);
        $address = "[email protected]";
        $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;
        }
Antisemite answered 14/7, 2009 at 7:46 Comment(2)
It's a domain registration file, whois.co.za, but the problem has been sorted. Thx JamesAntisemite
You can simplify the if($mail->Send()) part to return $mail->Send();.Asuncion
S
26

You are setting $mail->MsgHTML() to a plain text message, and since whitespace formatting is ignored in HTML, you're getting an inline text.

I haven't used PHPMailer for a while, but from memory try:

$mail->Body = file_get_contents($newFile); 
Streamy answered 14/7, 2009 at 7:51 Comment(0)
M
13
    $mail->ContentType = 'text/plain'; 
    $mail->IsHTML(false);
    $address = "[email protected]";
    $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;
    }
Mecham answered 24/1, 2013 at 16:34 Comment(1)
+1 for the explaination of what lines to exclude as well as what to include to trigger text-only.Urana
I
1

Try below code which works fine:

        try {
            $mail->AddAddress('[email protected]', 'Jit Pal');
            $mail->SetFrom('[email protected]', '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();
        }
Ishmul answered 24/5, 2015 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.