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

时间:2009-10-12 15:11:20

标签: php codeigniter email smtp

<?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,但我失败了。)

如何解决这个问题呢?

9 个答案:

答案 0 :(得分:114)

$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 Forums

答案 1 :(得分:18)

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

;extension=php_openssl.dll

取消注释。 :d

(从语句中删除分号)

extension=php_openssl.dll

答案 2 :(得分:14)

根据CI文档(CodeIgniter Email Library)...

  

如果您不想使用上述方法设置首选项,则可以   而是将它们放入配置文件中。只需创建一个名为的新文件   email.php,在该文件中添加$ config数组。然后保存文件   在config / email.php,它将自动使用。你不会   如果您保存,则需要使用$ this-&gt; email-&gt; 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

答案 3 :(得分: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();

答案 4 :(得分: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());
    }

答案 5 :(得分:1)

我在使用Postfix的Linux服务器上工作的另一个选项:

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

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

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

如果您有一个已经配置为将部分/全部外发电子邮件发送给Google的Linux盒子,这会更简单(并且不那么碎片化)。

答案 6 :(得分: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';

它对我有用。

答案 7 :(得分:1)

可能是这样的:

  

如果您使用cpanel为您的网站smtp限制是有问题的   并导致此错误。 SMTP限制

Error while sending an email with CodeIgniter

答案 8 :(得分:0)

对此有一个非常简单的答案。唯一适用于 en 的解决方案是这种配置..我只需要将端口更改为 587 并覆盖 codeignitor 默认的“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);