不能通过codeigniter发送电子邮件

时间:2018-05-02 08:00:36

标签: php codeigniter email backend mail-server

这是我调用发送电子邮件的功能,它既不发送到肝服务器也不发送到本地服务器。有人可以帮忙吗?

//this function sends a confirmation email to the user while registration
public function send_email($email,$message,$subject) {
     // configuration
    $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'example@gmail.com', 
      'smtp_pass' => 'password', 
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('example@gmail.com');
    $this->email->to($email);
    $this->email->subject($subject);
    $this->email->message($message);
    $result = $this->email->send();
    $this->email->clear();
    return ($result);
}

2 个答案:

答案 0 :(得分:1)

你正在使用gmail smtp吗?我以前面临同样的问题。然后我在我的谷歌帐户上激活允许不太安全的应用程序

enter image description here

然后它起作用

答案 1 :(得分:0)

尝试以下设置:

// The mail sending protocol.
$config['protocol'] = 'smtp';
// SMTP Server Address for Gmail.
$config['smtp_host'] = ssl://smtp.googlemail.com
// SMTP Port - the port that you is required
$config['smtp_port'] = 465;
// SMTP Username like. (abc@gmail.com)
$config['smtp_user'] = $sender_email;
// SMTP Password like (abc***##)
$config['smtp_pass'] = $user_password;
// Load email library and passing configured values to email library
$this->load->library('email', $config);
// Sender email address
$this->email->from($sender_email, $username);
// Receiver email address.for single email
$this->email->to($receiver_email);
//send multiple email
$this->email->to(abc@gmail.com,xyz@gmail.com,jkl@gmail.com);
// Subject of email
$this->email->subject($subject);
// Message in email
$this->email->message($message);
// It returns boolean TRUE or FALSE based on success or failure
$this->email->send();