2017-10-06 11 views
0

这是我的主要功能我无法使用本地服务器在codeigniter中发送电子邮件。帮助解决此问题

用户注册成功,但没有收到任何电子邮件。

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"); 
    } 

而这里的邮件功能。我从输入中获取用户电子邮件并将用户记录存储到数据库。成功记录商店并将页面重定向到登录页面。但用户在注册时不会收到电子邮件。帮我解决这个问题。

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('[email protected]', '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(); 
    } 

如果我为此使用了任何错误的代码,还会突出显示该代码。

感谢 阿马尔·哈利克

+0

您是否已在gmail中授予应用程序使用该服务的权限? –

+2

不要将密码分享给公众 –

+0

谢谢,先生。下次我会小心的。 –

回答

0

我已经使用这种电子邮件的方法。

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 = '[email protected]';     // 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 = "[email protected]"; 
    $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; 
    } 
    } 
0

请使用:

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

$this->email->send();后删除return

它会告诉你为什么你不能够发送电子邮件的原因

+0

它不显示任何信息。 –

+0

然后请检查你的代码破坏的地方,因为$ this-> email-> print_debugger();总是显示一些输出。 –

+1

我有一个解决方案。谢谢 –

相关问题