PHP giving a trailing “=” on each line after reading from stdin

风流意气都作罢 提交于 2020-01-15 03:45:31

问题


The contents of stdin is getting corrupted with word wrapping and trailing "=" throughout which obviously breaks the URL that I need to post.

I need to extract a URL/link from an email then post the URL. So, I'm piping my email to a php script in cpanel using a standard code snip I've seen all over the internet:

$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) { $email .= trim(fread($fd, 1024)); }
fclose($fd);

Then dumping the contents of the email to a file "pipemail.txt" for now to inspect it and make sure it's all working properly.

$fdw = fopen("pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);

The output is looking like this:

...
<table style=3D"width:100%" cellpadding=3D"0" cellspacing=3D"0" border=3D"0=
"><tbody><tr><td><table style=3D"background-color:#ffffff;color:#3c445a;fon=
t-family:arial;font-size:10px;font-weight:bold;width:100%" cellpadding=3D"0=
" cellspacing=3D"0">
...

I have been working on this for over a day now and I'm completely stumped. I've tried trimming the trailing "=" from incoming lines and it does not give me the expected result. Instead it seems to remove random "=" from seemingly random locations in the content. I am guessing that it is not random but it only seems so because it's not what I expect. It's probably only removing it if it happens to be the last character of the 1024 k/char line but if that is true then where else is the wordwrapping coming from? I don't know enough about how this works to trouble shoot this myself.

Why is it wrapping? Where are the "=" coming from? Does anyone have any suggestions?


回答1:


Emails are commonly encoded in the quoted printable format (http://en.wikipedia.org/wiki/Quoted-printable)

You can decode it using quoted_printable_decode() - this is done automatically by your email client, which is why it looks like php is adding those character.

http://www.php.net/manual/en/function.quoted-printable-decode.php




回答2:


from an email

and

trailing “=” on each line

means that the email is probably quoted-printable encoded. You need to parse the message properly and run the body part through quoted_printable_decode().

There are Content-Type headers that will tell you what kind of encoding is used, and probably MIME parts and boundaries that you'll need to deal with.




回答3:


Your data is in quoted-printable format, use quoted_printable_decode to decode it:

$email = quoted_printable_decode(file_get_contents("php:://stdin"));


来源:https://stackoverflow.com/questions/23415339/php-giving-a-trailing-on-each-line-after-reading-from-stdin

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