phpbar.de logo

Mailinglisten-Archive

AW: [php] Mit PHP Emails beantworten?

AW: [php] Mit PHP Emails beantworten?

Frank Rust frust at iti.cs.tu-bs.de
Die Mar 15 11:52:24 CET 2005


Michael Borchers schrieb:

>hallo frank,
>hast du schon eine lösung gefunden?
>ich arbeite z zt auch an einer art mailing liste,
>die nach einem ähnlichen prinzip funktioniert.
>
>bisher konnte ich auch einzelne mails auslesen
>und deren inhalt je nach subject zum ein-
>oder austragen verwenden,
>nur das säubern des headers bereitet mir noch
>schwierigkeiten!
>
>wie sieht's bei dir aus?
>
>
>MfG
>Michael Borchers
>borchers at tridem.de
>0491 96 06 71 63
>Tridem Internet Services GmbH
>Reimersstraße 41b
>26789 Leer
>
>
>  
>
>>-----Ursprüngliche Nachricht-----
>>Von: Frank Rust [mailto:frust at iti.cs.tu-bs.de]
>>Gesendet: Mittwoch, 9. März 2005 08:36
>>An: deutschsprachige PHP-Mailingliste
>>Betreff: [php] Mit PHP Emails beantworten?
>>
>>
>>
>>Gibt es ein Programmpaket mit dem ich mittels PHP Emails 
>>_empfangen_ und 
>>darauf irgendwie
>>reagieren kann? Ich habe ein Umfangreiches Programmsystem 
>>dass über die 
>>Website bedient
>>wird und (unter anderem) Emails aussendet. Die Leute sollen 
>>nun mittels 
>>Reply auf eine solche
>>Mail Aktionen im PHP System auslösen.
>>
>>Gruß Frank
>>
>>-- 
>>______________________________________________________________
>>__________
>>Frank Rust, Technische Universität, Institut für Theoretische 
>>Informatik
>>Tel.: +49 531 391 9525               Postfach 3329, D-38023 
>>Braunschweig
>>Fax.: +49 531 391 9529      Mühlenpfordtstr. 22-23, D-38106 
>>Braunschweig
>>
>>-- 
>>** Allgemeine deutschsprachige PHP-Liste: php at phpbar.de **
>>Informationen: http://www.phpbar.de
>>http://lists.phpbar.de/mailman/listinfo/php
>>
>>    
>>
>
>
>
>  
>
Hi,

habe mir inzwischen etwas programmiert. Sehr simpel, tut aber was es soll...

parseMail.php:

<?php
    // parse an incoming mail
    // Version 0.5, 2005/03/16
    // Copyright (c) Frank Rust, TU Braunschweig (f.rust at tu-bs.de)
    //
    // This code is free software; you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation; either version 2 of the License, or
    // (at your option) any later version.
    // 
    // This code is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    // 
    // Since this is a very short Program the GNU General Public License
    // is not included. Please find it on the website of the Open Software
    // Foundation at
    //     http://www.fsf.org/licensing/licenses/lgpl.txt
    // or write to the Free Software Foundation, Inc., 59 Temple Place, 
    // Suite 330, Boston, MA  02111-1307  USA
    
    class parseMail {
		var $from="";
		var $to="";
		var $subject="";
		var $received="";
		var $date="";
		var $message_id="";
		var $content_type="";
		var $part =array();
		
		// decode a mail header
		function parseMail($text="") {
			$start=0;
			$lastheader="";
			while (true) {
				$end=strpos($text,"\n",$start);
				$line=substr($text,$start,$end-$start);
				$start=$end+1;
				if ($line=="") break; // end of headers!
				if (substr($line,0,1)=="\t") {
					$$last.="\n".$line;
				}
				if (preg_match("/^(From:)\s*(.*)$/",$line,$matches)) {
					$last="from";
					$$last=$matches[2];
				}
				if (preg_match("/^(Received:)\s*(.*)$/",$line,$matches)) {
					$last="received";
					$$last=$matches[2];
				}
				if (preg_match("/^(To:)\s*(.*)$/",$line,$matches)) {
					$last="to";
					$$last=$matches[2];
				}
				if (preg_match("/^(Subject:)\s*(.*)$/",$line,$matches)) {
					$last="subject";
					$$last=$matches[2];
				}
				if (preg_match("/^(Date:)\s*(.*)$/",$line,$matches)) {
					$last="date";
					$$last=$matches[2];
				}
				if (preg_match("/^(Content-Type:)\s*(.*)$/",$line,$matches)) {
					$last="content_type";
					$$last=$matches[2];
				}
				if (preg_match("/^(Message-Id:)\s*(.*)$/",$line,$matches)) {
					$last="message_id";
					$$last=$matches[2];
				}
			}
			$this->from=$from;
			$this->received=$received;
			$this->to=$to;
			$this->subject=$subject;
			$this->date=$date;
			$this->content_type=$content_type;
			$this->message_id=$message_id;
			
			if (preg_match("/^multipart\/mixed;/",$content_type)) {
				$b=strpos($content_type,"boundary=");
				$boundary=substr($content_type,$b+strlen("boundary="));
				$boundary=substr($boundary,1,strlen($boundary)-2);
				$this->multipartSplit($boundary,substr($text,$start));
				
			} else {
				$this->part[0]['Content-Type']=$content_type;
				$this->part[0]['content']=substr($text,$start);
			}
		}
		// decode a multipart header 
		function multipartHeaders($partid,$mailbody) {
			$text=substr($mailbody,$this->part[$partid]['start'],
			             $this->part[$partid]['ende']-$this->part[$partid]['start']);

			$start=0;
			$lastheader="";
			while (true) {
				$end=strpos($text,"\n",$start);
				$line=substr($text,$start,$end-$start);
				$start=$end+1;
				if ($line=="") break; // end of headers!
				if (substr($line,0,1)=="\t") {
					$$last.="\n".$line;
				}
				if (preg_match("/^(Content-Type:)\s*(.*)$/",$line,$matches)) {
					$last="c_t";
					$$last=$matches[2];
				}
				if (preg_match("/^(Content-Transfer-Encoding:)\s*(.*)$/",$line,$matches)) {
					$last="c_t_e";
					$$last=$matches[2];
				}
				if (preg_match("/^(Content-Description:)\s*(.*)$/",$line,$matches)) {
					$last="c_desc";
					$$last=$matches[2];
				}
				if (preg_match("/^(Content-Disposition:)\s*(.*)$/",$line,$matches)) {
					$last="c_disp";
					$$last=$matches[2];
				}
			}
			if ($c_t_e=="base64") {
				$this->part[$partid]['content']=base64_decode(substr($text,$start));
				$c_t_e="8bit";
			} else {
				$this->part[$partid]['content']=substr($text,$start);	
			}
			$this->part[$partid]['Content-Type']=$c_t;
			$this->part[$partid]['Content-Transfer-Encoding']=$c_t_e;
			$this->part[$partid]['Content-Description']=$c_desc;
			$this->part[$partid]['Content-Disposition']=$c_disp;
			unset($this->part[$partid]['start']);
			unset($this->part[$partid]['ende']);
		}
		// we have a multipart message body
        // split the parts 
		function multipartSplit($boundary,$text) {
			$start=0;
			$b_len=strlen("--".$boundary);
			$partcount=0;
			while (true) { // should have an emergency exit...
				$end=strpos($text,"--".$boundary,$start);
				if (substr($text,$end+$b_len,1)=="\n") {
					// '\n' => part boundary
					$this->part[$partcount]['start']=$end+$b_len+1;
					if ($partcount) { 
						$this->part[$partcount-1]['ende']=$end-1;
						$this->multipartHeaders($partcount-1,$text);
					}
					$start=$end+$b_len+1;
					$partcount++;
				} else {
					// '--' => end boundary
					$this->part[$partcount-1]['ende']=$end-1;				
					$this->multipartHeaders($partcount-1,$text);
					break;
				}
			}	
		}
    } 
