Email mit Anhang

mtk-flo

Erfahrenes Mitglied
Ich verschicke schon Emails.
Das geht auch wunderbar.

Nur will ich nun auch noch ein Anhang vom typ .zip verschicken.

wie muss ich das in mein scriupt einbauen ?

PHP:
  $to_email = "email@zuhause.de";
  $text = "hier ist der anhang";
  $betreff = "Sie haben Post";
  
  mail($to_email, $betreff, $text,
  		"From: email@domain.de\r\n" .
  		"Reply-To: email@domain.de\r\n" .
  		"X-Mailer: PHP/" . phpversion());
 
Guck Dir mal die beiden folgenden Scripts an.
Besonders das erste duerfte fuer Dich interessant sein, da Du dort nachvollziehen koennen solltest wie die eMail aufgebaut wird und wie die Anhaenge dort eingefuegt werden.

Uebrigens, diese Klasse funktioniert in der Form nur in PHP5, fuer PHP4 muss die etwas umgeschrieben werden.
Ausserdem werden zur richtigen Kodierung der Mail die IMAP-Funktionen benoetigt.

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

	public function __construct($to,$subject,$body,$from="",$cc="",$bcc="")
	{
		$this->id=md5(uniqid());
		$this->to=$to;
		$this->subject=$subject;
		$this->body=$body;
		$this->from=$from;
		$this->cc=$cc;
		$this->bcc=$bcc;
		$this->attachedfiles=array();
	}

	public function destroy()
	{
		if (file_exists("mailtemp/".$this->id))
			{
				for ($attachment=0;$attachment<count($this->attachedfiles);$attachment++)
					{
						unlink("mailtemp/".$this->id."/".$this->attachedfiles[$attachment]);
					}
				rmdir("mailtemp/".$this->id);
			}
	}

	public function attachfile($tempfile,$filename)
	{
		if (!file_exists("mailtemp/".$this->id))
			{
				mkdir("mailtemp/".$this->id);
			}
		move_uploaded_file($tempfile,"mailtemp/".$this->id."/".$filename);
		$this->attachedfiles[]=$filename;
	}

	public function removeattachment($filename)
	{
		unlink("mailtemp/".$this->id."/".$filename);
		$newattachedfiles=array();
		for ($attachment=0;$attachment<count($this->attachedfiles);$attachment++)
			{
				if ($this->attachedfiles[$attachment]!=$filename)
					{
						$newattachedfiles[]=$this->attachedfiles[$attachment];
					}
			}
		$this->attachedfiles=$newattachedfiles;
		unset($newattachedfiles);
	}

	public function send()
	{
		$body=imap_8bit($this->body);
		$body.="\n\n";
		$boundary="----".$this->id;
		$email="";
		$email.="Content-class: urn:content-classes:message";
		$email.="\nUser-Agent: Free WebMail";
		$email.="\nMIME-Version: 1.0";
		if (!empty($this->attachedfiles))
			{
				$email.="\nContent-Type: multipart/mixed;\n\tboundary=\"".$boundary."\"";
			}
		if (!empty($this->from))
			{
				$email.="\nFrom: ".$this->from;
			}
		if (!empty($this->cc))
			{
				$email.="\nCc: ".$this->cc;
			}
		if (!empty($this->bcc))
			{
				$email.="\nBcc: ".$this->bcc;
			}
		$email.="\nX-Priority: 3 (Normal)";
		$email.="\nImportance: Normal";
		if (!empty($this->attachedfiles))
			{
				$email.="\n\n--".$boundary;
			}
		$email.="\nContent-Type: text/plain;\n\tcharset=\"iso-8859-1\"";
		$email.="\nContent-Transfer-Encoding: quoted-printable";
		$email.="\nContent-Disposition: inline";
		$email.="\n\n".$body;
		if (!empty($this->attachedfiles))
			{
				$email.="\n\n--".$boundary;
			}
		for ($attachment=0;$attachment<count($this->attachedfiles);$attachment++)
			{
				$file=fopen("mailtemp/".$this->id."/".$this->attachedfiles[$attachment],"r");
				$content=fread($file,filesize("mailtemp/".$this->id."/".$this->attachedfiles[$attachment]));
				fclose($file);
				$encodedfile=chunk_split(base64_encode($content));
				$email.="\nContent-Type: application/octet-stream;\n\tname=\"".$this->attachedfiles[$attachment]."\"";
				$email.="\nContent-Transfer-Encoding: base64";
				$email.="\nContent-Description: ".$this->attachedfiles[$attachment];
				$email.="\nContent-Disposition: attachment;\n\tfilename=\"".$this->attachedfiles[$attachment]."\"";
				$email.="\n\n".$encodedfile."\n\n--".$boundary;
			}
		if (!empty($this->attachedfiles))
			{
				$email.="--";
			}
		imap_mail($this->to,$this->subject,"",$email);
	}
}

