• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[CI 1.7 rev 1572+] Email Class - html email problems

#1
[eluser]a&w[/eluser]
I went through the various threads for people having problems sending html emails. Mostly a lot of issues with carriage returns or having problems finding the config file, etc. Originally I was trying to play with all those things (crlf, etc.) to no avail, so I decided to dig through the code.

I should probably state the following platform conditions just to be complete:
Apache on Windows Box, Outlook 2007 MUA, and using mail protocol.

What I found in the Email class was really confusing how it would work for anyone. Basically it appears that when sending html emails the Email class would put part of the message (the alternate message) in the header, and the other part (the html message) in the body. What I had read elsewhere is to put both the alternate and html messages in the body, and let the MUA pick which one of those "body" messages to use.

Using the original version of the Email class I had all sorts of weird things happening, carriage return problems in the message and subject line, html tags coming through, part of both message types would show up in the body, etc. etc.

I noticed some inconsistency in the library's handling and took a stab at fixing it. I just attached my version since I figured that would make it easiest for someone to do a diff on it. I created two little helper methods just so the repetitive coding could be eliminated and make it easier to maintain.

Anyway, with the attached changes, all of the problems I mentioned having were solved. I have a whole bunch test emails showing the source code of the emails and how screwed up it was, but I'll refrain from posting unless someone requests it. I think the attached code illustrates the problem sufficiently by itself.

I'll post a peek of the portion of the code I altered in the next post. (Post #3 has corrected version of code).

#2
[eluser]a&w[/eluser]
Code:
//new method for maintainability/consistency, etc.
    function _get_content_type_header($type = 'html')
    {
        if ($type == 'multi') {
            return "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
        }
        return "Content-Type: text/".$type."; charset=" . $this->charset . $this->newline;
    }

//new method for maintainability/consistency, etc.
    function _get_content_encoding_header($type = 'data')
    {
        if ($type == 'quoted-printable') {
            return "Content-Transfer-Encoding: quoted-printable";
        }
        return "Content-Transfer-Encoding: " . $this->_get_encoding();
    }

    /**
     * Build Final Body and attachments
     *
     * @access    private
     * @return    void
     */
    function _build_message()
    {
        if ($this->wordwrap === TRUE  AND  $this->mailtype != 'html')
        {
            $this->_body = $this->word_wrap($this->_body);
        }

        $this->_set_boundaries();
        $this->_write_headers();

        $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';

        switch ($this->_get_content_type())
        {
            case 'plain' :

//              $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                $hdr .= $this->_get_content_type_header($type = 'plain');
//              $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
                $hdr .= $this->_get_content_encoding_header();

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;
                    $this->_finalbody = $this->_body;

                    return;
                }

                $hdr .= $this->newline . $this->newline . $this->_body;

                $this->_finalbody = $hdr;
                return;

            break;
            case 'html' :

                if ($this->send_multipart === FALSE)
                {
//                    $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                    $hdr .= $this->_get_content_type_header($type = 'html');
//                    $hdr .= "Content-Transfer-Encoding: quoted-printable";
                    $hdr .= $this->_get_content_encoding_header('quoted-printable');
                }
                else
                {
                    // add one additional header of Content-type:multipart/alternative and
                    // boundary string that marks the different areas of the email.
                    // Note that the content type of the message itself is sent as a mail header,
                    // while the content types of the individual parts of the message are embedded
                    // in the message itself.
                    // This way, mail clients can decide which part of the message they want to display.
//                    $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
                    $hdr .= $this->_get_content_type_header($type = 'multi');
                    $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;

//NOTE to CI Developers:  originally the plain (alternate) type message was included in the header
//instead of being included as an alternate message in the message body?!
//I broke this out into different variables to it was easier to see here:
                    // the alternate (plain) part of the message:
                    $alt  = "--" . $this->_alt_boundary . $this->newline;
//                  $alt .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                    $alt .= $this->_get_content_type_header($type = 'plain');
//                    $alt .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
                    $alt .= $this->_get_content_encoding_header() . $this->newline . $this->newline;
                    // add the message body:
                    $alt .= $this->_get_alt_message() . $this->newline . $this->newline;

                    // the html part of the message:
                    $html  = "--" . $this->_alt_boundary . $this->newline;
//                  $html .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                    $html .= $this->_get_content_type_header($type = 'html');
//NOTE to CI Developers:  the two trailing newlines were missing here.
//                  $html .= "Content-Transfer-Encoding: quoted-printable";
                    $html .= $this->_get_content_encoding_header('quoted-printable') . $this->newline . $this->newline;
                    // add the message body:
                    $html .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
                }

//this needs to be altered slightly because $alt, $html won't be defined for one case above.
//                $this->_body = $this->_prep_quoted_printable($this->_body);
                $this->_body = $alt . $html;

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;
//                    $this->_finalbody = $this->_body . $this->newline . $this->newline;
                    $this->_finalbody = $this->_body;

#3
[eluser]a&w[/eluser]
I noticed the above and attached will have $html undefined after posting. Probably just needs a little tweak:
Code:
case 'html' :

                $this->_body = $this->_prep_quoted_printable($this->_body);

                if ($this->send_multipart === FALSE)
                {
                    ...
                }
                else
                {
                    ...
                    $this->_body = $alt . $html . $this->_body;
                }


                if ($this->_get_protocol() == 'mail')

#4
[eluser]a&w[/eluser]
Here's modified version based on above comments.

#5
[eluser]rt30000[/eluser]
Thank you so much for posting, a&w. I have another thread where I was running across several issues with the email class. This library simply dropped in and resolved all my issues. Thanks! I wonder if CI will release an updated file that resolves these issues you have apparently fixed...

#6
[eluser]a&w[/eluser]
Dunno. You are to first to reply/acknowledge my post. I figured I was doing something wrong. I see a few people have downloaded the attachment, would have been nice if they would confirm the bug and say if the attached remedied the problem or not.

#7
[eluser]Hibiscus[/eluser]
I was having some weird email issues too, with the html message getting strangely garbled. This seems to have solved my problems.

Nice job.

#8
[eluser]Unknown[/eluser]
Your class solved my problems too, thanks a lot!

My clients were complaining about strange characters appearing in the message body (=3D and such). It so happened that the text/html part (in quoted-printable) was being printed as if it were an 8-bit encoded message.

#9
[eluser]rhasan[/eluser]
Hi a&w,

Thanx for your code. I have replace CI Email class by your class. Now I am getting the following error :

Code:
A PHP Error was encountered

Severity: Notice

Message: fwrite() [function.fwrite]: send of 11 bytes failed with errno=32 Broken pipe

Filename: libraries/Email.php

Line Number: 1832

---------------------
etc

and also shows the error :
Code:
451-The server has reached its limit for processing requests from your host. 451 Please try again later.

hello:

The following SMTP error was encountered:
Unable to send data: AUTH LOGIN
Failed to send AUTH LOGIN command. Error:
Unable to send data: MAIL FROM:
from:
The following SMTP error was encountered:
Unable to send data: RCPT TO:
to:
The following SMTP error was encountered:
Unable to send data: DATA

data:

--------------
etc

your help would be appreciable

#10
[eluser]rhasan[/eluser]
Hmm...I am not using CI Email class and switched to PHPMailer. Now everything works. I have followed the tutorial described here http://codeigniter.com/wiki/PHPMailer/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.