This topic is locked

Getting email into the database

11/4/2016 6:25:12 AM
PHPRunner General questions
mbintex author

Hi at all,
I am kind of fighting with getting emails into my database.
I have the following button script so far.



global $dal;

global $link,$msgid,$partsarray;
$msgid=1;

$partsarray="";
function parsepart($p,$i){
$filestore = '[files/(chmod777)]';
//fetch part

$part=imap_fetchbody($link,$msgid,$i);

//if type is not text

if ($p->type!=0){

//DECODE PARTÂ Â Â Â

//decode if base64

if ($p->encoding==3)$part=base64_decode($part);

//decode if quoted printable

if ($p->encoding==4)$part=quoted_printable_decode($part);

//no need to decode binary or 8bit!
//get filename of attachment if present

$filename='';

// if there are any dparameters present in this part

if (count($p->dparameters)>0){

foreach ($p->dparameters as $dparam){

if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;

}

}

//if no filename found

if ($filename==''){

// if there are any parameters present in this part

if (count($p->parameters)>0){

foreach ($p->parameters as $param){

if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;

}

}

}

//write to disk and set partsarray variable

if ($filename!=''){

$partsarray[$i][attachment] = array('filename'=>$filename,'binary'=>$part);

$fp=fopen($filestore.$filename,"w+");

fwrite($fp,$part);

fclose($fp);

}

//end if type!=0Â Â Â Â

}
//if part is text

else if($p->type==0){

//decode text

//if QUOTED-PRINTABLE

if ($p->encoding==4) $part=quoted_printable_decode($part);

//if base 64

if ($p->encoding==3) $part=base64_decode($part);
//OPTIONAL PROCESSING e.g. nl2br for plain text

//if plain text
if (strtoupper($p->subtype)=='PLAIN')1;

//if HTML

else if (strtoupper($p->subtype)=='HTML')1;

$partsarray[$i][text] = array('type'=>$p->subtype,'string'=>$part);

}
//if subparts... recurse into function and parse them too!

if (count($p->parts)>0){

foreach ($p->parts as $pno=>$parr){parsepart($parr,($i.'.'.($pno+1)));

}

}

return;

}
/* connect to mail */

$hostname = '{mailserver.com:143/imap/tls}INBOX';

$username = 'address@mailserver.com';

$password = 'your mail password';
/* try to connect */

$link = imap_open($hostname,$username,$password) or die('Fehler: ' . imap_last_error());
/* grab emails */

$emails = imap_search($link,'UNSEEN');
/* if emails are returned, cycle through each... */

if($emails) {



/* begin output var */

$output = '';



/* put the newest emails on top */

rsort($emails);



/* for every email... */

foreach($emails as $msgid) {



/* get information specific to this email */

$overview = imap_fetch_overview($link,$msgid,0);

/* output the email header information */

$lesestatus=($overview[0]->seen ? 'read' : 'unread');
//Betreffzeile dekodieren

$subject=$overview[0]->subject;

$elements = imap_mime_header_decode($subject);

for ($i=0; $i<count($elements); $i++) {

$charset=$elements[$i]->charset;

$betreff=$elements[$i]->text;

}
//Absender dekodieren

$absender=$overview[0]->from;

$elements = imap_mime_header_decode($absender);

for ($i=0; $i<count($elements); $i++) {

$charset=$elements[$i]->charset;

$absender=$elements[$i]->text;

}
//Datum dekodieren

$datum=$overview[0]->date;

$datum=date("d.m.Y",strtotime($datum));
//Nachricht

// $message = imap_fetchbody($inbox,$email_number,0);
$s=imap_fetchstructure($link,$msgid);
//see if there are any parts

if (count($s->parts)>0){

foreach ($s->parts as $partno=>$partarr){

//parse parts of email

parsepart($partarr,$partno+1);

$parttext=imap_fetchbody ( $link , $msgid , ($partno+1) );
//OPTIONAL PROCESSING

if ($s->encoding==4) $parttext=quoted_printable_decode($parttext)."\n\n";

if ($s->encoding==3) {

$parttext = preg_replace('~[^a-zA-Z0-9+=/]+~s', '', $parttext);

$parttext=imap_base64($parttext)."\n\n";

}

if ($s->encoding==2) $parttext=imap_binary($parttext)."\n\n";

if ($s->encoding==1) $parttext=imap_utf8($parttext)."\n\n";

if (strtoupper($s->subtype)=='PLAIN') $parttext=$parttext."\n\n";

if (strtoupper($s->subtype)=='HTML') $parttext=$parttext."\n\n";
$text.=$parttext;

}

}
//for not multipart messages

else{

//get body of message

$text=imap_body($link,$msgid);

parsepart($partarr,1);

//decode

if ($s->encoding==4) $text=imap_qprint($text);

if ($s->encoding==3) $text=imap_base64($text);

if ($s->encoding==2) $text=imap_binary($text);

if ($s->encoding==1) $text=imap_utf8($text);
//OPTIONAL PROCESSING

if (strtoupper($s->subtype)=='PLAIN') $text=$text;

if (strtoupper($s->subtype)=='HTML') $text=$text;
}
$message=$text;
//In die Datenbank übertragen

$tblEvents=$dal->Table("Korrespondenz");

$tblEvents->Value["Postkorb"]="Posteingang";

$tblEvents->Value["Art"]="Mail";

$tblEvents->Value["Datum"]=Now();

$tblEvents->Value["Betreff"]=$betreff;

$tblEvents->Value["Absendedatum"]=$datum;

$tblEvents->Value["Absendermail"]=$absender;

$tblEvents->Value["Text"]=$message;

$tblEvents->Add();

$message="";

$text="";

}


}
/* close the connection */

imap_close($link);


With this I can get new emails whenever I decide to get them. I also get the fields filled correctly with sender, date, subject and mail content.
Problems are:

  • base64 encoded mails don´t get decoded - althoug I try to do so in the code
  • HTML mails don´t get formatted properly in most cases - shown as HTML and "custom"
  • pure quoted printable text mails loose all of their line breaks, which makes them kind of unreadable
    Every help or input to get rid of any of these problems would be very much appreciated. If any error I made is shocking - I am a newbie to PHP :-) So be nice to me please.
    Regards
    Martin

Sergey Kornilov admin 11/4/2016

I'm not really sure what kind of feedback you expect here but you can check EmailReader template:

http://xlinesoft.com/emailreader
You can send a few emails to test address to see if they are parsed as expected.

mbintex author 11/4/2016



I'm not really sure what kind of feedback you expect here but you can check EmailReader template:

http://xlinesoft.com/emailreader
You can send a few emails to test address to see if they are parsed as expected.


Hoped for a little help since I feel that I am very close to the solution, or not ?

mbintex author 11/4/2016

Sent two testmails to test@xlinesoft.com more than 10 min ago - nothing shows up there.
In fact the last mail the demo shows is from June this year - is it still working?

Sergey Kornilov admin 11/4/2016

Check again now

mbintex author 11/4/2016



Check again now


one of two made it now and looks good

Sergey Kornilov admin 11/4/2016

You can try to send another test to see if it goes through this time.
It's hard for anyone to tell what might be wrong with this bunch of code they never saw. Email parsing is a complicated topic, in our EmailReader template we use a PHP library specifically designed to handle all those situations.