Read email attachment - php

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.

Related

Contact form with unique file name

I want to create php file for my contact form but whenever they submit I want a different file name.txt.
This is the code. Whenever someone else submits I lose the old one.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST ['message'];
$file = fopen('contactform.txt', "w+");
$content = $name. PHP_EOL .$email. PHP_EOL .$message;
header('Location: http://localhost:8080/site-edits/contact.php');
fwrite($file , $content);
fclose($file);
?>
Like Barry said, you could append a timestamp to your name.
PHP also has a function tempnam available that generates a unique filename:
$file = fopen(tempnam(".", "contactform.txt"), "w+");
One thing you can do is add the current date to the file name:
$file = fopen('contactform-'.date('YmdHis').'.txt', "w");
Alternatively, you can append to the file instead of writing over it.
$file = fopen('contactform.txt', "a");
$content = $name. PHP_EOL .$email. PHP_EOL .$message .PHP_EOL.PHP_EOL;
Most people use a database (for example MySQL) for this. I'd suggest learning about how to use databases when you get the time.
You can either create new file with different name any time user submits the form or append text in existed file usin 'a' rather 'w+'
Like:
$file = fopen('contactform.txt', 'a');
For more go to php file handling

Printing or Echo form data to csv file

I have listened to a youtube tutorial severally in order to write a php code for online form data and print to CSV file on my website. The code the teacher used on the tutorial works perfectly on my local computer but doesn’t print anything on the CSV file as I have now uploaded the page on my website.
when site users fill the form online and click on submit button on my site – no information from form is printed on the same CSV file. Why?
This is the code I got from the tutorial and wrote the same:
<?php
if(isset($_POST['submit'])){
$names = $_POST['names'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$job = $_POST['job'];
$city = $_POST['city'];
$data = $names . "," . $telephone . "," . $email . "," . $job . "," . $city;
$file = "cardealer.csv";
file_put_contents ($file, $data . PHP_EOL, FILE_APPEND);
echo "Thank you for completing this form, we will reply soon";
}
?>
How can I fix this problem sir, I used the exact offline code you gave in the tutorial.
I appreciate your kind gestures here to help starters like us.
Thank you,
this to export excel file based on CSV data and download the file
if you want to write a file you should consider using chmod
like this
chmod($folder_path. $file_name. ".".$ext, '775');
also in this part
fopen("php://output",w);
you can change it to
fopen($path_to_file,w);
you can change the MIME-TYPE and the file extension to what ever you want.
/**
*the $array the data to be converted to CSV
*
*/
function array_to_csv_download($array, $filename = "export.xls", $delimiter=";") {
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename="'.$filename.'";');
header("Pragma: no-cache");
header("Expires: 0");
// open the "output" stream
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}

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!

PHP get specific keywords to variables from a piped mail

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);

Save email attachment from piped email

I am using pipe to program to send emails to a script. Using this script, I can sucessfully save the entire email as a .txt file on my server. The only thing left to do, is figure out how to save any attachment that the email comes in with. (This email address is only given to one trusted source, so security is not an issue)
Working code that saves entire email as .txt file:
$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("/home/lmshost22/public_html/pipemail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
Can anyone help me with code that simply extracts the attachment (will ALWAYS be a .csv file) and saves it on my server?
Here is the code that the .txt file shows for the attachment:
------=_NextPart_000_0133_01CE0E98.061E7400
Content-Type: application/vnd.ms-excel;
name="leads.csv"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="leads.csv"
ApplicationDetailId,DateCreated,VehicleInfoId,FirstName,MiddleName,LastNa=
me,Suffix,Street,City,State,ZipCode,Email,HomePhoneArea,HomePhonePrefix,H=
omePhoneSuffix,CellArea,CellPrefix,CellSuffix,WorkPhoneArea,WorkPhonePreF=
ix,WorkPhoneSuffix,AmountBorrow,IsVehiclePaidOff,IsVehicleSalvaged,OweAmo=
unt,TitleOwnership,IsInBankruptcy,IsInCreditCounseling,HearOfUs,VehicleIn=
foId,Year,Make,Model,Trim,Miles,Engine,DriveTrain,Transmission,Options,Bo=
ok,ClassCode,Door,FuelType,BodyStyle
4523,2/18/2013 2:56:33 PM,4524,James,,Pruitt,,7900 =
Carmelita,Atascadero,CA,93422,,702,=3D"353",=3D"9760",=3D"",=3D"",=3D"",=3D=
"",=3D"",=3D"",=3D"2500.0000",True,False,0.0000,No =
one,False,False,Google,4524,2001,=3D"Toyota",Tacoma =
Xtracab,PreRunner,200000,V6 3.4 =
Liter,2WD,Automatic,199443~199448~199471~199480~199508~4234190~,0.0000,1,=
0,Gas,Pickup
------=_NextPart_000_0133_01CE0E98.061E7400--
Attachments imply that the message is MIME/Multipart and has a boundary header set which will delimit the various message parts. eg:
Content-Type: multipart/mixed;
boundary="b1_bb1b331cd6dafa1dc6a19e3b2e090b07"
So you want to find that first via something like:
preg_match('/boundary="(.*)"/', $message, $matches);
The message is divided by -- concatenated with the boundary, followed by headers specific to that part, a blank line, and then the data. eg:
--b1_bb1b331cd6dafa1dc6a19e3b2e090b07
Content-Type: application/pdf; name="Invoice-37272.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Invoice-37272.pdf"
// base64 data here //
And the message is terminated with '--' . $boundary . '--', eg: --b1_bb1b331cd6dafa1dc6a19e3b2e090b07--
So you can use those two bits of info to break up your messages into their constituent parts and find/save the attachment.
here is my entire PHP file to extract a csv file(email with .csv attachment is piped to this script). The key is you have to decode the text that makes up the file, it is between the boundaries mentioned above.
<?
$home_path = '/home/USERNAME/public_html/DIRECTORY/';
$filename = "FILENAME.TXT"; // TO SAVE THE CONTENTS OF CSV FILE AS TEXT
$sock = fopen ("php://stdin", 'r');
$email = '';
while (!feof($sock)) {
$email .= fread($sock, 10240);
}
fclose($sock);
$encoding1 = explode ("------=_", $email);
$encoding2 = explode ("------=_", $encoding1[2]);
$encoding3 = $encoding2[0];
$encoding4 = explode (".csv", $encoding3);
$encoding5 = explode ("\n\n", $encoding4[2]);
$encoding = base64_decode($encoding5[1]);
$encoding = $encoding . "---" . strlen($encoding);
$fdw = fopen($home_path . $filename . ".txt", "w+");
fwrite($fdw, $encoding);
fclose($fdw);
?>

Categories