function mailstring($mailobject)
{
	return rawurlencode(serialize($mailobject));
}

function mailobject($mailstring)
{
	return unserialize(stripslashes(rawurldecode($mailstring)));
}
?>

sendmail.php
PHP:
<?php
require("email.class.php");
if ((isset($_POST['sendmail'])) || (isset($_POST['attachfile'])) || (isset($_POST['removeattachment'])))
	{
		if (isset($_POST['mailto']))
			{
				$mailto=$_POST['mailto'];
			}
		else
			{
				$mailto="";
			}
		if (isset($_POST['subject']))
			{
				$subject=$_POST['subject'];
			}
		else
			{
				$subject="";
			}
		if (isset($_POST['message']))
			{
				$body=$_POST['message'];
			}
		else
			{
				$body="";
			}
		if (!isset($_POST['mail']))
			{
				$mail=new email($mailto,$subject,$body,"ich@irgendwodahinten.de");
			}
		else
			{
				$mail=mailobject($_POST['mail']);
				if ($mail->to!=$mailto)
					{
						$mail->to=$mailto;
					}
				if ($mail->subject!=$subject)
					{
						$mail->subject=$subject;
					}
				if ($mail->body!=$body)
					{
						$mail->body=$body;
					}
			}
		if (isset($_POST['attachfile']))
			{
				if ($_FILES['uploadfile']['size']>0)
					{
						$mail->attachfile($_FILES['uploadfile']['tmp_name'],$_FILES['uploadfile']['name']);
					}
			}
		if (isset($_POST['removeattachment']))
			{
				$postkeys=array_keys($_POST);
				for ($keycount=0;$keycount<count($postkeys);$keycount++)
					{
						if (substr($postkeys[$keycount],0,13)=="delattachment")
							{
								$mail->removeattachment($_POST[$postkeys[$keycount]]);
							}
					}
			}
		if (isset($_POST['sendmail']))
			{
				$mail->send();
				$mail->destroy();
				unset($mail);
			}
	}
echo '<html>';
echo '<body>';
echo '<form method="post" action="sendmail.php" enctype="multipart/form-data">';
echo 'Recipient:';
echo '<input type="text" name="mailto"';
if (isset($mail))
	{
		echo ' value="'.$mail->to.'"';
	}
echo '><br>';
echo 'Subject:';
echo '<input type="text" name="subject"';
if (isset($mail))
	{
		echo 'value="'.$subject.'"';
	}
echo '><br>';
echo '<textarea name="message">';
if (isset($mail))
	{
		echo $mail->body;
	}
echo '</textarea><br>';
if (isset($mail))
	{
		for ($attachment=0;$attachment<count($mail->attachedfiles);$attachment++)
			{
				echo '<input type="hidden" name="attachment'.$attachment.'" value="'.$mail->attachedfiles[$attachment].'">';
				echo '<input type="checkbox" name="delattachment'.$attachment.'" value="'.$mail->attachedfiles[$attachment].'">'.$mail->attachedfiles[$attachment].'<br>';
			}
		if (count($mail->attachedfiles)>0)
			{
				echo '<input type="submit" name="removeattachment" value="Remove selected attachments"><br>';
			}
	}
echo '<input type="file" name="uploadfile">';
echo '<input type="submit" name="attachfile" value="Attach file"><br>';
echo '<input type="submit" name="sendmail" value="Send eMail">';
if (isset($mail))
	{
		echo '<input type="hidden" name="mail" value="'.mailstring($mail).'">';
	}
echo '</form>';
echo '</body>';
echo '</html>';
?>
 
Zuletzt bearbeitet:
Naja, ein Mail mit Anhang zu verschicken ist nicht unbedingt das Einfachste was man sich vorstellen kann.

Willst Du nur einen Anhang haben oder auch mehrere?
Soll der Anhang hochgeladen werden oder liegt der schon auf dem Server?
 
Hm... Hab das ganze eben ausprobiert.. Krieg folgende Fehlermeldung:

Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/www/web15/html/email.class.php on line 4

Woran könnte das liegen?
 
Daran:
Dennis Wronka hat gesagt.:
Uebrigens, diese Klasse funktioniert in der Form nur in PHP5, fuer PHP4 muss die etwas umgeschrieben werden.
PHP4 kennt Schluesselworte wie public und private noch nicht.

Eine PHP4-Version der Klasse kannst Du ueber den Link in meiner Signatur bekommen.
 

Neue Beiträge

Zurück