8

Wanted to know if there are any functions/classes/etc.. to help with the 990 character limitation for email as my HTML is being effected due to this.

The Problem: (Source)

Note that mailservers have a 990-character limit on each line contained within an email message. If an email message is sent that contains lines longer than 990-characters, those lines will be subdivided by additional line ending characters, which can cause corruption in the email message, particularly for HTML content. To prevent this from occurring, add your own line-ending characters at appropriate locations within the email message to ensure that no lines are longer than 990 characters.

Anyone else seem to have this problem? and how did you fix this?

Sounds like I need to find a good place to split my HTML and manually add a line break, ugh...

UPDATE:

It's tablature data with many rows. So do I need to add a \n or <br /> somewhere?

UPDATE #2: Adding MIME Type Code

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n"; // added this, but still no results
$headers .= "From: from@email.com\r\n";

Here is how I'm calling the function(s):

How I originally called:

return $html;

What I tried:

return imap_8bit($html); // not working, nothing is captured in the error log

AND

return imap_binary($html); // not working, nothing is captured in the error log

UPDATE #3 (Adding Mail Function)

try {
    mail(
        'to@email.com',
        'Subject of Email',
        $html,
        $headers
        );
    } catch (Exception $e) {
        echo ("ERROR: Email NOT sent, Exception: ".$e->getMessage());
    }

Example HTML (This is the message of the HTML email) (This is also in a class that is part of a XMLRPC service)

private function getHTML() {
    $html  = '<html><head><title>Title</title></head><body>';
    $html .= '<table>';
    $html .= '<tr><td>many many rows like this</td></tr>';
    $html .= '<tr><td>many many rows like this</td></tr>';
    $html .= '<tr><td>many many rows like this</td></tr>';
    $html .= '<tr><td>many many rows like this</td></tr>';
    $html .= '<tr><td>many many rows like this</td></tr>';
    $html .= '</table>';
    $html .= '</body>';
    $html .= '</html>';

    return $html;
    //return imap_8bit($html); // not working, nothing is captured in the error log
    //return imap_binary($html); // not working, nothing is captured in the error log
    // Both of these return the XMLRPC Fault Exception: 651 Failed to parse response
}

Fault Exception: 651 Failed to parse response basically doesn't like the format or how the data is returned.

Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • May I ask what you're doing in HTML that's can't have a line break? You _can_ split mid-tag between attributes and have no effect on markup. – Brad Christie Jan 19 '11 at 16:58
  • so I guess my question is, do I have to enter in line break like this: \n or with the HTML
    work? Also I wanted to maintain HTML W3C validation as much as I can
    – Phill Pafford Jan 19 '11 at 17:00
  • You don't need to worry about the HTML, as long as you correctly use the functions I mentioned in my answer. – Alix Axel Jan 19 '11 at 17:07
  • it has nothing to do with your HTML breaks. `
    ` is irrelevant here. The solution is to base64 encode, but if you don't want to do that, a simple `\r\n` will do.
    – Brad Jan 19 '11 at 17:29

3 Answers3

7

You can put your content through the wordwrap() function so that you don't manually have to insert newlines.

Have you considered using one of the many mail libraries available? PHPMailer, PEAR Mail, SwiftMailer, etc...?

jasonbar
  • 13,333
  • 4
  • 38
  • 46
  • wouldn't wordwrap() add the characters itself to the viewable html? The data is in a table with several rows of data, I'm not sure if adding the newline or
    would cause any display issues on how the data is formated
    – Phill Pafford Jan 19 '11 at 17:39
  • @Phill Pafford, Unless he is using `
    ` tags using `wordwrap()` to wrap the text won't impact the display. This is what we do at the ESP I work for, and it's how the mail libraries do it as well.
    – jasonbar Jan 19 '11 at 17:42
  • I think the wordwrap() might actually work, testing it now. BTW what is the default action if you just use wordwrap($html); with no parms? Looks to split the string only of spaces (which is what I would like and am testing for right now), just wanted to confirm – Phill Pafford Jan 19 '11 at 18:00
  • 1
    @Phill Pafford, Correct, `wordwrap($html)` will just insert `\n`'s into the string every 75 characters (or less, depending on wordbreaks.) It won't break words up. – jasonbar Jan 19 '11 at 18:32
  • 1
    I think this is the correct answer. I was getting spaces inserted after 990 characters in emails sent via php's mail() function. I added the following code (after this answer) and my problem went away: mail($to, $subject, wordwrap($message, 75, "\n", true), wordwrap($headers, 75, "\n", true)) – SG1 Feb 19 '12 at 17:55
2

Order servers have an even lower limit: 76 chars per line + \r\n.

You have to make use of the imap_8bit() and imap_binary() functions in order to convert your data to a base64 or quoted-printable encoding.

You can also use an existing library, like SwiftMailer.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • I'm running this in a XMLRPC service and both imap_8bit() and imap_binary() crash. usage: return imap_8bit($html); instead of return $html; (same for imap_binary()). Am I using this right? and SwiftMailer is not an option right now as it would need to go through an approval process to be installed on a production server. – Phill Pafford Jan 19 '11 at 17:13
  • @Phill Pafford: You have to set the mime headers accordingly, I can't tell you if it's right without having a look at your code. Have you considered using SwiftMailer or any other email library, like PHPMailer? – Alix Axel Jan 19 '11 at 17:15
  • @Phill: Try adding this additional header: `Content-Transfer-Encoding: quoted-printable`. – Alix Axel Jan 19 '11 at 17:21
  • updated my header to add the code (see main question), still no luck – Phill Pafford Jan 19 '11 at 17:25
  • @Phill: Ok, I need more info then. What do you mean "crashed"? How are you sending the email - using the built-in `mail()` function? – Alix Axel Jan 19 '11 at 17:28
  • @Phill Pafford: Help me help you, bring some helpful context to the question. Where are you doing `return $html;`? And what did you mean by "crashed"? Any error output? – Alix Axel Jan 19 '11 at 17:43
  • ok I have added more to the main question, the XMLRPC doesn't offer much in the way of error codes and nothing shows up in the Apache error log as well. – Phill Pafford Jan 19 '11 at 17:54
  • @Phill Pafford: Ok, so the problem is how to escape chars within the XML, I'm not sure what the correct way to do that is. What happens if you `base64_encode()` your `$html`? Also, you may want to post a new question regarding the XML escaping since that has nothing to do with the SMTP email protocol. – Alix Axel Jan 19 '11 at 17:59
1

Actually, this is not a "mail server" problem. The SMTP line limit dictates the number of characters allowed on each line during transmission. The SMTP RFC allows for up to 1000 characters per line, and default postfix installed cap at 998 characters. You should contact your hosting provider on increasing your SMTP line limit if you feel it's necessary to exceed the RFC.