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


79
<?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是错误的,索引键是有效的,所以使用$config['validate'] = TRUE
Delmo

Answers:


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


10
+1,请注意所有Array元素都用单引号引起来,而只有的参数才$this->email->set_newline用双引号引起来。这非常重要,否则将无法正常工作。
纳文·库马尔

11
这是因为在PHP(和其他一些语言)中,\n如果用双引号引起来,则仅表示换行。单引号表示文字字符\ n。
DMin

1
解决方法是:thnx:对于$ this-> email-> set_newline(“ \ r \ n”)
TooCooL 2015年

1
@NaveenKumar仍然无法发送电子邮件。没有得到任何回应$result = $this->email->send();
Pathik Vejani

1
@ThorpeObazee先生,我们可以在邮件配置文件中添加更多配置吗?例如,$ config ['subjectEnquiry'] =>“查询电子​​邮件”; 或$ config ['subjectSuccess'] =>“已成功添加数据”;
ankit suthar

19

您需要在PHP配置中启用SSL。加载php.ini并找到包含以下内容的行:

;extension=php_openssl.dll

取消注释。:D

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

extension=php_openssl.dll


1
这仅适用于Windows Server。使用phpinfo()查看是否有合适的套接字可用,以及服务器上是否装有openssl。
jjwdesign 2011年

17

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

如果您不想使用上述方法设置首选项,则可以将其放入配置文件中。只需创建一个名为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

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

通过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());
    }

1

我在使用Postfix的linux服务器中使用的另一个选项是:

首先,配置CI电子邮件使用你的服务器的电子邮件系统:例如在email.php,例如

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

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

如果您已经配置了Linux盒子,可以将它的部分/全部外发电子邮件发送给Google,则这样会更简单(且碎片更少)。


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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.