SMTP mailer

Status
Niet open voor verdere reacties.

slabbetje

Gebruiker
Lid geworden
5 mei 2007
Berichten
290
Beste helpers ik ben bedrijfsmatig bezig met een smtp mailer deze is nu zo goed als af.
Ik kom er alleen even niet uit hoe ik meerdere datatypes kan sturen omdat ik nooit met smtp werk..

Het is de bedoeling dat de volgende data word verzonden

Htmltekst
Plaintekst wanneer de browser/mailclient geen html ondersteund
Bijlages

Edit
Hij stuurt nu bijde berichten
Het probleem is nu alleen nog dat het bestand wat niet gelezn kan worden als bijlage word gestuurd...

PHP:
<?php
Class Mail
{
	public $smtpServer;
	public $port;
	public $timeout;
	public $username;
	public $password;
	public $localhost;
	public $errors;
	
	//
	// Starting the constructor
	//
	public function __construct($smtpServer="localhost",$port="25",$timeout="30",$username=NULL,$password=NULL)
	{
		//Define variables
		$this->smtpServer = $smtpServer;
		$this->port = $port;
		$this->timeout = $timeout;
		$this->username = $username;
		$this->password = $password;
		$this->localhost = "localhost";
		
		$this->errors = Array();
	}
	
	//
	//Sending the email
	//
	public function send($to, $from, $subject, $message, $htmlmessage)
	{
		echo $this->smtpServer . "<br/>";
		echo $this->port . "<br/>";
		echo $this->timeout . "<br/>";
		echo $this->username . "<br/>";
		echo $this->password . "<br/>";
		echo $this->localhost . "<br/>";
		echo "_____________________________________<br/>";
		echo $to . "<br/>";
		echo $from . "<br/>";
		echo $subject . "<br/>";
		echo $message . "<br/>";
		
		$smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
		$smtpResponse = fgets($smtpConnect, 515);
		
		if(empty($smtpConnect))
		{
			echo "Cant connect to the smtp server";
		}
		//Authorize
		fputs($smtpConnect,"AUTH LOGIN" . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
		fputs($smtpConnect, base64_encode($this->username) . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
		fputs($smtpConnect, base64_encode($this->password) . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);

		//Say hello to smtp
		fputs($smtpConnect, "HELO " . $this->localhost . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);		
		echo "<br/>HI smtp" . $this->localhost . "<br/>";		
		fputs($smtpConnect, "MAIL FROM: " . $from . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
		echo "" . $from . "<br/>";		
		fputs($smtpConnect, "RCPT TO: " . $to . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
		echo "" . $to . "<br/>";
		fputs($smtpConnect, "DATA" . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
		echo "DATA<br/>";
		
		//STart mail
		$test="";
		$uid = md5(uniqid(time()));
		
		$header = "From: ".$from."\r\n";
		$header .= "Reply-To: ".$to."\r\n";
		$header .= "Subject: " . $subject ."\r\n"; 
		$header .= "MIME-Version: 1.0\r\n";
		$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
		$header .= "--".$uid."\r\n";
		$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
		$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		$header .= "$message \r\n\r\n";
		$header .= "--".$uid."\r\n";
		$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
		$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
		$header .= "$htmlmessage \r\n\r\n";
		$header .= "--".$uid."--";

		fputs($smtpConnect, "$header\n\n$test\n.\n");
		$smtpResponse = fgets($smtpConnect, 515);		
		
		//End mail
		fputs($smtpConnect,"QUIT" . "\r\n");
		$smtpResponse = fgets($smtpConnect, 515);
	}
	
	//
	//Output the errors etc.
	//
	public function __destruct()
	{
		for ($i=0; $i<count($this->errors); $i++)
		{
			echo $this->errors[$i] . "<br/>\n";
		}
	}

}
?>
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan