Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Plain text emails using PHPMailer

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;
        }
like image 413
Elitmiar Avatar asked Jul 14 '09 07:07

Elitmiar


People also ask

How do you send plain text and HTML email simultaneously?

GroupMail allows senders to send both an HTML and plain-text version of their email at the same time. This is done using a format called Multi-Part MIME. When a multi-part MIME email (read: an email that includes both an HTML and plain-text part) is created in GroupMail, they will be sent out together as one message.

Why it is advantages to use PHPMailer for sending and receiving email?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.

How many emails can I send with PHPMailer?

Its called SMTP Relays and it is defined on a per (sending) email basis but usually defaults to 250.


3 Answers

    $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;
    }
like image 41
elim Avatar answered Sep 18 '22 13:09

elim


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); 
like image 70
bumperbox Avatar answered Sep 21 '22 13:09

bumperbox


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();
        }
like image 31
Jitesh Sojitra Avatar answered Sep 22 '22 13:09

Jitesh Sojitra