PHP get specific keywords to variables from a piped mail - php

I am using this piece of code to pipe emails:
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
}
fclose($fd);
In emails I need 4 lines starting with
---BEGIN---
data here
more data
ending with
---END---
I tried with explode and strstr but I am getting everything after the --BEGIN---
It doesn't stop at ---END--- somehow.
How I can "say" to it that I only need what is between ---BEGIN--- and ---END--- ?

preg_replace('/^.*---BEGIN---(.+?)---END---.*$/s', '$1', $email);

Related

pipe to program email script not working

i have inserted following link to pipe the emails.
link: public_html/emailer.php
i have script file in public_html folder, the following script i am using:
it should create a file in the directory, but its not working.
#!/public_html/ -q
<?php
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("pipemail.txt", "w");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
?>
this is how i am setting pipe to program path
This will work.
#!/usr/local/bin/php -q
<?php
$fd = fopen("php://stdin","r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd,1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("pipemail.txt", "w");
fwrite($fdw, $email);
fclose($fdw);
?>
Make sure it has unix line [\n] ending while you put the code in your server.
Also make sure the script has permission to execute!

Fwrite solution rewrite PHP

So.. i have problems with this code :
$filename = "info.txt";
$tex = $_POST['Name'];
$text = $tex . $_POST['Surname'];
$fp = fopen ($filename, "w");
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
It saves ONLY 1 name & surname. after another person submits his info, previous information is lost.
Is it possible to save all information?
You have to change the mode for fopen :
change 'w' to 'a' (a for append)
You'll find more information about each mode right here:
http://us2.php.net/manual/en/function.fopen.php#refsect1-function.fopen-parameters

Read email attachment

I have emails piped to a PHP script, which looks like this:
#!/usr/local/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
}
fclose($fd);
mail('test#test.com','From TEST','"' . $email . '"');
?>
This essentially sends me the entirety of the email.
I can't seem to find anything that doesn't rely on existing frameworks to read attachments.
Can anyone point me in the right direction for getting just the attachment data? The attachment will always be a CSV file.

How can I add multiple strings to one text file, and read the all?

I am trying to make a program where I can add things to a list, read things, and clear the list. I have the clear function working perfectly, however I can't seem to add or read more than 1 line at a time. I am using fwrite($handle, $MyString); but that replaces everything in the entire file with $MyString. To get the information from the file I am using $list = fgets($handle); and then using echo to print it. This reads the first line in the file only.
Any help?
Thanks!
Getlist code:
<?php
$myFile = "needlist.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>
Add to the list code:
<?php
$neededlist = "needlist.txt";
$fh = fopen($neededlist, 'w');
$user_message = $_REQUEST['txtweb-message'];
$needed .= $user_message;
$needed .= "\n";
fwrite($fh, $needed);
fclose($fh);
echo "You have successfully added ", $user_message;
?>
When you write to the file are you opening your filehandle with the "a" mode option? Opening with "w" or "x" truncates it so you start with a clean file (http://php.net/fopen)
fgets(); reads only until the end of the line ( http://php.net/fgets ). To get the whole file you can try:
var $list = "";
var $line = "";
while ($line = fgets($handle)) {
$list = $list . "\n" . $line;
}
echo $list;
You want to add the "\n" because fread doesn't read the linefeeds IIRC. There're also a couple functions that might be more appropriate in this situation like file_get_contents and fread.
Fgets returns only one string. You should use it in cycle like that:
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}

PHP Write and Read from Text File

I have an issue with writing and reading to text file.
I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:
Write to text file:
$fp = #fopen ("text1.txt", "r");
$fh = #fopen("text2.txt", 'a+');
if ($fp) {
//for each line in file
while(!feof($fp)) {
//push lines into array
$thisline = fgets($fp);
$thisline1 = trim($thisline);
$stringData = $thisline1. "\r\n";
fwrite($fh, $stringData);
fwrite($fh, "test");
}
}
fclose($fp);
fclose($fh);
Read from the written textfile
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
echo rtrim($kw[$i]);
}
But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.
How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.
I'm not sure whether you have issues with the even line numbers or with reading the file back in.
Here is the solution for the even line numbers.
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$myValue = rtrim($kw[$i]);
if(i % 2 == 0)
{
echo $myValue;
}
}

Categories