?>  

getEmail.php:
<?php
    // get an email from a post request and parse it using parseMail class
    // Version 0.1, 2005/03/16
    // Copyright (c) Frank Rust, TU Braunschweig (f.rust at tu-bs.de)
    //

    require("parseMail.php");
  
    // could be a bit more safety here:
    // get the contents of the uploaded email file    
    $mailtext=file_get_contents($_FILES['postfile']['tmp_name']);

    // parse that file
    $email=new parseMail($mailtext);

    // show the results (or do anything useful...)
    print_r($email);


?>

PostNewMail.php
<?php
    // Very simple example:
    // receive an email file on stdin and post that file to an url
    // (e.g. on a Unix-like server create an entry in /etc/aliases:
    // phpmail: |PostNewMail.php
    // PostNewMail.php should be executable and have the 
    // #!/path/to/console-php -q 
    // in the first line. Don't forget to allow
    // the execution of this program to smrsh)
    //
    // Version 0.1, 2005/03/13
    // Copyright (c) Frank Rust, TU Braunschweig (f.rust at tu-bs.de)
    //
    // This code is free software; you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation; either version 2 of the License, or
    // (at your option) any later version.
    // 
    // This code is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    // 
    // Since this is a very short Program the GNU General Public License
    // is not included. Please find it on the website of the Open Software
    // Foundation at
    //     http://www.fsf.org/licensing/licenses/lgpl.txt
    // or write to the Free Software Foundation, Inc., 59 Temple Place, 
    // Suite 330, Boston, MA  02111-1307  USA
    

	
	// create temporary file to store the mail
	$tmpnam=tempnam("/tmp","MAIL");
	$tmpfile=fopen($tmpnam,"w");

	// read mail from stdin
	$input=fopen("php://stdin","r");
    
	// read complete mail and write it to tmpfile
	while (!feof($input)) {
		$line = fgets($input,4096);
		fwrite($tmpfile,$line);
	}
	fclose($input); 
	fclose($tmpfile);

	// use cURL to post the file to the Websystem
	$postvars=array("postfile" => "@".$tmpnam );
    
	$URL="http://www.mywebhost.org/getEmail.php";
    
	$ch=curl_init($URL);
	$user_agent="Franks Email POSTer";
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_VERBOSE, 0); // 1 for debugging
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$x=curl_exec($ch);
	$info=curl_getinfo($ch);
	curl_close($ch);

	//for debugging the console program: 
	echo $x; print_r($info);

	// cleanup
	unlink($tmpnam);	
?>





-- 
________________________________________________________________________
Frank Rust, Technische Universität, Institut für Theoretische Informatik
Tel.: +49 531 391 9525               Postfach 3329, D-38023 Braunschweig
Fax.: +49 531 391 9529      Mühlenpfordtstr. 22-23, D-38106 Braunschweig


php::bar PHP Wiki   -   Listenarchive