How to have \n convert to newline for emailing PHP? [closed]

白昼怎懂夜的黑 提交于 2019-12-14 03:33:31

问题


My code as below

$ID = mysqli_real_escape_string($cnx, trim($data->id));
$MSG = mysqli_real_escape_string($cnx, trim($data->message));

$query = "REPLACE INTO mytbl ".
           "(id, msg, dateentry, status, rate) ".
           "VALUES ('$ID', '$MSG', NOW(), 'ok', '$RATE')";

$result =mysqli_query($cnx, $query) or die ("Can't execute query!");

$to = 'my@gmail.com';
$subject = 'my report';
$message = 'Message From User on:  '. $MSG  . "\r\n";
$headers = 'From: anonymous@mymail.com' . "\r\n" .
           'Reply-To: anonymous@mymail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

In my message ($MSG), it contains new line, which is represented by \n. So when the email is sent, the \n persist, instead of the making a new line, it stays as \n. I would like it to be a new line instead of \n displaying on the email.

I have read about How to replace \r & \n with <br/>?, and think perhaps I could use double-quote instead of single, and work with nl2br, but of no success. Perhaps my value comes from somewhere else (i.e. $data->message), so I don't know how to make it a single quote or double quote string. Any light to shed?

Thanks!

(Note the $MSG is both used for DB insertion and Emailing)


回答1:


Not knowing anything about how $MSG is constructed, you could try something like this:

$MSG = str_replace("\\n","\n",$MSG);

Before adding $MSG to your $message.

Double quotes should be used whenever you want \n to mean newline rather than \\n.




回答2:


Removing mysqli_real_escape_string works for me. – Elye

^ as said after my initial comments. ^

Taken from my commments to close the question:

Using mysqli_real_escape_string() is most likely the reason.

It is escaping characters, and newline charcters most likely.

I don't see why you're using that. You're sending mail, not querying a DB. Remove it.

mysqli_real_escape_string() has nothing to do with sending mail().

If the query is "not" coming from user input, you don't need to escape the data.

If you need to filter user input from a form which won't go as an INSERT/UPDATE in DB, use PHP's filters http://php.net/manual/en/filter.filters.php, not MySQL's.

You can also set a new variable for outgoing mail and using PHP's filters; that's what they are there for.




回答3:


According to mail() documentation the body message MUST have line breaks that include carriage return.

As your email does not seem to be multipart -- with HTML body --, that is, at least you don't specify that it is, you should be able to achieve what is needed with

preg_replace("/(?<=[^\r]|^)\n/", "\r\n", $MSG);

which will replace alone-standing \n with \r\n.

I would suggest, however, not using on mysqli_real_escape_string, as you are not using it to build a SQL statement.



来源:https://stackoverflow.com/questions/28252935/how-to-have-n-convert-to-newline-for-emailing-php

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