Hallo!
Es wäre vielleicht hilfreich das ganze Script zu sehen, um etwas über deine Fehler sagen zu können. Ich habe mich allerdings vor längerer Zeit auch mal mit dem
Simple Mail Transfere Protocol (
RFC 821 oder
RFC 2821) beschäftigt, da mir die PHP eigene
mail()-Funktion sehr zu Lasten gefallen ist. Dabei ist eine kleine SMTP-Klasse entstanden (Code unten). Mit dieser Klasse kann man relativ einfach und sicher Mails versenden ohne den Stress mit mail() zu haben. Ein Beispiel für den Aufruf der Klasse ist am Ende des Codes zu finden. Da ich leider momentan nicht zuhause bin kann ich leide nicht sagen, ob dies die aktuellste Version ist, sollte sie aber sein.
MfG, Andy
PHP-Code:
<?php
error_reporting(E_ALL);
/***
* Class SmtpConnect
*
* The SmtpConnect class enables sending mails via SMTP.
* It is not very easily operated, but that is not required,
* because this class should just be the base for an comfortable
* Mail class. On the basis of some comments to this class i want
* to add that it sure supports Secure Sockets Layer connections.
* You just have to modify the host and port in the constructor.
*
* @package Mail
* @subpackage SmtpConnect
* @version 0.4
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class SmtpConnect
{
// declare class variables
private $host;
private $port;
private $sock;
private $auth;
private $authTypes = array(
'LOGIN',
'PLAIN');
private $response;
private $log;
/**
* Constructor - Is called when the class is instanced
*
* @access: public
* @param Str $host
* @param Int $port
* @return NONE
*/
public function __construct($host='localhost', $port=25)
{
// set server-variables
$this->host = $host;
$this->port = $port;
}
/**
* connect() - Connects to the given server
*
* @access: public
* @return Boolean
*/
public function connect()
{
// control-connection handle is saved to $handle
$this->sock = @fsockopen($this->host, $this->port);
if ( !$this->sock OR !$this->check('220') )
throw new Exception("Connection failed.");
// switch to non-blocking mode - just return data no response
set_socket_blocking($this->sock, true);
// set timeout of the server connection
stream_set_timeout($this->sock, 0, 200000);
return true;
}
/**
* ehlo() - Sends greeting to secured server
*
* @access: public
* @return Boolean
*/
public function ehlo()
{
// send EHLO -spezified in RFC 2554
$this->cmd("EHLO " . $this->host);
if( !$this->check('250') )
throw new Exception("Failed to send EHLO.");
return true;
}
/**
* helo() - Sends greeting to server
*
* @access: public
* @return Boolean
*/
public function helo()
{
// Send the RFC821 specified HELO.
$this->cmd('HELO ' . $this->host);
if( !$this->check('250') )
throw new Exception("Failed to send HELO.");
return true;
}
/**
* auth() - Sends authentification
*
* @access: public
* @param Str $user
* @param Str $pwd
* @param Str $type
* @return Boolean
*/
public function auth($user, $pwd, $type)
{
if( in_array($type, $this->authTypes) )
{
// send authentification-identifier
$this->cmd("AUTH $type");
// catch first ready response
$response = $this->getReply();
if( substr($response,0,1) != 3 )
{
throw new Exception("Failed to send AUTH.");
}
}
if( $type == 'LOGIN' )
{
// send user-name
$this->cmd(base64_encode($user));
if( !$this->check('334') )
throw new Exception("Failed to send user-name.");
// send password
$this->cmd(base64_encode($pwd));
}
elseif( $type == 'PLAIN' )
{
// prepare data
$data = base64_encode($user.chr(0).$user.chr(0).$pwd);
$this->cmd($data);
}
else
throw new Exception("Authentification failed.");
if( !$this->check('235') )
{
throw new Exception("Authentification failed.");
}
return true;
}
/**
* from() - Sends specified addressor
*
* @access: public
* @param Str $from
* @return Boolean
*/
public function from($from)
{
// specify addressor
$this->cmd("MAIL FROM: $from");
if( !$this->check('250') )
throw new Exception("Failed to send addressor.");
return true;
}
/**
* rcpt() - Sends specified acceptor
*
* @access: public
* @param Str $to
* @return Boolean
*/
public function rcpt($to)
{
// send specified acceptor
$this->cmd("RCPT TO: $to");
if( !$this->check('250') )
throw new Exception("Failed to send acceptor.");
return true;
}
/**
* data() - Sends the data to the server
*
* @access: public
* @param Str $message
* @param Arr $header
* @return NONE
*/
public function data($message, $header)
{
// initiate data-transfere
$this->cmd('DATA');
if( !$this->check('354') )
throw new Exception("Data-transfere failed.");
// validate header-data
if( !is_array($header) )
throw new Exception("Header-data must be an array.");
// initiate counter
$i = 0;
// include header data
foreach( $header as $key => $value)
{
// send header
if( $i < count($header)-1 )
{
$this->cmd("$key: $value");
}
else
{
$this->cmd("$key: $value\r\n");
}
$i++;
}
// send the message
$this->cmd("$message\r\n");
// send end parameter
$this->cmd('.');
$this->check('250');
}
/**
* quit() - Closes the server-connection
*
* @access: public
* @return NONE
*/
public function quit()
{
$this->cmd("QUIT");
$this->check('221');
fclose($this->sock);
return true;
}
/**
* cmd() - Sets a ftp-command given by the user
*
* @access: public
* @param Str $cmd
* @return NONE
*/
public function cmd($cmd)
{
fputs($this->sock, "$cmd\r\n");
$this->log("> $cmd");
}
/**
* getReply() - Catches the reply of the server
*
* @access: public
* @return String
*/
public function getReply()
{
$go = true;
$message = "";
do
{
$tmp = @fgets($this->sock, 1024);
if($tmp === false)
{
$go = false;
}
else
{
$message .= $tmp;
if( preg_match('/^([0-9]{3})(-(.*[\r\n]{1,2})+\\1)? [^\r\n]+[\r\n]{1,2}$/', $message) ) $go = false;
}
} while($go);
$this->log($message);
return $message;
}
/**
* valid() - Checks if the response of a command is ok
*
* @access: public
* @return Boolean
*/
public function valid()
{
// get response of the server
$this->response = $this->getReply();
// check the response and say if everything is allright
return (empty($this->response) || preg_match('/^[5]/', $this->response)) ? false : true;
}
/**
* check() - Checks if the response-code is correct
*
* @access: public
* @param Str $code
* @return Boolean
*/
public function check($code)
{
if( $this->valid() )
{
$pat = '/^'. $code .'/';
if( preg_match($pat, $this->response))
{
return true;
}
}
return false;
}
/**
* log() - Saves all request to the server and their responses into $this->log
*
* @access: private
* @return NONE
*/
private function log($str)
{
$this->log .= "$str<br />";
}
/**
* getLog() - Prints out all requests to the server and their responses
*
* @access: public
* @return NONE
*/
public function getLog()
{
return $this->log;
}
}
try
{
$from = 'pseudo@web.net';
$to = 'test@web.net';
$date = date('r');
$header = array(
'Date' => $date,
'From' => $from,
'Subject' => 'Sending mail via PHP!',
'To' => $to,
'X-Mailer' => 'Avedo Mailer');
$message = "Hello. \nIf you can read this message, I was able to send my first mail with my new SmtpConnect-Class. \nThat's great! So have a beer and enjoy the show! \nMfG, Andy";
$mail = new SmtpConnect('host');
$mail->connect();
$mail->ehlo();
$mail->auth('user', 'pwd', 'PLAIN');
$mail->from($from);
$mail->rcpt($to);
$mail->data($message, $header);
$mail->quit();
echo $mail->getLog();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>