简体   繁体   中英

\r\n not working but \r\n\r\n works

I am sending an email via CodeIgniter's email->send(). I have come across an anomaly that I cannot figure out.

"\\r\\n" is not working in a certain section of the email. However if I switch "\\r\\n" to "\\r\\n\\r\\n" it works. By works I mean, it adds the 2 line breaks expected.

The problem area is at the bottom.

$order  = $this->ordersclass->getOrder( $order_id );
$quantity   = $order['no_codes'];
$client = $this->clientclass->getClient( $order['client_id'] );
$multi_d    = $this->session->userdata('multi-use-codes-d');
$multi_t    = $this->session->userdata('multi-use-codes-t');

$this->load->library('email');

$to         = $client['client_email'];
$subject    = 'Personal Resiliency Builder Order';
$from       = 'accounts@resiliencybuilder.com.au';

$message  = "TAX INVOICE\r\n\r\n";
$message .= "Client Name: ". $client['client_name']."\r\n";
$message .= "Invoice Number: ".$order['order_id']."\r\n";
$message .= "Invoice Date: ".$order['order_timestamp']."\r\n\r\n";

$message .= "TOTAL AMOUNT PAYABLE:     $".number_format($order['order_amount'],2)."\r\n";
$message .= "GST ON SERVICES:          $".number_format(($order['order_amount']/110)*10,2)."\r\n\r\n";

$message .= "ACCOUNT PAID IN FULL VIA CREDIT CARD\r\n\r\n";
$message .= "=============================================================\r\n";

$message .= "DESCRIPTION OF SERVICES\r\n\r\n";

$message .= "Code List Name: ".$this->session->userdata('codelistname') . "\r\n";
$message .= "Quantity: ".$quantity ."\r\n";
$message .= "Single-use Developmental Reports Purchased: ".$order['no_codes_d']."\r\n";
$message .= "Single-use Thriving Reports Purchased: ".$order['no_codes_t']."\r\n";

The last 2 $message variables are the problem.

The email looks like this:

TOTAL AMOUNT PAYABLE: $1,771.00

GST ON SERVICES: $161.00

ACCOUNT PAID IN FULL VIA CREDIT CARD

=============================================================

DESCRIPTION OF SERVICES

Code List Name: fggdgfdgfd

Quantity: 12

Single-use Developmental Reports Purchased: 7 Single-use Thriving Reports Purchased: 5

Multi-use Developmental Reports Purchased: 5 Multi-use Thriving Reports Purchased: 5

SOLVED. Now looking for the understanding behind it.

After many tests, the formula for failure is: Failure = X + Y:

Failure = X character length + spaces.

In addition, it seams this problem occurs with Microsoft outlook 20xx, but not with hotmail or gmail.

Example:

$message .= "Single-use: Developmental Reports : ddddd\\r\\n"; ////// fail - length 41
$message .= "Single-use: DevelopmentaldReportsd:dddddd\\r\\n"; /// fail - length 41
$message .= "Single-use:dDevelopmentaldReportsd:dddddd\\r\\n"; // pass - length 41
$message .= "Single-use:DevelopmentaldReportsd:dddddddddddddddddddddddddddddd\\r\\n"; // pass

NOTE: This answer is for developers using CodeIgniter version 2.1.3

This is not tested on any other PHP Framework.

Based on many tests via changing the character length, adding/removing spaces, and testing with different email services:

  • Outlook version 20xx
  • Hotmail
  • Gmail

It is safe to conclude that CodeIgniter version 2.1.3 parses the message in such a way that given a string length around 40 characters long + 1 space character + a newline character and sending the email to all three email serves above, that only Outlook will determine that the newline character is an "Extra" newline character and therefore remove it.

It is possible to change outlooks settings to disable Outlook from removing extra newline characters

As the link suggests: Microsoft Outlook Guide to disabling Extra Newline Removal

EASY DEVELOPER SOLUTION:

Keep your string length to a maximum 39 characters including spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM