【问题标题】:I am unable to send email in codeigniter using Local Server. Help to resolve this issue我无法使用本地服务器在 codeigniter 中发送电子邮件。帮助解决此问题
【发布时间】:2018-03-17 21:09:46
【问题描述】:

这是我的主要功能

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

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('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();
   }

如果我为此使用了错误代码,请同时突出显示该代码。

谢谢 阿马尔·哈利克

【问题讨论】:

  • 您是否在 gmail 中允许该应用使用该服务?
  • 不要将您的密码公开
  • 谢谢,先生。下次我会小心的。

标签: php codeigniter email gmail gmail-api


【解决方案1】:

请使用:

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

$this-&gt;email-&gt;send(); 之后删除return

它会告诉你无法发送电子邮件的原因

【讨论】:

  • 那么请检查你的代码哪里出错了,因为 $this->email->print_debugger();总是显示一些输出。
  • 我有一个解决方案。谢谢
  • 很高兴听到您找到了解决方案,您能告诉我是什么问题吗?
  • Codeigniter 的默认邮件功能存在问题。我使用了核心 php 邮件功能。
【解决方案2】:

我用过这种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;
     }
   }

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2019-07-28
    • 2018-04-29
    • 2023-03-07
    • 2018-09-08
    相关资源
    最近更新 更多