Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing paremeters from PHP Email piping
    primarykey
    data
    text
    <p>I have a PHP Script that received information from a piped email forwarder.</p> <p>Currently everything works perfectly. I just want to manipulate the received parameters to receive only the data I want from the email</p> <p>the script is:</p> <pre><code>#!/usr/local/bin/php -q &lt;?PHP //get email $fd = fopen("php://stdin", "r"); $email_content = ""; while (!feof($fd)) { $email_content .= fread($fd, 1024); } fclose($fd); //get email end //get variables from email start //split the string into array of strings, each of the string represents a single line, recieved $lines = explode("\n", $email_content); // initialize variable which will assigned later on $from = ""; $subject = ""; $headers = ""; $message = ""; $is_header= true; //loop through each line for ($i=0; $i &lt; count($lines); $i++) { if ($is_header) { // hear information. instead of main message body, all other information are here. $headers .= $lines[$i]."\n"; // Split out the subject portion if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { $subject = $matches[1]; } //Split out the sender information portion if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { $from = $matches[1]; } } else { // content/main message body information $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $is_header = false; } } </code></pre> <p>Currently, the variable $email returns:</p> <pre><code>"Person name" &lt;person@domain.net&gt; </code></pre> <p>How can I modify this variable to only get the email portion?</p> <p>Secondly, the variable message returns a whole pile of data including formatting and tags as below:</p> <pre><code>This is a multipart message in MIME format. ------=_NextPart_000_0031_01CD9CA8.9BE5F470 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit testing ------=_NextPart_000_0031_01CD9CA8.9BE5F470 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable &lt;html xmlns:v=3D"urn:schemas-microsoft-com:vml" = xmlns:o=3D"urn:schemas-microsoft-com:office:office" = xmlns:w=3D"urn:schemas-microsoft-com:office:word" = xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" = xmlns=3D"http://www.w3.org/TR/REC-html40"&gt;&lt;head&gt;&lt;META = HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; = charset=3Dus-ascii"&gt;&lt;meta name=3DGenerator content=3D"Microsoft Word 14 = (filtered medium)"&gt;&lt;style&gt;&lt;!-- /* Font Definitions */ @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri","sans-serif";} a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri","sans-serif"; color:windowtext;} .MsoChpDefault {mso-style-type:export-only; font-family:"Calibri","sans-serif";} @page WordSection1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt;} div.WordSection1 {page:WordSection1;} --&gt;&lt;/style&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt; &lt;o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" /&gt; &lt;/xml&gt;&lt;![endif]--&gt;&lt;!--[if gte mso 9]&gt;&lt;xml&gt; &lt;o:shapelayout v:ext=3D"edit"&gt; &lt;o:idmap v:ext=3D"edit" data=3D"1" /&gt; &lt;/o:shapelayout&gt;&lt;/xml&gt;&lt;![endif]--&gt;&lt;/head&gt;&lt;body lang=3DEN-US link=3Dblue = vlink=3Dpurple&gt;&lt;div class=3DWordSection1&gt;&lt;p class=3DMsoNormal&gt;Ninety = nine&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt; ------=_NextPart_000_0031_01CD9CA8.9BE5F470-- </code></pre> <p>. I want to extract only the content of my body in plain text. so if body of email was simply 'testing' then I want the variable to return testing?</p> <p>Help appreciated as always.</p> <p>Thanks again.</p> <hr> <p>edit as per @hakra</p> <pre><code>#!/usr/local/bin/php -q &lt;?php // read from stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd); // handle email $lines = explode("\n", $email); // empty vars $from = ""; $subject = ""; $headers = ""; $message = ""; $splittingheaders = true; for ($i=0; $i &lt; count($lines); $i++) { if ($splittingheaders) { // this is a header $headers .= $lines[$i]."\n"; // look out for special headers if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { $subject = $matches[1]; } if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { $from = $matches[1]; } } else { // not a header, but message $message .= $lines[$i]."\n"; } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } $buffer = [$email] // Mail Content from Pipe $mail = mailparse_msg_create(); mailparse_msg_parse($mail, $buffer); $struct = mailparse_msg_get_structure($mail); foreach ($struct as $st) { $section = mailparse_msg_get_part($mail, $st); $info = mailparse_msg_get_part_data($section); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload