vom Server eine Mail verschicken

Seven Secrets

Erfahrenes Mitglied
So, bevor jetzt allgemeines gewetter losgeht, daß haben wir schon so oft behandelt, als erstes eine Frage, die für mich nicht eindeutig geklärt ist:

Die Funktion mail(), kann sie Anhänge verschicken, ja oder nein?

Ansonsten meine Frage, gibt es etwas anderes auser php-Mailer, das diese Funktion hat?

Es grüßt euch, da Maddin
 
Grad vor ein paar Tagen hab ich 2 Klassen geschrieben die fuer Dich interessant sein koennten. Hatte nur noch keine Zeit Tutorials draus zu machen.

email.class.php
PHP:
<?php
class email
{
	private $from;
	private $to;
	private $cc;
	private $bcc;
	private $subject;
	private $body;
	private $useragent;
	private $attachedfiles;

	public function __construct($from,$to,$subject,$body,$cc="",$bcc="",$useragent="PHP/ReptilerEMailClass")
	{
		$this->from=$from;
		$this->to=$to;
		$this->subject=$subject;
		$this->body=$body;
		$this->cc=$cc;
		$this->bcc=$bcc;
		$this->useragent=$useragent;
		$this->attachedfiles=array();
	}

	public function addattachment($filename)
	{
		$this->attachedfiles[]=$filename;
	}

	public function composemail()
	{
		if (count($this->attachedfiles)>0)
			{
				$boundary='----'.md5(uniqid());
			}
		$mail='To: '.$this->to."\r\n";
		$mail.='Subject: '.$this->subject."\r\n";
		$mail.="Content-class: urn:content-classes: message\r\n";
		$mail.='User-Agent: '.$this->useragent."\r\n";
		$mail.="MIME-Version: 1.0\r\n";
		if (count($this->attachedfiles)>0)
			{
				$mail.="Content-Type: multipart/mixed;\r\n\tboundary=\"".$boundary."\"\r\n";
			}
		$mail.='From: '.$this->from."\r\n";
		if (!empty($this->cc))
			{
				$mail.='Cc: '.$this->cc."\r\n";
			}
		if (!empty($this->bcc))
			{
				$mail.='Bcc: '.$this->bcc."\r\n";
			}
		$mail.="X-Priority: 3 (Normal)\r\n";
		$mail.="Importance: Normal\r\n";
		if (count($this->attachedfiles)>0)
			{
				$mail.="\r\n--".$boundary."\r\n";
			}
		$mail.="Content-Type: text/plain;\r\n\tcharset=\"iso-8859-1\"\r\n";
		$mail.="Content-Transfer-Encoding: quoted-printable\r\n";
		$mail.="Content-Disposition: inline\r\n\r\n";
		$mail.=imap_8bit($this->body)."\r\n\r\n";
		if (count($this->attachedfiles)>0)
			{
				for ($x=0;$x<count($this->attachedfiles);$x++)
					{
						$mail.="\r\n\r\n--".$boundary."\r\n";
						$filename=explode('/',$this->attachedfiles[$x]);
						$filename=$filename[count($filename)-1];
						$file=fopen($this->attachedfiles[$x],"r");
						$content=fread($file,filesize($this->attachedfiles[$x]));
						fclose($file);
						$encodedfile=chunk_split(base64_encode($content));
						$mail.="Content-Type: application/octet-stream;\r\n\tname=\"".$filename."\"\r\n";
						$mail.="Content-Transfer-Encoding: base64\r\n";
						$mail.='Content-Description: '.$filename."\r\n";
						$mail.="Content-Disposition: attachment;\r\n\tfilename=\"".$filename."\"\r\n\r\n";
						$mail.=$encodedfile;
					}
				$mail.="\r\n\r\n--".$boundary."--";
			}
		return $mail;
	}
}
?>
smtpconnection.class.php
PHP:
<?php
class smtpconnection
{
	private $host;
	private $authentication;
	private $username;
	private $password;
	private $ssl;

	public function __construct($host,$authentication=false,$username="",$password="",$ssl=false)
	{
		$this->host=$host;
		$this->authentication=$authentication;
		$this->username=$username;
		$this->password=$password;
		$this->ssl=$ssl;
	}

	public function sendmail($mail)
	{
		if (isset($_SERVER['SERVER_NAME']))
			{
				$hostname=$_SERVER['SERVER_NAME'];
			}
		elseif (isset($_SERVER['HOSTNAME']))
			{
				$hostname=$_SERVER['HOSTNAME'];
			}
		else
			{
				$hostname="PHP/ReptilerSMPTClass";
			}
		$startpos=strpos($mail,"From: ")+6;
		$endpos=strpos($mail,"\r\n",$startpos);
		$from=substr($mail,$startpos,$endpos-$startpos);
		$startpos=strpos($mail,"To: ")+4;
		$endpos=strpos($mail,"\r\n",$startpos);
		$to=substr($mail,$startpos,$endpos-$startpos);
		$size=strlen($mail);
		if ($this->ssl==true)
			{
				$connection=@fsockopen("ssl://".$this->host,465);
			}
		else
			{
				$connection=@fsockopen($this->host,25);
			}
		if ($connection==false)
			{
				return false;
			}
		stream_set_timeout($connection,0,50000);
		fgets($connection);
		fwrite($connection,'EHLO '.$hostname."\r\n");
		$response=fgets($connection);
		if (substr($response,0,1)!=2)
			{
				fclose($connection);
				return false;
			}
		while (fgets($connection)!=false)
			{
			}
		if ($this->authentication!=false)
			{
				fwrite($connection,'AUTH '.strtoupper($this->authentication)."\r\n");
				$response=fgets($connection);
				if (substr($response,0,1)!=3)
					{
						fclose($connection);
						return false;
					}
				if (strtoupper($this->authentication)=="LOGIN")
					{
						fwrite($connection,base64_encode($this->username)."\r\n");
						$response=fgets($connection);
						if (substr($response,0,1)!=3)
							{
								fclose($connection);
								return false;
							}
						fwrite($connection,base64_encode($this->password)."\r\n");
						$response=fgets($connection);
						if (substr($response,0,1)!=2)
							{
								fclose($connection);
								return false;
							}				
					}
				elseif (strtoupper($this->authentication)=="PLAIN")
					{
						fwrite($connection,base64_encode($this->username.chr(0).$this->username.chr(0).$this->password)."\r\n");
						$response=fgets($connection);
						if (substr($response,0,1)!=2)
							{
								fclose($connection);
								return false;
							}				
					}
				else
					{
						fclose($connection);
						return false;
					}
			}
		fwrite($connection,'MAIL FROM:<'.$from.'> SIZE='.$size."\r\n");
		$response=fgets($connection);
		if (substr($response,0,1)!=2)
			{
				fclose($connection);
				return false;
			}
		fwrite($connection,'RCPT TO:<'.$to.">\r\n");
		$response=fgets($connection);
		if (substr($response,0,1)!=2)
			{
				fclose($connection);
				return false;
			}
		fwrite($connection,"DATA\r\n");
		$response=fgets($connection);
		if (substr($response,0,1)!=3)
			{
				fclose($connection);
				return false;
			}
		fwrite($connection,$mail."\r\n.\r\n");
		$response=fgets($connection);
		if (substr($response,0,1)!=2)
			{
				fclose($connection);
				return false;
			}
		fwrite($connection,"QUIT\r\n");
		fgets($connection);
		fclose($connection);
		return true;
	}
}
?>
 
An der Mail-Klasse hätte ich auch enorm viel Interesse - würde mich also freuen, wenn du das Fest der Liebe ein paar Minuten am Rechner verbringst ;) - ich werde auf jeden Fall am Ball bleiben und schomal vorab vielen Dank sagen!
 
Ich werd wohl am 24. und 25. Zeit dafuer finden.
Dann werd ich mir mal was aus den Fingern ziehen und 2 neue Tutorials abliefern.

Nachtrag:
Die Tutorials sind fertig.
eMail-Klasse
SMTP-Klasse
Der Code entspricht groesstenteils dem oben geposteten, ist jedoch "etwas umfangreicher" erklaert als dort.
 
Zurück