CodeIgniter Forums

Full Version: problem with sending mail
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a form that a user fills out to register for an account on my site. If the user successfully fills out the form, a mail is sent to the user using the user's email but anytime the form is submitted the mail doesn't go through rather i keep getting this error from my localhost;

Code:
A PHP Error was encountered

Severity: Warning

Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1845

Backtrace:

File: C:\wamp\www\busticket\application\models\model_user.php
Line: 121
Function: send

File: C:\wamp\www\busticket\application\models\model_user.php
Line: 33
Function: send_validation_email

File: C:\wamp\www\busticket\application\controllers\user.php
Line: 43
Function: insert_user

File: C:\wamp\www\busticket\index.php
Line: 315
Function: require_once

please help!!
That's because you do not have a mail server installed on your local system

If your running Windows you can set the php.ini smtp port to your internet providers smtp port

or install this:

Test Mail Server Tool
(05-02-2016, 11:21 AM)InsiteFX Wrote: [ -> ]That's because you do not have a mail server installed on your local system

If your running Windows you can set the php.ini smtp port to your internet providers smtp port

or install this:

Test Mail Server Tool

hii again, i did everything you asked me plus i even integrated phpmailer with codeigniter but the receipient doesn't get the message i send to him/her. i dont get errors or anything of that sort. please help!!
When I was doing a similar thing with Xampp on my office machine it worked fine, on my laptop at home I could not get email to send. I could not even get the 'write to file' to work without a few problems, which in the end I just though 'bol***cks to it', and did the mail testing on the live site. It was a pain that I never solved, despite hours on xampp forums, fiddling with config settings, installing different bits and bobs and generally pulling my hair out.

Why it worked on the office and not the laptop I have to this day still no idea.

Although no help at all, just thought I would share my experience of this :-)

Best wishes,

Paul.
Show us your method for sending the email.
(05-05-2016, 10:22 AM)InsiteFX Wrote: [ -> ]Show us your method for sending the email.

ayt here is it, my whole model coe and controller code;
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_user extends CI_Model {

 private $email_code;
       public function __construct()
       {
             parent::__construct();
             $this->load->database();
             
       }

       public function insert_user()
       {
          $firstname = $this->input->post('firstname');
         $lastname = $this->input->post('lastname');
         $username = $this->input->post('username');
         $email = $this->input->post('email');

           $data = array(
                          'firstname' => $this->input->post('firstname'),
                          'lastname' => $this->input->post('lastname'),
                          'username' => $this->input->post('username'),
                          'email' =>    $this->input->post('email'),
                          'password' => $this->input->post('password'),
                         
                            );

          $this->db->insert('user', $data);
         if ($this->db->affected_rows() === 1) {
                $this->set_session($firstname, $lastname, $username, $email);
                $this->send_validation_email();
                  return $firstname;
         } else {
                  $this->loaad->library('email');
                  $this->email->from('[email protected]', 'Administrator-Busticket');
                  $this->email->to('[email protected]');
                  $this->email->subject('Problem Inserting User into Database');

        if(isset($email)) {
                           $this->email->message('Unable to register and insert user with the email of ' . $email . ' to the databse');
               } else{
                          $this->email->message('Unable to register and insert user into the database');
                     }
          }
             $this->email->send();
             return FALSE;
       }

       public function validate_email($email, $email_code)
       {
         $sql = "SELECT `email`, 'regtime`, `firstname` FROM `user` WHERE `email`  = '" . $email . "' LIMIT 1";
         $result = $this->db->query($sql);
         $row = $result->row();

         if ($result->num_rows() === 1 && $row->firstname) {
           if(md5((string)$row->regtime) === $email_code) {
             $result= $this->activate_account($email);
             if($result === TRUE) {
               return TRUE;
             } else {
                     echo 'There was an error activating your account, please contact the [email protected]';
             }
           }   else {
                     echo 'There was an error validating your accoint, plaese contact [email protected]';
             }
         }

       }

       
       


         

       private function set_session($firstname, $lastname, $username, $email)
       {
         $sql= "SELECT `id`,`regtime` FROM `user` WHERE `email`  = '" . $email . "' LIMIT 1";
         $result = $this->db->query($sql);
         $row = $result->row();

         $newdata = array(  
                           'id' => $row->id,
                           'firstname' => $firstname,
                           'lastname' => $lastname,
                           'username' => $username,
                           'email'    => $email,
                           'logged_in' => FALSE
           );
         $this->email_code = md5((string)$row->regtime);
         $this->session->set_userdata($newdata);

       }

          private function send_validation_email()
         {
          $this->load->library('email');
          $email = $this->session->userdata('email');
          $email_code = $this->email_code;


          $this->email->set_mailtype('html');
          $this->email->from('busticketon[email protected]', 'Busticket');
          $this->email->to($this->session->userdata('email'));
          $this->email->subject('Please activate your account at Busticket.com');

          $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "https://www.w3.org/TR/xhtml1//DTD/xhtml1-strict.dtd"><html>
                      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                      </head><body>';

          $message .= '<p>Dear ' . $this->session->userdata('firstname') . ',</p>';
          $message .= '<p>Thanks for registering at Busticket.com! Please <strong><a href="' . base_url() . 'user/validate_email/' . $email . '/' . $email_code . '">click here</a></strong> to activate your account. After you have activated your account you will be able to log into Busticket.com and perform all your bus transactions.</p>';
          $message .= '<p>Thank you!</p>';
          $message .= '<p>The Busticket.com Team</p>';
          $message .= '</body></html>';

          $this->email->message($message);
          $this->email->send();
          }

          private function activate_account($email)
          {
            $sql = "UPDATE `user` SET `status` = 1 WHERE `email`= '" . $email . "' LIMIT 1";
            $this->db->query($sql);

            if($this->db->affected_rows() === 1) {
                return TRUE;
            } else {
                 echo 'Error activating your account in the database. Kindly contact [email protected]';
                 return FALSE;
            }
          }

       }  
controller code;

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {



            public function __construct()
       {
               parent::__construct();
               $this->load->model('model_user');
       
       }

     

    public function register_user()
    {
        $this->load->library('form_validation');
        //$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><span class="sr-only">Error:</span>', '</div>');
         $data['title'] = 'Register';



        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|min_length[3]|max_length[15]');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|min_length[3]|max_length[15]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]|max_length[15]|is_unique[user.username]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|max_length[30]|valid_email|is_unique[user.email]');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[20]|matches[password_conf]');
        $this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|min_length[6]|max_length[20]');

        if ( $this->form_validation->run() === FALSE) {

            $this->load->view('templates/header', $data);
            // $this->load->view('templates/navigation');
            $this->load->view('frontend/view_register');
            $this->load->view('templates/footer');

        }
        else
        {
            
             $this->model_user->insert_user();
            
            //echo 'good';
            

            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('frontend/view_register_success');
           
            }

        }

      public function validate_email($email, $email_code)
      {
        $email_code = trim($email_code);

        $validated = $this->model_user->validate_email($email, $email_code);

        if(validated === TRUE) {
             $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('frontend/view_email_validated');
        } else {
                  echo 'Error giving email activated confirmation, please contact [email protected]';
        }


      }
        
    }