【问题标题】:SMTP PHP not sending mailSMTP PHP不发送邮件
【发布时间】:2019-08-13 13:50:12
【问题描述】:

我正在使用 SMTP 不工作的开源。我可以确定我的 Gmail smtp 信息是正确的(我检查过,它适用于其他应用程序)
我的设置:

SMTP host: smtp.gmail.com   
SMTP port: 587   
SMTP username/password: <my account detail>   

不太安全的应用也打开了

代码如下:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Email_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function password_reset_email($new_password = '' , $email = '')
    {
        $query = $this->db->get_where('users' , array('email' => $email));
        if($query->num_rows() > 0)
        {

            $email_msg  =   "Your password has been changed.";
            $email_msg  .=  "Your new password is : ".$new_password."<br />";

            $email_sub  =   "Password reset request";
            $email_to   =   $email;
            //$this->do_email($email_msg , $email_sub , $email_to);
            $this->send_smtp_mail($email_msg , $email_sub , $email_to);
            return true;
        }
        else
        {
            return false;
        }
    }

    public function send_email_verification_mail($to = "", $verification_code = "") {
        $redirect_url = site_url('login/verify_email_address/'.$verification_code);
        $subject        = "Verify Email Address";
        $email_msg  =   "<b>Hello,</b>";
        $email_msg  .=  "<p>Please click the link below to verify your email address.</p>";
        $email_msg  .=  "<a href = ".$redirect_url." target = '_blank'>Verify Your Email Address</a>";
        $this->send_smtp_mail($email_msg, $subject, $to);
    }

    public function send_mail_on_course_status_changing($course_id = "", $mail_subject = "", $mail_body = "") {
        $instructor_id       = 0;
        $course_details    = $this->crud_model->get_course_by_id($course_id)->row_array();
        if ($course_details['user_id'] != "") {
            $instructor_id = $course_details['user_id'];
        }else {
            $instructor_id = $this->session->userdata('user_id');
        }
        $instuctor_details = $this->user_model->get_all_user($instructor_id)->row_array();
        $email_from = get_settings('system_email');

        $this->send_smtp_mail($mail_body, $mail_subject, $instuctor_details['email'], $email_from);
    }

    public function send_smtp_mail($msg=NULL, $sub=NULL, $to=NULL, $from=NULL) {
        //Load email library
        $this->load->library('email');

        if($from == NULL)
            $from       =   $this->db->get_where('settings' , array('key' => 'system_email'))->row()->value;

        //SMTP & mail configuration
        $config = array(
            'protocol'  => get_settings('protocol'),
            'smtp_host' => get_settings('smtp_host'),
            'smtp_port' => get_settings('smtp_port'),
            'smtp_user' => get_settings('smtp_user'),
            'smtp_pass' => get_settings('smtp_pass'),
            'mailtype'  => 'html',
            'charset'   => 'utf-8'
        );
        $this->email->initialize($config);
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");

        $htmlContent = $msg;

        $this->email->to($to);
        $this->email->from($from, get_settings('system_name'));
        $this->email->subject($sub);
        $this->email->message($htmlContent);

        //Send email
        $this->email->send();
    }
}

这里的问题是它不起作用,我不知道为什么。对不起,因为我是 PHP 的菜鸟

【问题讨论】:

  • 如果您在特定框架中使用特定邮件库,您可能还想添加哪个。如果您告诉我们您正在使用哪些工具,则更容易找到帮助。这看起来像 CodeIgniter?
  • 是的,我不知道为什么它不发送电子邮件
  • 您可以使用 CI 的电子邮件库中的 print_debugger() 方法,看看服务器是否为您提供了任何有用的信息。
  • 您是否在发送帐户的 gmail 设置中启用了“使用安全性较低的应用程序”(原文如此)?
  • “不太安全的应用也打开了”我说

标签: php email smtp


【解决方案1】:

通过将端口改为465,并将smtp主机添加到ssl://,就成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多