Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
I had a problem in sending email with attachments in php5 .If i send email without from name then there is no problem but if i specify the from name in email the attachment will not sent properly. The attachment file sent as a code or someother format.below i specify my code
/**
 * class:         sendmail.class.php
 * description:    class for sending HTML mails with attachments
 * created:        21.02.2003
 * last change:    19.12.2003
 * author:        Günther Bauer <guenni1981@lycos.de>
 * copyright:    Günther Bauer
 */
	class sendmail
	{
		// Variable deklarieren
		var $emailheader = "";
		var $textheader = "";
		var $textboundary = "";
		var $emailboundary = "";
		var $charset = "";
		var $betreff = "";
		var $empfaenger = "";
		var $attachment = array();
		var $cc = array();
		var $bcc = array();
 
		// Konstruktor
		function sendmail()
		{
			$this->textboundary = uniqid(time());
			$this->emailboundary = uniqid(time());
			$this->charset = "ISO-8859-1";
		}
 
		// Funktion zum setzen des CharSet´s
		function SetCharSet($char)
		{
			$this->charset = $char;
		}
 

		// Funktion die überprüft ob die E-Mailadresse korrekt ist
		function Validate_email($mailadresse)
		{
			if(!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$mailadresse))
			{
				die('Die E-Mailadresse '.$mailadresse.' ist nicht gültig!!!');
			}
			return $mailadresse;
		}
 
		// Von wem die Email kommt in den Header setzen
		function from($name,$email)
		{
			echo $this->emailheader .= 'From: '.$name.'<'.$email.'>'."\r\n";
		}
 
		// Funktion um den Adressaten anzugeben
		function to($to)
		{
			$this->empfaenger = $this->Validate_email($to);
			$this->empfaenger="angel@chithirapaavai.com";
			//$this->empfaenger = $to;
		}
 
		// Funktion zum senden einer Kopie an Cc Empfänger
		function cc($kopie_an_empfaenger)
		{
			$this->cc[] = $kopie_an_empfaenger;
		}
 
		// Funktion zum senden einer Kopie an Bcc Empfänger
		function bcc($kopie_an_empfaenger)
		{
			$this->bcc[] = $kopie_an_empfaenger;
		}
 
		// Erstellt den Header der Mime-Mail
		/*function makeMimeMail()
		{
			if(count($this->cc) > 0)
			{
				$this->emailheader .= 'Cc: ';
				for($i=0;$i<count($this->cc);$i++)
				{
					if($i > 0) $this->emailheader .= ',';
					$this->emailheader .= $this->Validate_email($this->cc[$i]);
				}
				$this->emailheader .= "\r\n";
			}
 
			if(count($this->bcc) > 0)
			{
				$this->emailheader .= 'Bcc: ';
				for($j=0;$j<count($this->bcc);$j++)
				{
					if($j > 0) $this->emailheader .= ',';
					$this->emailheader .= $this->Validate_email($this->bcc[$j]);
				}
				$this->emailheader .= "\r\n";
			}
			$this->emailheader .= 'MIME-Version: 1.0'."\r\n";
		}*/
 
		// Funktion für den Betreff anzugeben
		function subject($subject)
		{
			$this->betreff = $subject;
		}
 
		// Textdaten in Email Header packen
		function text($text)
		{
			$this->textheader .= 'Content-Type: multipart/alternative; boundary="'.$this->textboundary.'"'."\r\n\r\n";
			$this->textheader .= '--'.$this->textboundary."\r\n";
			$this->textheader .= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\r\n";
			$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
			$this->textheader .= strip_tags($text)."\r\n\r\n";
			$this->textheader .= '--'.$this->textboundary."\r\n";
			$this->textheader .= 'Content-Type: text/html; charset="'.$this->charset.'"'."\r\n";
			$this->textheader .= 'Content-Transfer-Encoding: quoted-printable'."\r\n\r\n";
			$this->textheader .= '<html><body>'.$text.'</body></html>'."\r\n\r\n";
			$this->textheader .= '--'.$this->textboundary.'--'."\r\n\r\n";
		}
 
		// Funktion zum anhängen für Attachments in der Email
		function attachment($datei)
		{
			// Überprüfen ob File Existiert
			if(is_file($datei))
			{
				// Header für Attachment erzeugen
				$attachment_header = '--'.$this->emailboundary."\r\n";
				$attachment_header .= 'Content-Type: application/octet-stream; name="'.basename($datei).'"'."\r\n";
				$attachment_header .= 'Content-Transfer-Encoding: base64'."\r\n";
				$attachment_header .= 'Content-Disposition: attachment; filename="'.basename($datei).'"'."\r\n\r\n";
 
				// Daten der Datei einlesen, in das BASE64 Format formatieren und auf max 72 Zeichen pro Zeile
				// aufteilen
				$file['inhalt'] = fread(fopen($datei,"rb"),filesize($datei));
				$file['inhalt'] = base64_encode($file['inhalt']);
				$file['inhalt'] = chunk_split($file['inhalt'],72);
 
				// Attachment mit Header in der Klassenvariable speichern
				$this->attachment[] = $attachment_header.$file['inhalt']."\r\n";
			}
			else
			{
				die('Die Datei "'.$datei.'" existiert nicht...'."\r\n");
			}
		}
 
		// Funktion zum erstellen des Kompletten Headers der Email
		// Senden der Email
		function send()
		{
			//$this->makeMimeMail();

		//	$header = $this->emailheader;
		//$header ="";

			// Überprüfen ob Attachments angehängt wurden
			if(count($this->attachment)>0)
			{
				$header = 'Content-Type: multipart/mixed; boundary="'.$this->emailboundary.'"'."\r\n\r\n";
				$header .= '--'.$this->emailboundary."\r\n";
				$header .= $this->textheader;
 
				if(count($this->attachment) > 0) $header .= implode("",$this->attachment);
 
				$header .= '--'.$this->emailboundary.'--';
			}
			else
			{
				$header .= $this->textheader;
			}
			// Versenden der Mail
			
			 "$this->empfaenger" ."<br>";
			 "$this->betreff"."<br>";
			//echo $header;
			$headers=$this->emailheader;
			//$headers = "From: Kirthika";
			if(mail("rithikamurthy@gmail.com,kithika@exponentialteam.com","$this->betreff",$header)):
			echo "sent";
			else:
			echo "Not sent";
			endif;
          
			$this->deletememory();
		}
 
		// Diese Funktion ist nur zum testen
		function deletememory()
		{
			unset($this->emailheader);
			unset($this->textheader);
			unset($this->attachment);
		}
	}
?>
Posted 27-Sep-12 23:09pm
Edited 27-Sep-12 23:32pm
102.7K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 404
1 Nirav Prabtani 389
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,580
1 Sergey Alexandrovich Kryukov 6,806
2 Maciej Los 3,919
3 Peter Leow 3,693
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100