简体   繁体   中英

Trouble sending email in CodeIgniter via Gmail SMTP

I am trying to use Gmail SMTP server to send email from my application built with CodeIgniter but had no luck. I used my Gmail account as the user, but I am changing the config here for confidentiality. I used the code below

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'myemail@gmail.com';
$config['smtp_pass'] = 'mypassword';
$config['mailtype'] = 'html';


//load email library
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$message = "Please validate your registration using this link: $link.";
$this->email->to($email);
$this->email->from('myemail@gmail.com', 'John Doe');
$this->email->subject('Validate Your Registration');
$this->email->message($message);
if($this->email->send())
{
    $this->session->sess_destroy();
    redirect(base_url()."thanks/indi/$id");
}
else
{
    show_error($this->email->print_debugger());
}

Here are the 2 error messages that appear everytime:

A PHP Error was encountered

Severity: Warning

Message: mail(): Failed to connect to mailserver at "ssl://smtp.googlemail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1553

An Error Was Encountered

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

From: "John Doe" Return-Path: Reply-To: "myemail@gmail.com" X-Sender: myemail@gmail.com X-Mailer: CodeIgniter X-Priority: 3 (Normal) Message-ID: <52333cd2aa4fb@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit =?utf-8?Q?Validate_Your_Registration?= Please validate your registration using this link: http://mywebsite.com/registration/index.php/validate/32/1afa34a7f984eeabdbb0a7d494132ee5 .

I tried 587 as port, ssl://smtp.googlemail.com as host, and tried different combinations to try if it will work. I tried the usual array constructor to declare the config array as well (eg $config = array('protocol' => 'smtp', ...) / $config = Array('protocol' => 'smtp', ...) ). I even tried putting the port inside single quotes, and added an smtp_timeout in the config.

Also, I set my php.ini according to what this page says: http://adisembiring.wordpress.com/2010/01/17/sending-gmail-email-using-codeigniter/ The extension=php_openssl.dll is uncommented, but I can't find this line: extension=php_smtp.dll which was mentioned in the site above so I just set the [mail function] part to something like this

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = ssl://smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465

What should I do to get this working? Any ideas on where I was lacking or something? Thanks. Any help is appreciated :)

尝试将其添加到$config数组中:

$config['starttls'] = TRUE;

如果您使用两步验证,是否已生成应用程序专用密码?

如下更改$ config数组

$config['smtp_host']  = 'ssl://smtp.googlemail.com';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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