我在Android中创建了一个“联系我们表单”,该表单使用PHP Api向我发送电子邮件。例如,在EditText消息中,键入:
This is line 1
This is line 2
当我收到电子邮件时,显示以下内容:
This is line 1\nThis is line 2\n
发送电子邮件的代码:
String myURL = myDomain+myPHPDir+"sendMessageFromContactUs.php?pMyUsername="+myUsername
+"&pName="+strName
+"&pEmail="+strEmail
+"&pMessage="+strMessage
;
String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n", "%0A");
task.execute(new String[] {noSpace});
最佳答案
我通过以下方式解决了它。
在PHP上:
首先"\n" or "\r\n"
不适用于我,它打印在同一行上。
其次,"<br>"
起作用,它将字符打印在新行上。
然后,我从下面的源中阅读了“ URL转义符的完整指南”:
https://www.werockyourweb.com/url-escape-characters/
所以:
for <br> this < is %3C AND this > is %3E THEREFORE <br> becomes %3Cbr%3E
在ANDROID上:
我从(无效)更改为:
String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%0A");
到(工作):
String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%3Cbr%3E");
现在,我可以在EditText中编写段落,Outlook可以正确显示它们。
干杯!
更新:(只需添加)
解释这些字符的方式(稍后我会学习以及共享此更新的原因)是由于发送邮件的php脚本中使用了标头:(设置带有文本的$ header / html内容类型(如下面的代码所示)将使用HTML(不再是PHP)转义字符)
$headers = "Content-type: text/html; charset=\"UTF-8\";