如何使用CodeIgniter发送Gmail电子邮件

时间:2017-06-12 09:22:34

标签: php codeigniter email

我想使用CodeIgniter通过我的Gmail地址发送电子邮件。 我尝试了很多解决方案,但都没有。

我的代码:

  $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxxxx@gmail.com',
        'smtp_pass' => 'yyyyy',
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'wordwrap' => TRUE
    );


    $this->load->library('email', $config);

    $this->email->from('xxxxx@gmail.com', 'admin');
    $this->email->to('zzzzz@gmail.com');
    $this->email->subject('Email test');
    $msg = "Testing email.";

    $this->email->message($msg);

    $this->email->send();

    echo $this->email->print_debugger();

我有以下错误:

Message: fwrite(): send of 5 bytes failed with errno=32 Broken pipe
Filename: libraries/Email.php

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

目前,我正在localhost工作。

感谢您帮助我。

3 个答案:

答案 0 :(得分:1)

在CodeIgniter中,您可以通过创建 email.php 文件来配置电子邮件数据。 我使用这个很长一段时间了。所以它也适合你。

文件内:

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com',
    'smtp_port' => 465,
    'smtp_user' => 'yourGmailId@gmail.com', // change it to yours
    'smtp_pass' => 'your password', // change it to yours
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'from' => 'your_from_email@bla.com',
    'subject' => 'test email'
);

现在在控制器中,你可以这样做:

$this->config->load('email');

    $subject = $this->config->item('subject');
    $from = $this->config->item('from');

    $this->load->library('email');
    $this->email->set_newline("\r\n");

    $this->email->from($from, 'Yards & Acares');
    $data = array(
        'some_data' => $email            
    );

    $this->email->to($email);  // replace it with receiver mail id
    $this->email->subject($subject); // replace it with relevant subject 

    // Here is I have added an email template.
    $body = $this->load->view('admin/propMailer.php', $data, TRUE);

    $this->email->message($body);

    if ($this->email->send()) {
       //Do whatever you want if success
    } else {
       //Do whatever you want if failed 
        log_message('error', 'Unable to Send Email to Customer.' . print_r($this->email->print_debugger(array('headers', 'subject')), TRUE));

   }

答案 1 :(得分:0)

请使用下面提到的解决方案

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

同样在php.ini中,请启用openssl

// Find the line.
;extension=php_openssl.dll

// Remove  ";" from this line to enable
extension=php_openssl.dll

答案 2 :(得分:0)

要在localhost上发送Gmail电子邮件,您可以查看我的回答here

在本地开发上执行此操作以测试codeigniter电子邮件功能。