AtDev
asked on
php save email attachments to server
I'm trying to write a script to achieve the following:
1> Cron the script daily
2> Script opens email account
3> Pulls csv attachment from email
4> Renames attachment YYYYMMDD.csv
5> Saves attachment in same directory as script
6> Deletes email
I'm using email classes from http://cingusoft.mirrors.phpclasses.org/browse/file/13399.html with not much success as it's coming up with the ERROR "Zero Sized Reply".
Here is the basic script with all variables as default.
-- parse-email.php --
<?
require_once("attachmentre ad.class.p hp");
$host="{my.mailserver.net: 110/pop3}" ; // pop3host
$login="user1"; //pop3 login
$password="passwd1"; //pop3 password
$savedirpath="" ; // attachement will save in same directory where scripts run othrwise give abs path
$jk=new readattachment(); // Creating instance of class####
$jk->getdata($host,$login, $password, $savedirpa th); // calling member function
?>
-- attachmentread.class.php --
<?
########################## ########## ##
#Coded By Jijo Last Update Date[Jan/19/06]
########################## ########## #
########################## ########## ########## ########## ##
###################### Class readattachment ###############
class readattachment
{
function getdecodevalue($message,$c oding)
{
if ($coding == 0)
{
$message = imap_8bit($message);
}
elseif ($coding == 1)
{
$message = imap_8bit($message);
}
elseif ($coding == 2)
{
$message = imap_binary($message);
}
elseif ($coding == 3)
{
$message=imap_base64($mess age);
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = imap_base64($message);
}
return $message;
}
function getdata($host,$login,$pass word,$save dirpath)
{
$mbox = imap_open ($host, $login, $password) or die("can't connect: " . imap_last_error());
$message = array();
$message["attachment"]["ty pe"][0] = "text";
$message["attachment"]["ty pe"][1] = "multipart";
$message["attachment"]["ty pe"][2] = "message";
$message["attachment"]["ty pe"][3] = "application";
$message["attachment"]["ty pe"][4] = "audio";
$message["attachment"]["ty pe"][5] = "image";
$message["attachment"]["ty pe"][6] = "video";
$message["attachment"]["ty pe"][7] = "other";
for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++)
{
$structure = imap_fetchstructure($mbox, $jk , FT_UID);
$parts = $structure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++)
{
$message["pid"][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT")
{
$message["type"][$i] = $message["attachment"]["ty pe"][$part ->type] . "/" . strtolower($part->subtype) ;
$message["subtype"][$i] = strtolower($part->subtype) ;
$ext=$part->subtype;
$params = $part->dparameters;
$filename=$part->dparamete rs[0]->val ue;
$mege="";
$data="";
$mege = imap_fetchbody($mbox,$jk,$ fpos);
$filename="$filename";
$fp=fopen($filename,w);
$data=$this->getdecodevalu e($mege,$p art->type) ;
fputs($fp,$data);
fclose($fp);
$fpos+=1;
}
}
//imap_delete tags a message for deletion
//imap_delete($mbox,$jk);
}
// imap_expunge deletes all tagged messages
//imap_expunge($mbox);
imap_close($mbox);
}
}
?>
1> Cron the script daily
2> Script opens email account
3> Pulls csv attachment from email
4> Renames attachment YYYYMMDD.csv
5> Saves attachment in same directory as script
6> Deletes email
I'm using email classes from http://cingusoft.mirrors.phpclasses.org/browse/file/13399.html with not much success as it's coming up with the ERROR "Zero Sized Reply".
Here is the basic script with all variables as default.
-- parse-email.php --
<?
require_once("attachmentre
$host="{my.mailserver.net:
$login="user1"; //pop3 login
$password="passwd1"; //pop3 password
$savedirpath="" ; // attachement will save in same directory where scripts run othrwise give abs path
$jk=new readattachment(); // Creating instance of class####
$jk->getdata($host,$login,
?>
-- attachmentread.class.php --
<?
##########################
#Coded By Jijo Last Update Date[Jan/19/06]
##########################
##########################
###################### Class readattachment ###############
class readattachment
{
function getdecodevalue($message,$c
{
if ($coding == 0)
{
$message = imap_8bit($message);
}
elseif ($coding == 1)
{
$message = imap_8bit($message);
}
elseif ($coding == 2)
{
$message = imap_binary($message);
}
elseif ($coding == 3)
{
$message=imap_base64($mess
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = imap_base64($message);
}
return $message;
}
function getdata($host,$login,$pass
{
$mbox = imap_open ($host, $login, $password) or die("can't connect: " . imap_last_error());
$message = array();
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
$message["attachment"]["ty
for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++)
{
$structure = imap_fetchstructure($mbox,
$parts = $structure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++)
{
$message["pid"][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT")
{
$message["type"][$i] = $message["attachment"]["ty
$message["subtype"][$i] = strtolower($part->subtype)
$ext=$part->subtype;
$params = $part->dparameters;
$filename=$part->dparamete
$mege="";
$data="";
$mege = imap_fetchbody($mbox,$jk,$
$filename="$filename";
$fp=fopen($filename,w);
$data=$this->getdecodevalu
fputs($fp,$data);
fclose($fp);
$fpos+=1;
}
}
//imap_delete tags a message for deletion
//imap_delete($mbox,$jk);
}
// imap_expunge deletes all tagged messages
//imap_expunge($mbox);
imap_close($mbox);
}
}
?>
ASKER
Thanks :-D
The error I get is "Zero Sized Reply" when running parse-email.php
The error I get is "Zero Sized Reply" when running parse-email.php
I don't see any html or print or echo statements. Is "Zero Sized Reply" an error? All it means is that the browser didn't get anything back in the response when it called this script. Looking at it, unless there was an error thrown and error_reporting was turned on, I'm not seeing why it would.
Does the attachment not save then?
Does the attachment not save then?
ASKER
yes "Zero Sized Reply" that the browser shows.
I have error_reporting on but there is no php errors outputed and the attachment does not save to the same folder either (permissions set to 777)
I have error_reporting on but there is no php errors outputed and the attachment does not save to the same folder either (permissions set to 777)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hmmm, if you run this script for yourself (on your server) does it work without error cause it doesn't work for me.
$filename comes fro the $params value so that part appears fine:
$params = $part->dparameters;
$filename=$part->dparamete rs[0]->val ue;
$filename comes fro the $params value so that part appears fine:
$params = $part->dparameters;
$filename=$part->dparamete
me either
Anyway, I looked over your code briefly, and I'm not clear on where the problem is. It would help if you could tell us what error you get when this script runs.