使用codeigniter电子邮件库发送带有gmail smtp的电子邮件

<?php class Email extends Controller { function Email() { parent::Controller(); $this->load->library('email'); } function index() { $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.gmail.com'; $config['smtp_port'] = '465'; $config['smtp_timeout'] = '7'; $config['smtp_user'] = 'mygmail@gmail.com'; $config['smtp_pass'] = '*******'; $config['charset'] = 'utf-8'; $config['newline'] = "\r\n"; $config['mailtype'] = 'text'; // or html $config['validation'] = TRUE; // bool whether to validate email or not $this->email->initialize($config); $this->email->from('mygmail@gmail.com', 'myname'); $this->email->to('target@gmail.com'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); $this->email->send(); echo $this->email->print_debugger(); $this->load->view('email_view'); } } 

我得到这个错误:

 A PHP Error was encountered Severity: Warning Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Connection timed out) Filename: libraries/Email.php Line Number: 1641 

使用PORT 25/587

我得到这个错误:

 A PHP Error was encountered Severity: Warning Message: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:func(119):reason(252) Filename: libraries/Email.php Line Number: 1641 

我不想现在使用phpmailer。 (其实我试图使用phpmailer,但我失败了)。

我该如何解决这个问题?

 $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.googlemail.com', 'smtp_port' => 465, 'smtp_user' => 'xxx', 'smtp_pass' => 'xxx', 'mailtype' => 'html', 'charset' => 'iso-8859-1' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); // Set to, from, message, etc. $result = $this->email->send(); 

来自CodeIgniter论坛

您需要在您的PHPconfiguration中启用SSL。 加载php.ini并find以下内容:

;extension=php_openssl.dll

取消注释。 :d

(通过从语句中删除分号)

extension=php_openssl.dll

根据CI文档( CodeIgniter电子邮件库 )…

如果您不想使用上述方法设置首选项,则可以将其放入configuration文件中。 只需创build一个名为email.php的新文件,在该文件中添加$ config数组。 然后将该文件保存在config / email.php中,并自动使用。 如果将首选项保存在configuration文件中,则不需要使用$ this-> email-> initialize()函数。

我可以通过把所有的设置到application / config / email.php中来实现这个function

 $config['useragent'] = 'CodeIgniter'; $config['protocol'] = 'smtp'; //$config['mailpath'] = '/usr/sbin/sendmail'; $config['smtp_host'] = 'ssl://smtp.googlemail.com'; $config['smtp_user'] = 'YOUREMAILHERE@gmail.com'; $config['smtp_pass'] = 'YOURPASSWORDHERE'; $config['smtp_port'] = 465; $config['smtp_timeout'] = 5; $config['wordwrap'] = TRUE; $config['wrapchars'] = 76; $config['mailtype'] = 'html'; $config['charset'] = 'utf-8'; $config['validate'] = FALSE; $config['priority'] = 3; $config['crlf'] = "\r\n"; $config['newline'] = "\r\n"; $config['bcc_batch_mode'] = FALSE; $config['bcc_batch_size'] = 200; 

然后,在其中一个控制器方法中,我有这样的东西:

 $this->load->library('email'); // Note: no $config param needed $this->email->from('YOUREMAILHERE@gmail.com', 'YOUREMAILHERE@gmail.com'); $this->email->to('SOMEEMAILHERE@gmail.com'); $this->email->subject('Test email from CI and Gmail'); $this->email->message('This is a test.'); $this->email->send(); 

另外,正如Cerebro所写的,我不得不在我的php.ini文件中取消注释,然后重新启动PHP:

 extension=php_openssl.dll 

将其更改为以下内容:

 $ci = get_instance(); $ci->load->library('email'); $config['protocol'] = "smtp"; $config['smtp_host'] = "ssl://smtp.gmail.com"; $config['smtp_port'] = "465"; $config['smtp_user'] = "blablabla@gmail.com"; $config['smtp_pass'] = "yourpassword"; $config['charset'] = "utf-8"; $config['mailtype'] = "html"; $config['newline'] = "\r\n"; $ci->email->initialize($config); $ci->email->from('blablabla@gmail.com', 'Blabla'); $list = array('xxx@gmail.com'); $ci->email->to($list); $this->email->reply_to('my-email@gmail.com', 'Explendid Videos'); $ci->email->subject('This is an email test'); $ci->email->message('It is working. Great!'); $ci->email->send(); 

通过codeiginater发送html邮件

  $this->load->library('email'); $this->load->library('parser'); $this->email->clear(); $config['mailtype'] = "html"; $this->email->initialize($config); $this->email->set_newline("\r\n"); $this->email->from('email@example.com', 'Website'); $list = array('xxxxxxxx@archmage.lk', 'xxxxx@gmail.com'); $this->email->to($list); $data = array(); $htmlMessage = $this->parser->parse('messages/email', $data, true); $this->email->subject('This is an email test'); $this->email->message($htmlMessage); if ($this->email->send()) { echo 'Your email was sent, thanks chamil.'; } else { show_error($this->email->print_debugger()); } 

也许你的托pipe服务器和电子邮件服务器位于相同的地方,你不需要去smtpauthentication。 只要保持每件事情的默认像:

 $config = array( 'protocol' => '', 'smtp_host' => '', 'smtp_port' => '', 'smtp_user' => 'yourname@gmail.com', 'smtp_pass' => '**********' ); 

要么

 $config['protocol'] = ''; $config['smtp_host'] = ''; $config['smtp_port'] = ; $config['smtp_user'] = 'yourname@gmail.com'; $config['smtp_pass'] = 'password'; 

它适用于我。

它可以是这样的:

如果您正在使用cPanel为您的网站smtp限制是问题并导致此错误。 SMTP限制

使用CodeIgniter发送邮件时出错

另一个选项我有工作,在与Postfix的Linux服务器:

首先,configurationCI电子邮件以使用您的服务器的电子邮件系统:例如,在email.php

 # alias to postfix in a typical Postfix server $config['protocol'] = 'sendmail'; $config['mailpath'] = '/usr/sbin/sendmail'; 

然后configuration你的后缀来中继邮件到谷歌(也许取决于发件人的地址)。 您可能需要将用户密码设置放在/etc/postfix/sasl_passwd (docs)

如果你有一个已经configuration好的发送一些/所有外发电子邮件给Google的Linux机器,这个要简单得多(而且不要太简单)。