
July 17th, 2005, 09:03 PM
|
 |
Coding is my mission
|
|
Join Date: Aug 2004
Location: Willebroek, Belgium
|
|
|
[PHP] VerySimpleMailTransport
I wrote my own class to send an e-mail via an smtp server. With VSMTP you can connect to any smtp server (authentication not suppored yet) and you will be able to send e-mail from server that don't support smtp via PHP. I tested it on my smtp server and some guys on another forum also tested it on some other (dutch and belgian) servers. Does it work on your smtp-server?
PHP Code:
<?php
class vsmtp{
/* LICENSE
VerySimpleMailTransport: php e-mailing made easy
Copyright (C) 2005 Aries Soft
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
var $mayor_version = 0;
var $minor_version = 2;
/* HISTORY
V 0.1 : First version, so nothing really to fix ...
V 0.2 : - Debugging added
*/
/* EXAMPLE
include("vsmtp.php");
$mail = new vsmtp;
$mail->smtp_host = "smtp.host.com";
$mail->smtp_port = "25" // default is 25
$mail->recipient = "you@host.com";
$mail->sender = "me@host.com";
$mail->subject = "It's a test";
$mail->body = "Read this ...";
$mail->send();
*/
// email related variables
var $recipient;
var $sender;
var $subject;
var $body;
// smtp related variables
var $smtp_host;
var $smtp_port = 25;
var $smtp_username;
var $smtp_password;
// vsmtp related variables
var $ouput = ""; // for debugging
function send(){
if($this->smtp_host != "" || $this->smpt_port != ""){
// creating the socket connection
$smtp_socket = fsockopen($this->smtp_host , $this->smtp_port);
if($smtp_socket == false){
return false;
exit();
}
$this->output = "Connection has been made ...";
// validate smtp host
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
if(substr($get , 0 , 3) != "220"){
// error 1: 220 was not retrieved
$this->output .= "<br /># ERROR[1]: This is probably not a valid smtp server";
exit();
}
// are we welcome?
$this->output .= "<br />" . htmlentities("<< HELO localdomain.com");
if(!fputs($smtp_socket,"HELO localdomain.org\r\n")) $this->output .= " <--- COULD NOT SEND";
// server response
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
if(substr($get , 0 , 3) != "250"){
// error 2: 250 was not retrieved
$this->output .= "<br /># ERROR[2]: We aren't welcome";
exit();
}
if($this->recipient != "" || $this->sender != "" || $this->subject != "" || $this->body != ""){
// who is it FROM?
$this->output .= "<br />" . htmlentities("<< MAIL FROM:<$this->sender>");
if(!fputs($smtp_socket,"MAIL FROM:<" . $this->sender . ">\r\n")) $this->output .= " <--- COULD NOT SEND";
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
if(substr($get , 0 , 3) != "250"){
// error 3: 250 was not retrieved
$this->output .= "<br /># ERROR[3]: Sender e-mail not accepted";
exit();
}
// who is it FOR (TO)?
$this->output .= "<br />" . htmlentities("<< RCPT TO:<$this->recipient>");
if(!fputs($smtp_socket,"RCPT TO:<" . $this->recipient . ">\r\n")) $this->output .= " <--- COULD NOT SEND";
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
if(substr($get , 0 , 3) != "250"){
// error 4: 250 was not retrieved
$this->output .= "<br /># ERROR[4]: Recipient e-mail not accepted";
exit();
}
// I'm listening ...
$this->output .= "<br />" . htmlentities("<< DATA");
if(!fputs($smtp_socket,"DATA\r\n")) $this->output .= " <--- COULD NOT SEND";
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
// set the subject
fputs($smtp_socket,"Subject: $this->subject\r\n");
// start the message
fputs($smtp_socket,"$this->body\r\n");
fputs($smtp_socket,"\r\n.\r\n");
$get = fgets($smtp_socket);
$this->output .= "<br />" . htmlentities(">> $get");
if(substr($get , 0 , 3) != "250"){
// error 5: 250 was not retrieved
$this->output .= "<br /># ERROR[5]: Data transfer not allowed";
exit();
}
// closing connection
fclose($smtp_socket);
}
}
}
}
?>
Just paste this in a file vsmtp.php and include it ... See example in code for more information ...
Hoping for quick response ...
|