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

依然范特西╮ 提交于 2019-12-11 06:29:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/18757398/r-n-not-working-but-r-n-r-n-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!