1

我正在尝试使用 Gmail SMTP 服务器从我使用 CodeIgniter 构建的应用程序发送电子邮件,但没有运气。我使用我的 Gmail 帐户作为用户,但为了保密,我在这里更改了配置。我使用了下面的代码

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

以下是每次出现的 2 条错误消息:

遇到 PHP 错误

严重性:警告

消息:mail():无法在“ssl://smtp.googlemail.com”端口 465 连接到邮件服务器,请验证 php.ini 中的“SMTP”和“smtp_port”设置或使用 ini_set()

文件名:库/Email.php

行号:1553

遇到错误

无法使用 PHP mail() 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。

发件人:“John Doe” 返回路径:回复:“myemail@gmail.com” X-Sender:myemail@gmail.com X-Mailer:CodeIgniter X-Priority:3(正常) Message-ID:<52333cd2aa4fb@ gmail.com> Mime 版本:1.0 内容类型:文本/纯文本;charset=utf-8 Content-Transfer-Encoding: 8bit =?utf-8?Q?Validate_Your_Registration?= 请使用此链接验证您的注册:http: //mywebsite.com/registration/index.php/validate/32/1afa34a7f984eeabdbb0a7d494132ee5 .

我尝试将587作为端口,将ssl://smtp.googlemail.com作为主机,并尝试了不同的组合以尝试是否可行。我也尝试了通常的数组构造函数来声明配置数组(例如 $config = array('protocol' => 'smtp', ...) / $config = Array('protocol' => 'smtp', .. .) )。我什至尝试将端口放在单引号内,并在配置中添加了 smtp_timeout。

另外,我根据这个页面上的内容设置了我的 php.ini:http://adisembiring.wordpress.com/2010/01/17/sending-gmail-email-using-codeigniter/ 未注释extension=php_openssl.dll,但我找不到这一行:extension=php_smtp.dll上面网站中提到过,所以我只是将[mail function]部分设置为这样的

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

我应该怎么做才能让它工作?关于我缺乏的地方或其他什么的任何想法?谢谢。任何帮助表示赞赏:)

4

3 回答 3

0

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

于 2013-09-13T20:09:33.173 回答
0

尝试将此添加到您的$config数组中:

$config['starttls'] = TRUE;
于 2013-09-13T17:40:58.040 回答
0

更改您的 $config 数组如下

$config['smtp_host']  = 'ssl://smtp.googlemail.com';
于 2013-11-23T15:30:19.247 回答