简体   繁体   English

我无法使用本地服务器在codeigniter中发送电子邮件。 帮助解决此问题

[英]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 感谢Ammar Khaliq

Please use : 请用 :

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

after $this->email->send(); $this->email->send(); by remove return 通过删除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;
     }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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