Scripts

Email-->Database script

Dit script plaatst emails die gestuurd worden vanaf een email adress in een database. De script is geschikt om een bijlage (plaatje) in een map te zetten + link in de inhoud te plaatsen. Werkt nu alleen voor jpg. Maar eventueel later voor extra extensies... Je moet wel over een email server beschikken en imap moet 'enabled' zijn. Aangepast: Geschikt voor multiple atachments + meerdere extensies http://phphulp.phpclub.nl/237/highlight.php?file=imap_to_db.php Voor werkende / voledige versie ^^

emaildatabase-script
<?
//show all errors, Very usefull while scripting. It shows al errors and warnings
error_reporting(E_ALL);
//include config.php for settings
//If you doen't want to use it. please leave it blank.
$mail_user_id = 'emailadress';//used for the script not your private mail or your ptivate mail will be inserted!
//email password 'password';
$mail_password = '';
//database host
$host = 'localhost';//often localhost
//database name
$database_name = 'database';
//database username
$database_username = 'username';
//database password
$database_password = 'password';
//dateformat in database follow iso standaard
$iso = 'Y-m-d H:i:s';//php 5 replace it for 'c' or keep it 
mysql_select_database($databse_name,mysql_connect($host,$databse_username,$database_password));
//conect to mailserver
//dit nog even aanpassen. Het werkt alleen als de mailserver op local host staat :/ en dat is niet iedeaal.
if($mbox = imap_open("{localhost:143}INBOX",$mail_user_id,$mail_password))
{
//check mail
$mtnum = imap_num_msg($mbox);
  if($mtnum > 0){
//get trough all messages with a for loop.
for($mnum = 1;$mnum <= $mtnum; $mnum++){
        //fetch content due there is a change that this page has a multi header. (as text and image) imap_body($mbox,$mnum); doesn't work.
        $mcontent = imap_fetchbody($mbox,$mnum,1);
	//fetch headers
	$mheader = imap_headerinfo($mbox,$mnum);
        //replace the emil in header info
        $mheader->fromaddress = preg_replace("/<(.*?)\>/si", "", $mheader->fromaddress);
        //recive the structure of the email
        $mstructure = imap_fetchstructure($mbox,$mnum);
        //check this email has a mixed subtype.
        if($mstructure->subtype == 'MIXED')
        {
        //loop trough all parts of the email
        for($mpart = 1;!empty($mstructure->parts[$mpart]->dparameters[0]->value);$mpart++){
        //revice any data from the attacment
        $mdata = imap_fetchbody($mbox,$mnum,$mpart + 1);
        //decode the 64 coded string to a string who could understood by PHP for creating the image form a string
        $mdecode = base64_decode($mdata);
        //create image from string.
        $mimage = imagecreatefromstring($mdecode);
        $mfilename = $mstructure->parts[$mpart]->dparameters[0]->value;
        //recive the size of the image.
        //getimagesize($mimage); doesn't work.
        $mwidth = imagesx($mimage);
        $mheigth = imagesy($mimage);
        //check max size;
        //resize the image.
        if($photow < $mwidth){
        $mprop = $photow / $mwidth;
        $mnewwidth = $photow;
        $mnewheight = $mheigth * $mprop;
        $mnewheight = round($mnewheight);
        $mnewimage = imagecreatetruecolor($mnewwidth, $mnewheight);
        imagecopyresampled($mnewimage, $mimage, 0, 0, 0, 0, $mnewwidth, $mnewheight, $mwidth, $mheigth);
        //check if the image name allready exist if true delete the older one
        if(file_exists('images/upload/'.$mfilename)){
        unlink('images/upload/'.$mfilename);
        }
        //only gif, jpg,jpeg and png are suported corectly.
        if(($mstructure->parts[$mpart]->subtype == 'jpg') or ($mstructure->parts[$mpart]->subtype == 'jpeg')){
        //create a jpeg file.
        imagejpeg($mnewimage,'images/upload/'.$mfilename);

        }else{
        //create a png/gif file. In GD 1.6 or higer imagegif isn't supproted
        imagepng($mnewimage,'images/upload/'.$mfilename);
        }
        }else{
        if(file_exists('images/upload/'.$mfilename)){
        unlink('images/upload/'.$mfilename);
        }
        //only gif, jpg,jpeg and png are suported
        if(($mstructure->parts[$mpart]->subtype == 'jpg') or ($mstructure->parts[$mpart]->subtype == 'jpeg')){
        imagejpeg($mimage,'images/upload/'.$mfilename);
        }else{
        imagepng($mimage,'images/upload/'.$mfilename);
        }
        }
//ubbcode: Replace * for a [ (Ubb phrasesr suxs here ^^)
        $mcontent.='*img]'.$domain.'images/upload/'.$mfilename.'[/img]\n';
        imagedestroy($mimage);
        }
        }
//insert in database
//make your own database query
$sql = "INSERT INTO `".$prefix."weblog` (`subject`,`date`,`content`,`author`) VALUES ('".$mheader->subject."','".date($iso, strtotime($mheader->date))."','".onpost($mcontent)."','".$mheader->fromaddress."')";
mysql_query($sql);
//send message to trashbin
imap_delete($mbox,$mnum);
}
}
//Delete al messages form the "thrashbin"
imap_expunge($mbox);
//disconect form server
imap_close($mbox);
}
?>

Reacties

0
Nog geen reacties.