我在 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
在安卓上:
我改变了(没用):
String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%0A");
致(工作):
String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%3Cbr%3E");
现在我可以在 EditText 中编写段落,Outlook 会正确显示它们。
干杯!
更新:(只是添加)
这些字符的解释方式(我后来了解到,也是分享此更新的原因)是因为发送邮件的 php 脚本中使用的 header :(设置 $header 时使用的字符text/html 内容类型(如下面的代码所示)将使用 HTML(不再是 PHP)转义字符)
$headers = "Content-type: text/html; charset=\"UTF-8\";
关于php - 将多文本从 Android 发送到 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388104/