【问题标题】:How to send Gmail email with CodeIgniter如何使用 CodeIgniter 发送 Gmail 电子邮件
【发布时间】:2017-06-12 09:22:34
【问题描述】:

我想使用 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 工作。

谢谢你帮助我。

【问题讨论】:

  • 我认为您不允许在您的 gmail 帐户中使用 SMTP。您需要检查您的 google SMTP 设置。

标签: php codeigniter email


【解决方案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));

   }

【讨论】:

    【解决方案2】:

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

    $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
    

    【讨论】:

    • 'smtp_user'地址和'from'地址应该相同吗?
    • 不知何故是的。请在这里找到这个stackoverflow.com/questions/1332510/…
    • 我在 php.ini 文件中找不到“extension=php_openssl.dll”这一行
    • 好的。然后请下载php的openssl扩展并将其包含在ext目录中,如果php.ini文件在末尾添加extension=php_openssl.dll
    • 怎么下载?
    【解决方案3】:

    在localhost上发送Gmail邮件可以看我的回答here

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

    【讨论】:

    • 您在 linux 机器上使用的是什么本地 Web 服务器?也许你应该在你的 linux 服务器中配置 SMTP
    猜你喜欢
    • 2014-01-24
    • 2012-03-22
    • 2021-03-26
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多