SMTP Mail problem

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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:
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;
        }
    }
}
?>
  1. <?php
  2. class smtp_mail {
  3.  
  4.     // Setting the class variables that hold special function for the use of the class.
  5.     var $template = null;                       // The template used to create the email body (If need be)
  6.     var $vari = array();                        // The variables used in the mail template
  7.     var $use_mail = false;                      // Determines if we should use the PHP mail() function. If set to false, we will use SMTP Authentication
  8.     var $body = null;                           // The variable holding the body template if template is set
  9.     var $not_null = false;                      // Used to determine how the body looks like
  10.     var $smtpServer = "smtp.webfaction.com";    // IP address of the mail server.  This can also be the local domain name
  11.     var $port = '25';                           // Should be 25 by default, but needs to be whichever port the mail server will be using for smtp
  12.     var $timeout = "30";                        // Typical timeout. try 45 for slow servers
  13.     var $username = "bogey";                    // The login for your smtp
  14.     var $password = "bogdan(elite)";            // The password for your smtp
  15.     var $localhost = "127.0.0.1";               // Defined for the web server.  Since this is where we are gathering the details for the email
  16.     var $newLine = "\r\n";                      // AKA, carrage return line feed. var just for newlines in MS
  17.     var $secure = 0;                            // Change to 1 if your server is running under SSL
  18.     var $sent = false;                          // Variable that holds the information if the mail was sent successfully or not
  19.     var $try_mail = false;                      // Checking if we should try the PHP function MAIL first before using the SMTP
  20.    
  21.     function __construct($message = null)
  22.     {
  23.         // Checking if we need to go off of a template
  24.         if(!is_null($this->template))
  25.         {
  26.             $this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
  27.             $this->not_null = true;
  28.         }
  29.        
  30.         // If no template is present, then we use a message passed to the script
  31.         if(!is_null($message) && is_null($this->template))
  32.         {
  33.             $this->body = $message;
  34.         }
  35.         elseif(!is_null($message) && !is_null($this->template))
  36.         {
  37.             $this->body = file_get_contents('./mail/templates/' . $this->template . '.html') . "\r\n\r\n\r\n" . $message;
  38.         }
  39.         elseif(is_null($message) && !is_null($this->template))
  40.         {
  41.             $this->body = file_get_contents('./mail/templates/' . $this->template . '.html');
  42.         }
  43.     }
  44.    
  45.     function m_connect()
  46.     {
  47.         // Connect to SMTP
  48.         $this->smtpConnect = fsockopen($this->smtpServer, $this->port, $errno, $errstr, $this->timeout);
  49.         $smtpResponse = fgets($this->smtpConnect, 4096);
  50.         if(empty($this->smtpConnect))
  51.         {
  52.            return false;
  53.         }
  54.         else
  55.         {
  56.            return true;
  57.         }
  58.     }
  59.    
  60.     function prepare_headers()
  61.     {
  62.         stream_set_blocking($this->smtpConnect, 0);
  63.    
  64.         // You have to say HELO again after TLS is started
  65.         fputs($this->smtpConnect, "HELO {$this->localhost}". $this->newLine);
  66.         $smtpResponse = fgets($this->smtpConnect, 4096);
  67.         $logArray['heloresponse2'] = $smtpResponse;
  68.  
  69.         // Request for auth login
  70.         fputs($this->smtpConnect, "AUTH LOGIN" . $this->newLine);
  71.         $smtpResponse = fgets($this->smtpConnect, 4096);
  72.         $logArray['authrequest'] = $smtpResponse;
  73.  
  74.         // Send the username
  75.         fputs($this->smtpConnect, base64_encode($this->username) . $this->newLine);
  76.         $smtpResponse = fgets($this->smtpConnect, 4096);
  77.         $logArray['authusername'] = $smtpResponse;
  78.  
  79.         // Send the password
  80.         fputs($this->smtpConnect, base64_encode($this->password) . $this->newLine);
  81.         $smtpResponse = fgets($this->smtpConnect, 4096);
  82.         $logArray['authpassword'] = $smtpResponse;
  83.  
  84.         // Email from
  85.         fputs($this->smtpConnect, "MAIL FROM: <{$this->vari['FROM']}>" . $this->newLine);
  86.         $smtpResponse = fgets($this->smtpConnect, 4096);
  87.         $logArray['mailfromresponse'] = $smtpResponse;
  88.  
  89.         // Email to
  90.         fputs($this->smtpConnect, "RCPT TO: <{$this->vari['TO']}>" . $this->newLine);
  91.         $smtpResponse = fgets($this->smtpConnect, 4096);
  92.         $logArray['mailtoresponse'] = $smtpResponse;
  93.  
  94.         // The email
  95.         fputs($this->smtpConnect, "DATA" . $this->newLine);
  96.         $smtpResponse = fgets($this->smtpConnect, 4096);
  97.         $logArray['data1response'] = $smtpResponse;
  98.  
  99.         // Construct headers
  100.         $headers = "MIME-Version: 1.0" . $this->newLine;
  101.         $headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
  102.         $headers .= "To: {$this->vari['NAME_TO']} <{$this->vari['TO']}>" . $this->newLine;
  103.         $headers .= "From: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
  104.         $headers .= "Reply-To: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
  105.         $headers .= "Return-Path: {$this->vari['NAME_FROM']} <{$this->vari['FROM']}>" . $this->newLine;
  106.         $headers .= "CC: {$this->vari['NAME_CC']} <{$this->vari['CC']}>" . $this->newLine;
  107.         $headers .= "BCC: {$this->vari['NAME_BCC']} <{$this->vari['BCC']}>" . $this->newLine;
  108.  
  109.         // Observe the . after the newline, it signals the end of message
  110.         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");
  111.         $smtpResponse = fgets($this->smtpConnect, 4096);
  112.         $logArray['data2response'] = $smtpResponse;
  113.  
  114.         // Say goodbye
  115.         fputs($this->smtpConnect,"QUIT" . $this->newLine);
  116.         $smtpResponse = fgets($this->smtpConnect, 4096);
  117.         $logArray['quitresponse'] = $smtpResponse;
  118.         $logArray['quitcode'] = substr($smtpResponse,0,3);
  119.         fclose($this->smtpConnect);
  120.        
  121.         // A return value of 221 in $retVal["quitcode"] is a success
  122.         return($logArray);
  123.     }
  124.    
  125.     function prepare_body($message = null)
  126.     {
  127.         // Checking if the body is empty or null
  128.         if(is_null($this->body) || empty($this->body))
  129.         {
  130.             // Making sure that the message here is not null
  131.             if(!is_null($message))
  132.             {
  133.                 $this->body = $message;
  134.             }
  135.             else
  136.             {
  137.                 return false;
  138.             }
  139.         }
  140.        
  141.         // Checking if we are using a template
  142.         if($this->not_null)
  143.         {
  144.             // Setting the template object.
  145.             $tpl = new tpl();
  146.             $tpl->vari = $mail->vari;
  147.            
  148.             $this->body = $tpl->svariable(true);
  149.         }
  150.         else
  151.         {
  152.             return true;
  153.         }
  154.         return false;
  155.     }
  156.    
  157.     function prepare_footers()
  158.     {
  159.         // Checking if we need to add a footer
  160.         if(isset($this->vari['footer']))
  161.         {
  162.             // Checking if the footer is a string or a file
  163.             $footer = ((is_file($this->vari['footer']) && file_exists($this->vari['footer'])) ? file_get_contents($this->vari['footer']) : $this->vari['footer']);
  164.            
  165.             // Adding the footer to the end of the file
  166.             $this->body .= "\n\r\n\r<hr />\n\r" . $footer;
  167.         }
  168.     }
  169.    
  170.     function send()
  171.     {
  172.         // Checking if we should try the PHP mail function first
  173.         if($this->try_mail === true)
  174.         {
  175.             // Sending the message and checking if it is actually sent
  176.             if(mail($this->vari['to'], $this->vari['subject'], $this->body))
  177.             {
  178.                 // The message was sent... set the variable sent to true
  179.                 $this->sent = true;
  180.                 return true;
  181.             }
  182.             else
  183.             {
  184.                 // The mail was not sent... try with SMTP instead
  185.                 $this->try_mail = false;
  186.                 $this->send();
  187.             }
  188.         }
  189.         else
  190.         {
  191.             // Connecting to SMTP
  192.             $this->m_connect();
  193.            
  194.             // Sending the mail
  195.             $sent = $this->prepare_headers();
  196.            
  197.             // Checking if the mail was sent successfully
  198.             if($sent['quitcode'] == 221)
  199.             {
  200.                 $this->sent = true;
  201.                 return true;
  202.             }
  203.             $this->sent = false;
  204.             return false;
  205.         }
  206.     }
  207. }
  208. ?>

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 :lol:

Thanks for any help I might/will get :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Nothing? Darn... another project at a stand-still... I have no clue where to start and what to do... I'm lost here... please...
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

This is no longer a problem... got it fixed :D :D :D :D :D :D :D :D :D :D :D :D ... etc etc etc...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: demonmaestro and 78 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.