Ich lese in letzter Zeit öfters, das einige User Probleme mit der Mail()-Funktion haben. Deshalb habe ich mich mal etwas hingesetzt, und einen SMTP-Mailer entwickelt.
Bitte beachtet diese Anleitung ganz genau, weil schon ein kliner Fehler dazu führt, daß das ganze nicht mehr klappt!
Zuerst braucht man ganz klar ein Forumlar wie dies z.B. (speichert es z.B. unter mail.htm):
<html><head>
<title>SMTP-Mailer by kenny197902</title>
</head><body>
<form method="POST" action="mailsend.php">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td>Empfänger:</td><td><input type="text" name="form_empfaenger" size="30" maxlength="255"></td></tr>
<tr><td>Betreff:</td><td><input type="text" name="form_betreff" size="30" maxlength="255"></td></tr>
<tr><td>Mailtext:</td><td><textarea rows="8" name="form_mail" cols="50"></textarea></td></tr>
</table><input type="submit" value="Abschicken"></form></body></html>
Passe in der folgenden Datei die Einstellungen an und speicher diese unter mailsend.php ab:
<?php
$mail_absender = ""; //Deine reale E-Mail-Adresse. Darf kein Fake sein, weil es sonst nicht klappt.
$mail_server= ""; //Dein Mailserver (z.B. mail.gmx.net)
$mail_usercode = ""; //Dein Zugangsname (z.B. Mailadresse) beim Mailanbieter (bei GMX ist es die Kundennummer!)
$mail_passwort = ""; //Dein Passwort zu deinem E-Mail-Account
// Ab hier nichts mehr ändern!!!
$mail_subject = $_POST["form_betreff"];
$mail_text = $_POST["form_mail"];
$mail_to= $_POST["form_empfaenger"];
define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE); define('SMTP_STATUS_CONNECTED', 2, TRUE);
class smtp{
var $authenticated; var $connection; var $recipients; var $headers; var $timeout; var $errors; var $status; var $body; var $from; var $host; var $port; var $helo; var $auth; var $user; var $pass;
function smtp($params = array()){
if(!defined('CRLF'))
define('CRLF', "\r\n", TRUE);
$this->authenticated = FALSE; $this->timeout = 5; $this->status = SMTP_STATUS_NOT_CONNECTED; $this->host = 'localhost'; $this->port = 25; $this->helo = 'localhost'; $this->auth = FALSE; $this->user = ''; $this->pass = ''; $this->errors = array();
foreach($params as $key => $value){
$this->$key = $value;}}
function &connect($params = array()){if(!isset($this->status)){$obj = new smtp($params);
if($obj->connect()){
$obj->status = SMTP_STATUS_CONNECTED;}
return $obj;
}else{
$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
if(function_exists('socket_set_timeout')){@socket_set_timeout($this->connection, 5, 0);}
$greeting = $this->get_data();
if(is_resource($this->connection)){return $this->auth ? $this->ehlo() : $this->helo();
}else{
$this->errors[] = 'Failed to connect to server: '.$errstr;
return FALSE;}}}
function send($params = array()){foreach($params as $key => $value){$this->set($key, $value);}
if($this->is_connected()){if($this->auth AND !$this->authenticated){
if(!$this->auth())
return FALSE;}
$this->mail($this->from);
if(is_array($this->recipients))
foreach($this->recipients as $value)
$this->rcpt($value);
else
$this->rcpt($this->recipients);
if(!$this->data())
return FALSE;
$headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
$body = str_replace(CRLF.'.', CRLF.'..', $this->body);
$body = $body[0] == '.' ? '.'.$body : $body;
$this->send_data($headers);
$this->send_data('');
$this->send_data($body);
$this->send_data('.');
$result = (substr(trim($this->get_data()), 0, 3) === '250');
//$this->rset();
return $result;
}else{
$this->errors[] = 'Not connected!';
return FALSE;}}
function helo(){if(is_resource($this->connection)
AND $this->send_data('HELO '.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'HELO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;}}
function ehlo(){if(is_resource($this->connection)
AND $this->send_data('EHLO '.$this->helo)
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'EHLO command failed, output: ' . trim(substr(trim($error),3));
return FALSE;}}
function rset(){if(is_resource($this->connection)
AND $this->send_data('RSET')
AND substr(trim($error = $this->get_data()), 0, 3) === '250' ){
return TRUE;
}else{
$this->errors[] = 'RSET command failed, output: ' . trim(substr(trim($error),3));
return FALSE;}}
function quit(){if(is_resource($this->connection)
AND $this->send_data('QUIT')
AND substr(trim($error = $this->get_data()), 0, 3) === '221' ){
fclose($this->connection);
$this->status = SMTP_STATUS_NOT_CONNECTED;
return TRUE;
}else{
$this->errors[] = 'QUIT command failed, output: ' . trim(substr(trim($error),3));
return FALSE;}}
function auth(){
if(is_resource($this->connection)
AND $this->send_data('AUTH LOGIN')
AND substr(trim($error = $this->get_data()), 0, 3) === '334'
AND $this->send_data(base64_encode($this->user))
AND substr(trim($error = $this->get_data()),0,3) === '334'
AND $this->send_data(base64_encode($this->pass))
AND substr(trim($error = $this->get_data()),0,3) === '235' ){
$this->authenticated = TRUE;
return TRUE;
}else{
$this->errors[] = 'AUTH command failed: ' . trim(substr(trim($error),3));
return FALSE;}}
function mail($from){
if($this->is_connected()
AND $this->send_data('MAIL FROM:<'.$from.'>')
AND substr(trim($this->get_data()), 0, 2) === '250' ){
return TRUE;
}else
return FALSE;}
function rcpt($to){
if($this->is_connected()
AND $this->send_data('RCPT TO:<'.$to.'>')
AND substr(trim($error = $this->get_data()), 0, 2) === '25' ){
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;}}
function data(){
if($this->is_connected()
AND $this->send_data('DATA')
AND substr(trim($error = $this->get_data()), 0, 3) === '354' ){
return TRUE;
}else{
$this->errors[] = trim(substr(trim($error), 3));
return FALSE;}}
function is_connected(){
return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));}
function send_data($data){
if(is_resource($this->connection)){
return fwrite($this->connection, $data.CRLF, strlen($data)+2);
}else
return FALSE;}
function &get_data(){
$return = '';
$line = '';
$loops = 0;
if(is_resource($this->connection)){
while((strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' ') AND $loops < 100){
$line = fgets($this->connection, 512);
$return .= $line;
$loops++;}
return $return;
}else
return FALSE;}
function set($var, $value){
$this->$var = $value;
return TRUE;}}
header('Content-Type: text/plain');
$params['host'] = $mail_server;
$params['port'] = 25;
$params['auth'] = TRUE;
$params['user'] = $mail_usercode;
$params['pass'] = $mail_passwort;
$send_params['recipients'] = array($mail_to);
$send_params['headers'] = array('From: '.$mail_absender, 'To: '.$mail_empfaenger, 'Subject: '.$mail_subject);
$send_params['from'] = $mail_absender;
$send_params['body'] = $mail_text;
if(is_object($smtp = smtp::connect($params)) AND $smtp->send($send_params)){
echo "Die Mail wurde erfolgreich versendet!";
exit;
}else{
echo "Die Mail konnte nicht versendet werden! Es gab möglicherweise Eingabefehler beim Formular!";
exit;
}
?>
So, und jetzt wünsche ich viel Spaß beim Mailen!
Solltest ihr Probleme haben, schreibt mich am besten per pn an!
Euer kenny197902
PS: Ein paar kleine Teile des Scripts wurden von der Seite http://www.baseboard.net/forums/showthread.php?s=&threadid=1995 übernommen. Ich habe den Autor angeschrieben, und seine Zustimmung zur Verwendung erhalten!