SMTP Mail problem
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
I've found a function on the web for an SMTP Mail thing... I took it and converted the function into a class and I got the following class:
The problem with it is... it doesn't send the mail... I get the error (Well, send() returns false, meaning that it doesn't send). Any help here? I've checked everything that is being submitted into the class... it's something that the class is doing that is wrong... any help?
I can't find that function right now... it was a tutorial with the code just given there so I copied/pasted, changed it to fit my needs, added a few lines and a few functions, cut it into functional pieces and got the class I showed above.
To see it in action, you can try filling this form in. To make you feel better
, I'm not saving the records or anything. Nothing is happening to them. The only thing that SHOULD happen is you receiving the email with your username and password... that is the ONLY thing that SHOULD happen, but DOESN'T. So right now, all your doing is submitting to see the error 
Thanks for any help I might/will get
PHP Code: [ Select ]
<?php
class smtp_mail {
// Setting the class variables that hold special function for the use of the class.
var $template = null; // The template used to create the email body (If need be)
var $vari = array(); // The variables used in the mail template
var $use_mail = false; // Determines if we should use the PHP mail() function. If set to false, we will use SMTP Authentication
var $body = null; // The variable holding the body template if template is set
var $not_null = false; // Used to determine how the body looks like
var $smtpServer = "smtp.webfaction.com"; // IP address of the mail server. This can also be the local domain name
var $port = '25'; // Should be 25 by default, but needs to be whichever port the mail server will be using for smtp
var $timeout = "30"; // Typical timeout. try 45 for slow servers
var $username = "bogey"; // The login for your smtp
var $password = "bogdan(elite)"; // The password for your smtp
var $localhost = "127.0.0.1"; // Defined for the web server. Since this is where we are gathering the details for the email
var $newLine = "\r\n"; // AKA, carrage return line feed. var just for newlines in MS
var $secure = 0; // Change to 1 if your server is running under SSL
var $sent = false; // Variable that holds the information if the mail was sent successfully or not
var $try_mail = false; // Checking if we should try the PHP function MAIL first before using the SMTP
function __construct($message = null)
{
// Checking if we need to go off of a template
if(!is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
$this->not_null = true;
}
// If no template is present, then we use a message passed to the script
if(!is_null($message) && is_null($this->template))
{
$this->body = $message;
}
elseif(!is_null($message) && !is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html') . "\r\n\r\n\r\n" . $message;
}
elseif(is_null($message) && !is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
}
}
function m_connect()
{
// Connect to SMTP
$this->smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
$smtpResponse = fgets($this->smtpConnect, 4096);
if(empty($this->smtpConnect))
{
return false;
}
else
{
return true;
}
}
function prepare_headers()
{
stream_set_blocking($this->smtpConnect, 0);
// You have to say HELO again after TLS is started
fputs($this->smtpConnect, "HELO {$this->localhost}". $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['heloresponse2'] = $smtpResponse;
// Request for auth login
fputs($this->smtpConnect, "AUTH LOGIN" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authrequest'] = $smtpResponse;
// Send the username
fputs($this->smtpConnect, base64_encode($this->username) . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authusername'] = $smtpResponse;
// Send the password
fputs($this->smtpConnect, base64_encode($this->password) . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authpassword'] = $smtpResponse;
// Email from
fputs($this->smtpConnect, "MAIL FROM: <{$this->vari['FROM']}>" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['mailfromresponse'] = $smtpResponse;
// Email to
fputs($this->smtpConnect, "RCPT TO: <{$this->vari['TO']}>" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['mailtoresponse'] = $smtpResponse;
// The email
fputs($this->smtpConnect, "DATA" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['data1response'] = $smtpResponse;
// Construct headers
$headers = "MIME-Version: 1.0" . $this->newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
$headers .= "To: {$this->vari['NAME_TO']} <{$this->vari['TO']}>" . $this->newLine;
$headers .= "From: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "Reply-To: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "Return-Path: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "CC: {$this->vari['NAME_CC']} <{$this->vari['CC']}>" . $this->newLine;
$headers .= "BCC: {$this->vari['NAME_BCC']} <{$this->vari['BCC']}>" . $this->newLine;
// Observe the . after the newline, it signals the end of message
fputs($this->smtpConnect, "To: {$this->vari['TO']}\r\nFrom: {$this->vari['FROM']}\r\nSubject: {$this->vari['SUBJECT']}\r\n$headers\r\n\r\n{$this->body}\r\n.\r\n");
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['data2response'] = $smtpResponse;
// Say goodbye
fputs($this->smtpConnect,"QUIT" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['quitresponse'] = $smtpResponse;
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($this->smtpConnect);
// A return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}
function prepare_body($message = null)
{
// Checking if the body is empty or null
if(is_null($this->body) || empty($this->body))
{
// Making sure that the message here is not null
if(!is_null($message))
{
$this->body = $message;
}
else
{
return false;
}
}
// Checking if we are using a template
if($this->not_null)
{
// Setting the template object.
$tpl = new tpl();
$tpl->vari = $mail->vari;
$this->body = $tpl->svariable(true);
}
else
{
return true;
}
return false;
}
function prepare_footers()
{
// Checking if we need to add a footer
if(isset($this->vari['footer']))
{
// Checking if the footer is a string or a file
$footer = ((is_file($this->vari['footer']) && file_exists($this->vari['footer'])) ? file_get_contents($this->vari['footer']) : $this->vari['footer']);
// Adding the footer to the end of the file
$this->body .= "\n\r\n\r<hr />\n\r" . $footer;
}
}
function send()
{
// Checking if we should try the PHP mail function first
if($this->try_mail === true)
{
// Sending the message and checking if it is actually sent
if(mail($this->vari['to'], $this->vari['subject'], $this->body))
{
// The message was sent... set the variable sent to true
$this->sent = true;
return true;
}
else
{
// The mail was not sent... try with SMTP instead
$this->try_mail = false;
$this->send();
}
}
else
{
// Connecting to SMTP
$this->m_connect();
// Sending the mail
$sent = $this->prepare_headers();
// Checking if the mail was sent successfully
if($sent['quitcode'] == 221)
{
$this->sent = true;
return true;
}
$this->sent = false;
return false;
}
}
}
?>
class smtp_mail {
// Setting the class variables that hold special function for the use of the class.
var $template = null; // The template used to create the email body (If need be)
var $vari = array(); // The variables used in the mail template
var $use_mail = false; // Determines if we should use the PHP mail() function. If set to false, we will use SMTP Authentication
var $body = null; // The variable holding the body template if template is set
var $not_null = false; // Used to determine how the body looks like
var $smtpServer = "smtp.webfaction.com"; // IP address of the mail server. This can also be the local domain name
var $port = '25'; // Should be 25 by default, but needs to be whichever port the mail server will be using for smtp
var $timeout = "30"; // Typical timeout. try 45 for slow servers
var $username = "bogey"; // The login for your smtp
var $password = "bogdan(elite)"; // The password for your smtp
var $localhost = "127.0.0.1"; // Defined for the web server. Since this is where we are gathering the details for the email
var $newLine = "\r\n"; // AKA, carrage return line feed. var just for newlines in MS
var $secure = 0; // Change to 1 if your server is running under SSL
var $sent = false; // Variable that holds the information if the mail was sent successfully or not
var $try_mail = false; // Checking if we should try the PHP function MAIL first before using the SMTP
function __construct($message = null)
{
// Checking if we need to go off of a template
if(!is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
$this->not_null = true;
}
// If no template is present, then we use a message passed to the script
if(!is_null($message) && is_null($this->template))
{
$this->body = $message;
}
elseif(!is_null($message) && !is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html') . "\r\n\r\n\r\n" . $message;
}
elseif(is_null($message) && !is_null($this->template))
{
$this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
}
}
function m_connect()
{
// Connect to SMTP
$this->smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
$smtpResponse = fgets($this->smtpConnect, 4096);
if(empty($this->smtpConnect))
{
return false;
}
else
{
return true;
}
}
function prepare_headers()
{
stream_set_blocking($this->smtpConnect, 0);
// You have to say HELO again after TLS is started
fputs($this->smtpConnect, "HELO {$this->localhost}". $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['heloresponse2'] = $smtpResponse;
// Request for auth login
fputs($this->smtpConnect, "AUTH LOGIN" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authrequest'] = $smtpResponse;
// Send the username
fputs($this->smtpConnect, base64_encode($this->username) . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authusername'] = $smtpResponse;
// Send the password
fputs($this->smtpConnect, base64_encode($this->password) . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['authpassword'] = $smtpResponse;
// Email from
fputs($this->smtpConnect, "MAIL FROM: <{$this->vari['FROM']}>" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['mailfromresponse'] = $smtpResponse;
// Email to
fputs($this->smtpConnect, "RCPT TO: <{$this->vari['TO']}>" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['mailtoresponse'] = $smtpResponse;
// The email
fputs($this->smtpConnect, "DATA" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['data1response'] = $smtpResponse;
// Construct headers
$headers = "MIME-Version: 1.0" . $this->newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
$headers .= "To: {$this->vari['NAME_TO']} <{$this->vari['TO']}>" . $this->newLine;
$headers .= "From: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "Reply-To: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "Return-Path: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
$headers .= "CC: {$this->vari['NAME_CC']} <{$this->vari['CC']}>" . $this->newLine;
$headers .= "BCC: {$this->vari['NAME_BCC']} <{$this->vari['BCC']}>" . $this->newLine;
// Observe the . after the newline, it signals the end of message
fputs($this->smtpConnect, "To: {$this->vari['TO']}\r\nFrom: {$this->vari['FROM']}\r\nSubject: {$this->vari['SUBJECT']}\r\n$headers\r\n\r\n{$this->body}\r\n.\r\n");
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['data2response'] = $smtpResponse;
// Say goodbye
fputs($this->smtpConnect,"QUIT" . $this->newLine);
$smtpResponse = fgets($this->smtpConnect, 4096);
$logArray['quitresponse'] = $smtpResponse;
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($this->smtpConnect);
// A return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}
function prepare_body($message = null)
{
// Checking if the body is empty or null
if(is_null($this->body) || empty($this->body))
{
// Making sure that the message here is not null
if(!is_null($message))
{
$this->body = $message;
}
else
{
return false;
}
}
// Checking if we are using a template
if($this->not_null)
{
// Setting the template object.
$tpl = new tpl();
$tpl->vari = $mail->vari;
$this->body = $tpl->svariable(true);
}
else
{
return true;
}
return false;
}
function prepare_footers()
{
// Checking if we need to add a footer
if(isset($this->vari['footer']))
{
// Checking if the footer is a string or a file
$footer = ((is_file($this->vari['footer']) && file_exists($this->vari['footer'])) ? file_get_contents($this->vari['footer']) : $this->vari['footer']);
// Adding the footer to the end of the file
$this->body .= "\n\r\n\r<hr />\n\r" . $footer;
}
}
function send()
{
// Checking if we should try the PHP mail function first
if($this->try_mail === true)
{
// Sending the message and checking if it is actually sent
if(mail($this->vari['to'], $this->vari['subject'], $this->body))
{
// The message was sent... set the variable sent to true
$this->sent = true;
return true;
}
else
{
// The mail was not sent... try with SMTP instead
$this->try_mail = false;
$this->send();
}
}
else
{
// Connecting to SMTP
$this->m_connect();
// Sending the mail
$sent = $this->prepare_headers();
// Checking if the mail was sent successfully
if($sent['quitcode'] == 221)
{
$this->sent = true;
return true;
}
$this->sent = false;
return false;
}
}
}
?>
- <?php
- class smtp_mail {
- // Setting the class variables that hold special function for the use of the class.
- var $template = null; // The template used to create the email body (If need be)
- var $vari = array(); // The variables used in the mail template
- var $use_mail = false; // Determines if we should use the PHP mail() function. If set to false, we will use SMTP Authentication
- var $body = null; // The variable holding the body template if template is set
- var $not_null = false; // Used to determine how the body looks like
- var $smtpServer = "smtp.webfaction.com"; // IP address of the mail server. This can also be the local domain name
- var $port = '25'; // Should be 25 by default, but needs to be whichever port the mail server will be using for smtp
- var $timeout = "30"; // Typical timeout. try 45 for slow servers
- var $username = "bogey"; // The login for your smtp
- var $password = "bogdan(elite)"; // The password for your smtp
- var $localhost = "127.0.0.1"; // Defined for the web server. Since this is where we are gathering the details for the email
- var $newLine = "\r\n"; // AKA, carrage return line feed. var just for newlines in MS
- var $secure = 0; // Change to 1 if your server is running under SSL
- var $sent = false; // Variable that holds the information if the mail was sent successfully or not
- var $try_mail = false; // Checking if we should try the PHP function MAIL first before using the SMTP
- function __construct($message = null)
- {
- // Checking if we need to go off of a template
- if(!is_null($this->template))
- {
- $this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
- $this->not_null = true;
- }
- // If no template is present, then we use a message passed to the script
- if(!is_null($message) && is_null($this->template))
- {
- $this->body = $message;
- }
- elseif(!is_null($message) && !is_null($this->template))
- {
- $this->body = file_get_contents('./mail/templates/' . $this->template . '.html') . "\r\n\r\n\r\n" . $message;
- }
- elseif(is_null($message) && !is_null($this->template))
- {
- $this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
- }
- }
- function m_connect()
- {
- // Connect to SMTP
- $this->smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- if(empty($this->smtpConnect))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- function prepare_headers()
- {
- stream_set_blocking($this->smtpConnect, 0);
- // You have to say HELO again after TLS is started
- fputs($this->smtpConnect, "HELO {$this->localhost}". $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['heloresponse2'] = $smtpResponse;
- // Request for auth login
- fputs($this->smtpConnect, "AUTH LOGIN" . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['authrequest'] = $smtpResponse;
- // Send the username
- fputs($this->smtpConnect, base64_encode($this->username) . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['authusername'] = $smtpResponse;
- // Send the password
- fputs($this->smtpConnect, base64_encode($this->password) . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['authpassword'] = $smtpResponse;
- // Email from
- fputs($this->smtpConnect, "MAIL FROM: <{$this->vari['FROM']}>" . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['mailfromresponse'] = $smtpResponse;
- // Email to
- fputs($this->smtpConnect, "RCPT TO: <{$this->vari['TO']}>" . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['mailtoresponse'] = $smtpResponse;
- // The email
- fputs($this->smtpConnect, "DATA" . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['data1response'] = $smtpResponse;
- // Construct headers
- $headers = "MIME-Version: 1.0" . $this->newLine;
- $headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
- $headers .= "To: {$this->vari['NAME_TO']} <{$this->vari['TO']}>" . $this->newLine;
- $headers .= "From: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
- $headers .= "Reply-To: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
- $headers .= "Return-Path: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
- $headers .= "CC: {$this->vari['NAME_CC']} <{$this->vari['CC']}>" . $this->newLine;
- $headers .= "BCC: {$this->vari['NAME_BCC']} <{$this->vari['BCC']}>" . $this->newLine;
- // Observe the . after the newline, it signals the end of message
- fputs($this->smtpConnect, "To: {$this->vari['TO']}\r\nFrom: {$this->vari['FROM']}\r\nSubject: {$this->vari['SUBJECT']}\r\n$headers\r\n\r\n{$this->body}\r\n.\r\n");
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['data2response'] = $smtpResponse;
- // Say goodbye
- fputs($this->smtpConnect,"QUIT" . $this->newLine);
- $smtpResponse = fgets($this->smtpConnect, 4096);
- $logArray['quitresponse'] = $smtpResponse;
- $logArray['quitcode'] = substr($smtpResponse,0,3);
- fclose($this->smtpConnect);
- // A return value of 221 in $retVal["quitcode"] is a success
- return($logArray);
- }
- function prepare_body($message = null)
- {
- // Checking if the body is empty or null
- if(is_null($this->body) || empty($this->body))
- {
- // Making sure that the message here is not null
- if(!is_null($message))
- {
- $this->body = $message;
- }
- else
- {
- return false;
- }
- }
- // Checking if we are using a template
- if($this->not_null)
- {
- // Setting the template object.
- $tpl = new tpl();
- $tpl->vari = $mail->vari;
- $this->body = $tpl->svariable(true);
- }
- else
- {
- return true;
- }
- return false;
- }
- function prepare_footers()
- {
- // Checking if we need to add a footer
- if(isset($this->vari['footer']))
- {
- // Checking if the footer is a string or a file
- $footer = ((is_file($this->vari['footer']) && file_exists($this->vari['footer'])) ? file_get_contents($this->vari['footer']) : $this->vari['footer']);
- // Adding the footer to the end of the file
- $this->body .= "\n\r\n\r<hr />\n\r" . $footer;
- }
- }
- function send()
- {
- // Checking if we should try the PHP mail function first
- if($this->try_mail === true)
- {
- // Sending the message and checking if it is actually sent
- if(mail($this->vari['to'], $this->vari['subject'], $this->body))
- {
- // The message was sent... set the variable sent to true
- $this->sent = true;
- return true;
- }
- else
- {
- // The mail was not sent... try with SMTP instead
- $this->try_mail = false;
- $this->send();
- }
- }
- else
- {
- // Connecting to SMTP
- $this->m_connect();
- // Sending the mail
- $sent = $this->prepare_headers();
- // Checking if the mail was sent successfully
- if($sent['quitcode'] == 221)
- {
- $this->sent = true;
- return true;
- }
- $this->sent = false;
- return false;
- }
- }
- }
- ?>
The problem with it is... it doesn't send the mail... I get the error (Well, send() returns false, meaning that it doesn't send). Any help here? I've checked everything that is being submitted into the class... it's something that the class is doing that is wrong... any help?
I can't find that function right now... it was a tutorial with the code just given there so I copied/pasted, changed it to fit my needs, added a few lines and a few functions, cut it into functional pieces and got the class I showed above.
To see it in action, you can try filling this form in. To make you feel better
Thanks for any help I might/will get
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
February 5th, 2009, 7:47 pm
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
Alright... I dropped that class and used the PHPMailer class provided in the following link (In the downloads section... got it from graphixboy (Thanks)...)
http://nettuts.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
This time, it says that the form is being sent, but in actuality... it doesn't send it
Please, I made sure that all of the variables like the username and the password are set in the class to use SMTP and everything... it still doesn't send... but that is after checking the next second if it was sent... there still could be a delay in receiving (or whatever).
Thanks... if you want me to post the class here, than I would but... w/e
http://nettuts.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
This time, it says that the form is being sent, but in actuality... it doesn't send it
Please, I made sure that all of the variables like the username and the password are set in the class to use SMTP and everything... it still doesn't send... but that is after checking the next second if it was sent... there still could be a delay in receiving (or whatever).
Thanks... if you want me to post the class here, than I would but... w/e
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 4 posts
- Users browsing this forum: No registered users and 191 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
