doufei3561 2019-08-13 13:50
浏览 110

SMTP PHP不发送邮件

i am using an open source which SMTP is not working. I can sure that my Gmail smtp info is correct (I checked and it works on other applications)
My settings:

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

Less secure app also turned ON

Here is the code:

<?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("
");

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

The problem here is it not working, I don't know why. Sorry because I am noob at PHP

  • 写回答

1条回答 默认 最新

  • doubiao7410 2019-08-13 14:50
    关注

    By changing the port to 465 and added smtp host to ssl://, it worked successfully!

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