2011-08-17 109 views
3

我想通过我的Gmail帐户在codeigniter中发送电子邮件。我当前的代码如下所示:使用CodeIgniter通过Gmail发送电子邮件的问题?

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.googlemail.com', 
    'smtp_port' => 465, 
    'smtp_user' => '[email protected]', 
    'smtp_pass' => 'my_gmail_password', 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1' 
); 

$this->load->library('email', $config); 
$this->email->set_newline("\r\n"); 
$this->email->from('[email protected]', 'Me'); 
$this->email->to("[email protected]"); 
$this->email->subject('A test email from CodeIgniter using Gmail'); 
$this->email->message("A test email from CodeIgniter using Gmail"); 

$this->email->send(); 

然而,这给了我以下错误:

Message: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out) 
Filename: libraries/Email.php 

Message: fwrite(): supplied argument is not a valid stream resource 

Message: fgets(): supplied argument is not a valid stream resource 

任何有助于解决这个问题,将不胜感激!谢谢!

注:我可以向你保证,我的Gmail电子邮件&密码是否正确

+0

也许它的东西与你的网络配置,我只是复制该代码逐字和它的工作就好了... ... – jackbot

+0

我记得Gmail和笨类似的问题。不幸的是,我无法解决它。 +1。如果你自己找到问题的解决方案,请发表一个答案。 – J0HN

+0

端口465是否在您的服务器上打开? –

回答

1

的错误意味着您无法连接您输入的SMTP地址。

你应该使用:smtp.gmail.com

检查下面的链接以供参考: http://mail.google.com/support/bin/answer.py?answer=13287

+0

不幸的是,这并没有什么区别,谢谢你的帮助。 – Danny

+0

它会给你完全相同的错误还是新的错误? – TFennis

+0

所以,只是为了澄清,''smtp_host'=>'ssl:// smtp.gmail.com''不起作用? –

0

您可以通过“肖恩·麦库尔”利用“雨燕梅勒”下载一个可爱的小火花包。 查看他的网站的安装说明

这里是我用DRY方式发送电子邮件的配置(需要PHP 5.3.0!)。

配置 | --mailer [点] PHP

$config['swift_email_server'] = 'ssl://smtp.googlemail.com'; 
$config['swift_email_port'] = 465; 
$config['swift_email_username'] = '######'; 
$config['swift_email_password'] = '######'; 

Libarary | --MY_Email

class MY_Email extends CI_Email { 


    public function __construct(){ 
     parent::__construct(); 

    } 
    /** 
    * ========================================================================= 
    * Parse's html email templates - initalised via swift-mailer 
    * 
    * @param mixed $data 
    * @param string $template 
    * @return html 
    * 
    * ========================================================================= 
    */ 
    public function send_mail($data, $template){ 

     //Anonymous functions are available since PHP 5.3.0 
     $callback = function ($matches) use ($data){ 
      return (isset($data[$matches[1]])) 
       ? $data[$matches[1]] 
       : $matches[0]; 
     }; 

     //Finds any matches in brackets [] and replaces them with data passed via callback 
     return preg_replace_callback(
       '/\[(.*?)\]/', 
       $callback, 
       read_file(EMAIL_TEMPLATES . $template)); 

    } 


} 

Email templates location defined in index.php 
define('EMAIL_TEMPLATES', 'emailtemplates'); 

控制器 | --MY_Controller

class MY_Controller extends MX_Controller{ 

     protected $user; 

     protected $swiftMailer = NULL; 


     public function __construct(){ 

      parent::__construct(); 


      $this->user = ($this->session->userdata('uid')) 
         ? Member::find($this->session->userdata('uid')) : array(); 

      $this->setup_profile(); 


      $this->form_validation->set_error_delimiters('<p class="form-error">', '</p>'); 


     } 

     public function setup_profile(){ 
      $sections = array(
      'config' => FALSE, 
      'http_headers' => FALSE, 
      'queries' => FALSE, 
      'memory_usage' => FALSE 
      ); 

      $this->output->set_profiler_sections($sections); 

      if(ENVIRONMENT === 'development'){ 
       $this->output->enable_profiler(TRUE); 
      } 
     } 

    protected function prep_email($data, $template){ 


      $message = 
      Swift_Message::newInstance('Type your heading here...') 
       ->setFrom($data['email_address']) 
       ->setTo(array($this->config->item('primary_email'), $data['email_address'])) 
       ->setBody($this->email->send_mail($data, $template)) 
       ->setContentType('text/html'); 

      return ($this->swiftMailer->send($message)); 


    } 


} 

模块 | --services //简单的测试模块È

公共功能安排(){ $配置=阵列( 阵列( '场'=> '服务', '标签'=> '服务', '规则'=>'修剪|所需| max_lenght [ 128] '), 阵列(' 场 '=>' NUM”, '标签'=> '会话的NUMER', '规则'=> '修剪|需要|数字'), 阵列( '场'=> 'REQUEST_DATE', '标签'=> 'REQUEST_DATE', '规则'=> '修剪|所需'), 阵列( '场'=> '音符', '标签'=> '注释', '规则'= >'trim | max_lenght [500]') );

$this->form_validation->CI =& $this; 
    $this->form_validation->set_rules($config); 

    if($this->form_validation->run($this)){ 

     $service = Service::find($this->input->post('service')); 

     if((int)$this->input->post('num') > 1){ 
      $potential = ($this->input->post('num') * $service->price); 
     }else{ 
      $potential = $this->input->post('num'); 
     } 

     $insert = array(
      'service_id' => $this->input->post('service'), 
      'num' => $this->input->post('num'), 
      'request_date' => $this->input->post('request_date'), 
      'note' => serialize($this->input->post('note')), 
      'potential_income' => $potential, 
      'member_id' => $this->user->id, 
      'status' => 'pending' 
     ); 

     $emaildata = array(
      'service' => $service->slug, 
      'num' => $insert['num'], 
      'request_date' => $insert['request_date'], 
      'note' => $insert['note'], 
      'potential_income' => $potential, 
      'email_address' => $this->user->email_address, 
      'status' => 'pending', 
      'fullname' => $this->user->firstname . '&nbsp;' . $this->user->surname, 
      'message_sent' => date('M d h:iA'), 
      'site_link' => '' 
     ); 

     if(Meeting::create($insert)){ 

      if(parent::prep_email($emaildata, 'request_meeting[dot]html')){ 
       $this->session->set_flashdata('success' ,'Request has been sent! You will recieve an email shortly from Paul'); 
       //redirect('/'); 
      }else{ 
       $this->session->set_flashdata('error', 'Email service seems to be down at the moment'); 
       //redirect('/'); 
      } 

     }else{ 
      $this->session->set_flashdata('error', 'Unable to request meeting at this time'); 
      //redirect('/'); 
     } 


    }else{ 
     $this->meetings_form(); 
    } 

希望这会有所帮助!

相关问题