E-mail mit Anhang (MIME)

Dolphon

Erfahrenes Mitglied
Moin.
Hab mir mal den Mime Code gezogen,
und versucht in eines meiner Skript zu includen.
Allerdigns habe ich mit dem Code so meine Probs, weil ich nicht weiß wo die Empfängeraddy
hinkommt. Damit das Skript weiß an wem er die E-mail schicken soll.

Hier mal der Code den ich include.


PHP:
<?php
/**
* Filename.......: example.1.php
* Project........: HTML Mime Mail class
* Last Modified..: 15 July 2002
*/
        error_reporting(E_ALL);
        include('htmlMimeMail.php');

/**
* Example of usage. This example shows
* how to use the class with html,
* embedded images, and an attachment.
*/
        /**
        * Create the mail object.
        * No longer takes any arguments
        */
        $mail = new htmlMimeMail();

        /*
        * Read the image background.gif into
        * $background
        */
        $background = $mail->getFile('background.gif');

        /*
        * Read the file test.zip into $attachment.
        */
        $attachment = $mail->getFile('example.zip');

        /*
        * Get the contents of the example text/html files.
        * Text/html data doesn't have to come from files,
        * could come from anywhere.
        */
        $text = $mail->getFile('example.txt');
        $html = $mail->getFile('example.html');

        /*
        * Add the text, html and embedded images.
        * The name (background.gif in this case)
        * of the image should match exactly
        * (case-sensitive) to the name in the html.
        */
        $mail->setHtml($html, $text);
        $mail->addHtmlImage($background, 'background.gif', 'image/gif');

        /*
        * This is used to add an attachment to
        * the email. Due to above, the $attachment
        * variable contains the example zip file.
        */
        $mail->addAttachment($attachment, 'example.zip', 'application/zip');

        /*
        * Set the return path of the message
        */
        $mail->setReturnPath('dolphon@mugamo.de');
        
        /**
        * Set some headers
        */
        $mail->setFrom('"Dolphon" <dolphon@mugamo.de>');
        $mail->setSubject('Test mail');
        $mail->setHeader('X-Mailer', 'HTML Mime mail class (http://www.phpguru.org)');
        
        /**
        * Send it using SMTP. If you're using Windows you should *always* use
        * the smtp method of sending, as the mail() function is buggy.
        */
        $result = $mail->send(array('postmaster@localhost'), 'smtp');

        // These errors are only set if you're using SMTP to send the message
        if (!$result) {
            print_r($mail->errors);
        } else {
            echo 'Mail sent!';
        }
?>
 

metua

Grünschnabel
PHP:
        /**
        * Send it using SMTP. If you're using Windows you should *always* use
        * the smtp method of sending, as the mail() function is buggy.
        */
        $result = $mail->send(array('postmaster@localhost'), 'smtp');

        // These errors are only set if you're using SMTP to send the message
        if (!$result) {
            print_r($mail->errors);
        } else {
            echo 'Mail sent!';
        }
?>

Hier wird über SMTP verschickt, d.h. die Email-addy kommt in die Anweisung
PHP:
$result = $mail->send(array('postmaster@localhost'), 'smtp');
Solltest du nicht SMTP verwenden, ist die function mail() dein Freund....
Meines wissens ist auch die nicht-SMTP-Variante in den Examples des downloadbaren Pakets enthalten.

Ich habe noch ein anderes Script in Verwendung, vielleicht genügt es ja deinen Ansprüchen. Ich habe es irgendwann einmal gefunden:
PHP:
<?php
//Script zum Versenden von Mails mit Anhang
function mail_att($to,$subject,$message,$anhang)
   {
   $absender = "DeinOnlinemailer";
   $absender_mail = "anfrage@DeineDomain.de";
   $reply = "noreply@DeineDomain.de";
   $mime_boundary = "-----=" . md5(uniqid(mt_rand(), 1));
	//emailheader wird zusammengebastelt
   $header  ="From:".$absender."<".$absender_mail.">\n";
   $header .= "Reply-To: ".$reply."\n";
   $header.= "MIME-Version: 1.0\r\n";
   $header.= "Content-Type: multipart/mixed;\r\n";
   $header.= " boundary=\"".$mime_boundary."\"\r\n";
   //Content der email wird zusammengebastelt
   $content = "This is a multi-part message in MIME format.\r\n\r\n";
   $content.= "--".$mime_boundary."\r\n";
   $content.= "Content-Type: text/html charset=\"iso-8859-1\"\r\n";
   $content.= "Content-Transfer-Encoding: 8bit\r\n\r\n";
   $content.= $message."\r\n";

   //$anhang ist ein Mehrdimensionals Array
   //$anhang enthält mehrere Dateien
   if(count($anhang)>1){
	if(is_array($anhang) AND is_array(current($anhang)))
      {
      foreach($anhang AS $dat)
         {
         $data = chunk_split(base64_encode($dat['datei']));
         $content.= "--".$mime_boundary."\r\n";
         $content.= "Content-Disposition: attachment;\r\n";
         $content.= "\tfilename=\"".$dat['name']."\";\r\n";
         $content.= "Content-Length: .".$dat['size'].";\r\n";
         $content.= "Content-Type: ".$dat['type']."; name=\"".$dat['name']."\"\r\n";
         $content.= "Content-Transfer-Encoding: base64\r\n\r\n";
         $content.= $data."\r\n";
         }
      $content .= "--".$mime_boundary."--"; 
      }
	else //Nur 1 Datei als Anhang
      {
			$data = chunk_split(base64_encode($anhang['datei']));
		  $content.= "--".$mime_boundary."\r\n";
	      $content.= "Content-Disposition: attachment;\r\n";
	      $content.= "\tfilename=\"".$anhang['name']."\";\r\n";
	      $content.= "Content-Length: .".$anhang['size'].";\r\n";
	      $content.= "Content-Type: ".$anhang['type']."; name=\"".$anhang['name']."\"\r\n";
	      $content.= "Content-Transfer-Encoding: base64\r\n\r\n";
	      $content.= $data."\r\n";
     }   
   }
	if(mail($to, $subject,$content,$header)){
		return true;
	}else{
	return false;
   }
   }



Gruß
metua
 
Zuletzt bearbeitet: