$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'XXXX@XXXX.com',
'smtp_pass' => 'XXXX',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('xxxxx@xxx.com', 'xxxx');
$this->email->to($email);
$this->email->subject('Hi');
$this->email->message('Hi');
if($this->email->send())
{
echo 'Your email was sent.';
}
else
{
show_error($this->email->print_debugger());
}
但它显示错误消息:fsockopen():无法连接到smtp.gmail.com:465(连接被拒绝).
我在这里看到了解决方案Sending mail with CodeIgniter using SMTP protocol not working,但它无法正常工作,我无法找到php.ini.
解决方法:
我正在使用这种方式,它对我有用:
$config = array(
'protocol' => 'smtps',
'smtp_host' => 'ssl://smtps.googlemail.com',
'smtp_user' => DONOT_EMAIL,
'smtp_pass' => EMAIL_PASSWORD,
'smtp_port' => '465',
'mailtype' => 'html',
'smtp_timeout' => '4',
'newline' => "\r\n"
);
这段代码我用于服务器端.
每次在本地或服务器中使用邮件功能时,请确保.像这样初始化:
$this->email->initialize($config);