简体   繁体   中英

I am unable to send email in codeigniter using Local Server. Help to resolve this issue

Here is my Main function

The user register successfully but don't receive any email.

public function register(){
        $this->load->view("Home/home_header");
        if(isset($_POST["user_register"])){
             $this->form_validation->set_rules('name', 'First Name', 'alpha|htmlspecialchars|trim|required');
             $this->form_validation->set_rules('username', 'Last Name', 'htmlspecialchars|trim|required');
             $this->form_validation->set_rules('email', 'Email', 'valid_email|trim|required');
             $this->form_validation->set_rules('confirm-mail', 'Confirm Email', 'valid_email|trim|required|matches[email]');
             $this->form_validation->set_rules('password', 'Password', 'required');
             $this->form_validation->set_rules('confirm-password', 'Confirm Password', 'required|matches[password]');
             $this->form_validation->set_rules('address', 'Address', 'htmlspecialchars|trim|required');
             $this->form_validation->set_rules('country', 'Country', 'required');
             $this->form_validation->set_rules('male-female', 'Gender', 'required');
             if($this->form_validation->run() == TRUE){
                  $status     = 0;
                 $data = array();
                  $data["first-name"] = $_POST["name"];
                  $data["username"] = $_POST["username"];
                  $data["mail"] = $_POST["email"];
                  $data["confirm-mail"] = $_POST["confirm-mail"];
                  $data["password"] = hash('md5',$_POST["password"]);
                  $data["confirm-password"] = hash('md5',$_POST["confirm-password"]);
                  $data["address"] = $_POST["address"];
                  $data["country"] = $_POST["country"];
                  $data["male-female"] = $_POST["male-female"];

                  $data["status"] = $status;

                  $email = $_POST["email"];
                  $saltid = md5($email);


                  if($this->db->insert("register",$data)){
                      if( $this->User_functions->sendmail($email,$saltid)){

                          //echo 'Succesful';
                        redirect(base_url().login);
                        }else{
                            echo "Sorry !";
                        }
                  }
             }
        }
        $this->load->view("Home/user_registration");
        $this->load->view("Home/home_footer");
    }

And Here's the mail function. I am getting user email from the input and store the user record to the database. Record stores successfully and page redirects to the login page. But the user don't get email on registration. Help me to resolve this issue.

function sendmail($email,$saltid){
    // configure the email setting
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'ssl://smtp.gmail.com'; //smtp host name
        $config['smtp_port'] = '465'; //smtp port number
        $config['smtp_user'] = '*******@gmail.com';
        $config['smtp_pass'] = '***********'; //$from_email password
        $config['mailtype'] = 'html';
        //$config['charset'] = 'iso-8859-1';
        //$config['wordwrap'] = TRUE;
        $config['newline'] = "\r\n"; //use double quotes
        //$this->email->initialize($config);
        $this->load->library('email', $config);
        $url = base_url()."user/confirmation/".$saltid;
        $this->email->from('gulfpro354@gmail.com', 'GulfPro');
        $this->email->to($email); 
        $this->email->subject('Please Verify Your Email Address');
        $message = "<html><head><head></head><body><p>Hi,</p><p>Thanks for registration with DGaps.</p>
        <p>Please click below link to verify your email.</p>".$url."<br/><p>Sincerely,</p><p>DGaps Team</p></body></html>";
        $this->email->message($message);
        return $this->email->send();
   }

If I used any wrong code for this, also highlight that code.

Thanks Ammar Khaliq

Please use :

echo $this->email->print_debugger();

after $this->email->send(); by remove return

It will show you the reason why you are not able to send an email

I have used this method of email.

public function sendmail($user_id, $email){
    require("plugins/mailer/PHPMailerAutoload.php");
    $mail = new PHPMailer;
    $mail->SMTPOptions = array(
      'ssl' => array(
      'verify_peer' => false,
      'verify_peer_name' => false,
      'allow_self_signed' => true
     )
    );
    //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'example@gmail.com';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to
    //Recipients
    $from = "example@gmail.com";
    $from_label = "ABC";
    $subject = "Please verify your email address.";
    $url = base_url()."user/".$user_id."/".md5($user_id);
    $message = "<p>Hi,</p><p>Thanks for registration with Portfolio Times.</p>
        <p>Please click below link to verify your email.</p>".$url."<br/><p>Sincerely,</p><p>DGaps Team</p>";

    $mail->setFrom($from,$from_label);
    $mail->addAddress($email);  
    $mail->isHTML(true);                                  
    $mail->Subject = $subject;
    $mail->Body    = $message;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    //$mail->SMTPDebug = 2;
    if( !$mail->send()){
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     } else {
        echo "<div class='alert alert-success pull-right' style='width:400px' align='center'>".'<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>'."Email Sent Successfully...</div>";
       //return true;
     }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM