使用 CodeIgniter 邮件库发送 Gmail SMTP 邮件

84
<?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['validation'] = TRUE is wrong, the index key is validate so use $config['validate'] = TRUE - Delmo
9个回答

128
$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论坛


12
+1,注意所有的数组元素都要用单引号括起来,只有 $this->email->set_newline 的参数需要用双引号括起来。这非常重要,否则它将无法正常工作。 - Naveen Kumar
12
这是因为在PHP(和其他一些语言)中,只有在双引号中,“\n”才表示换行。而在单引号中,“\n”表示字面上的“\n”字符。 - DMin
1
解决方案是:感谢 $this->email->set_newline("\r\n")。 - TooCooL
1
我找到了一篇文章,描述了如何在CodeIgniter应用程序中使用SMTP发送电子邮件,并使用广为人知且维护良好的电子邮件发送类。https://www.cloudways.com/blog/send-email-codeigniter-smtp/ - Owais Alam
1
我找到了一篇文章,演示了如何在CodeIgniter应用程序中使用SMTP发送电子邮件。https://www.cloudways.com/blog/send-email-codeigniter-smtp/ - Owais Alam
显示剩余6条评论

20
根据 CI 文档 (CodeIgniter Email Library) 的说明...

如果您不想使用上述方法设置偏好参数,您可以将它们放入一个配置文件中。只需创建一个名为 email.php 的新文件,并在该文件中添加 $config 数组。然后将该文件保存在 config/email.php 并自动使用。如果您将您的偏好设置保存在一个配置文件中,则无需使用 $this->email->initialize() 函数。

我通过将所有设置放入 application/config/email.php 中,成功实现了邮件发送。

$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

您能提供邮件库的代码吗?我每次看到“$this->load->library('email')”,但我没有邮件库,我该怎么办?请给我指条路,我是CI和编码新手。谢谢 :) - prakash pokhrel
这至今为止是可用的。 - rahim.nagori

19

您需要在PHP配置中启用SSL。 打开php.ini文件并找到下面的一行:

;extension=php_openssl.dll

取消注释它。(通过删除语句中的分号):D

extension=php_openssl.dll


1
这仅适用于Windows服务器。使用phpinfo()查看您是否拥有适当的套接字并且您的服务器上是否安装了openssl。 - jjwdesign

7

将其更改为以下内容:

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

5
通过CodeIgniter发送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());
    }

1
也许您的主机服务器和电子邮件服务器位于同一地点,您不需要进行SMTP身份验证。只需保持一切默认即可,例如:
$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';

它对我有效。


$config ['crlf'] =“\r\n”; $config ['newline'] =“\r\n”; - BoCyrill

1

我还有另一种可行的方案,可以在Linux服务器上使用Postfix:

首先,配置CI电子邮件以使用您的服务器电子邮件系统:例如,在email.php中进行配置。

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

然后配置您的postfix将邮件中继到Google(可能取决于发件人地址)。您可能需要在/etc/postfix/sasl_passwd中放置用户密码设置。 (文档)

如果您有一个已经配置为将其某些/所有出站电子邮件发送到Google的Linux框,则这要简单得多(且不会分散注意力)。


1

0

这个问题有一个非常简单的答案。唯一有效的解决方案是进行以下配置更改:将端口更改为587并覆盖CodeIgniter默认的“Email.php”文件。

$config = array( 'protocol' => 'smtp', 'smtp_host' => 'smtp.gmail.com', 'smtp_port' => 587, 'smtp_user' => 'admin@cafeadmin.com.au', 'smtp_pass' => '1800@Footscray123!' );$this->load->library('email', $config);$this->email->initialize($config);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接