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

时间:2022-11-15 10:53:19
<?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');
    }
}

I am getting this error:

我得到了这个错误:

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

Using PORT 25/587

使用端口25/587

I got this error:

我得到这个错误:

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

I don't want to use phpmailer now. (Actually I have tried to use phpmailer, but I failed).

我现在不想用phpmailer。(其实我试过用phpmailer,但是失败了)。

How do I solve this problem guys?

我怎么解决这个问题?

8 个解决方案

#1


107  

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

From the CodeIgniter Forums

从CodeIgniter论坛

#2


18  

You need to enable SSL in your PHP config. Load up php.ini and find a line with the following:

您需要在PHP配置中启用SSL。php装载。ini并找到一行与以下内容:

;extension=php_openssl.dll

;扩展= php_openssl.dll

Uncomment it. :D

取消它。:D

(by removing the semicolon from the statement)

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

extension=php_openssl.dll

扩展= php_openssl.dll

#3


13  

According to the CI docs (CodeIgniter Email Library)...

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

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

如果您不喜欢使用上述方法设置首选项,您可以将它们放入配置文件中。只需创建一个名为电子邮件的新文件。php,在该文件中添加$config数组。然后在config/email中保存文件。php会自动使用。如果将首选项保存在配置文件中,则不需要使用$this->电子邮件->initialize()函数。

I was able to get this to work by putting all the settings into application/config/email.php.

我可以通过将所有的设置放入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;

Then, in one of the controller methods I have something like:

然后,在其中一个控制器方法中,我有如下内容:

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

Also, as Cerebro wrote, I had to uncomment out this line in my php.ini file and restart PHP:

而且,正如Cerebro所写的,我必须在php中取消注释。ini文件和重启PHP:

extension=php_openssl.dll

#4


7  

Change it to the following:

将其更改为以下内容:

$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


4  

send html email via codeiginater

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

#6


1  

Perhaps your hosting server and email server are located at same place and you don't need to go for smtp authentication. Just keep every thing default like:

也许您的主机服务器和电子邮件服务器位于同一个位置,您不需要进行smtp身份验证。保持所有的东西都是默认的:

$config = array(        
    'protocol' => '',
    'smtp_host' => '',
    'smtp_port' => '',
    'smtp_user' => 'yourname@gmail.com',
    'smtp_pass' => '**********'
    );

or

$config['protocol'] = '';
$config['smtp_host'] = '';
$config['smtp_port'] = ;
$config['smtp_user'] = 'yourname@gmail.com';
$config['smtp_pass'] = 'password';

it works for me.

它适合我。

#7


1  

It can be this:

可以是这样的:

If you are using cpanel for your website smtp restrictions are problem and cause this error. SMTP Restrictions

如果您正在使用cpanel,您的网站smtp限制是问题,并造成此错误。SMTP的限制

Error while sending an email with CodeIgniter

使用CodeIgniter发送电子邮件时出错

#8


0  

Another option I have working, in a linux server with Postfix:

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

First, configure CI email to use your server's email system: eg, in email.php, for example

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

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

Then configure your postfix to relay the mail to google (perhaps depending on the sender address). You'll probably need to put you user-password settings in /etc/postfix/sasl_passwd (docs)

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

This is much simpler (and less fragmente) if you have a linux box, already configured to send some/all of its outgoing emails to Google.

如果您有一个linux box,已经配置为向谷歌发送一些/所有的传出电子邮件,那么这将更简单(也更少碎片)。

#1


107  

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

From the CodeIgniter Forums

从CodeIgniter论坛

#2


18  

You need to enable SSL in your PHP config. Load up php.ini and find a line with the following:

您需要在PHP配置中启用SSL。php装载。ini并找到一行与以下内容:

;extension=php_openssl.dll

;扩展= php_openssl.dll

Uncomment it. :D

取消它。:D

(by removing the semicolon from the statement)

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

extension=php_openssl.dll

扩展= php_openssl.dll

#3


13  

According to the CI docs (CodeIgniter Email Library)...

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

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

如果您不喜欢使用上述方法设置首选项,您可以将它们放入配置文件中。只需创建一个名为电子邮件的新文件。php,在该文件中添加$config数组。然后在config/email中保存文件。php会自动使用。如果将首选项保存在配置文件中,则不需要使用$this->电子邮件->initialize()函数。

I was able to get this to work by putting all the settings into application/config/email.php.

我可以通过将所有的设置放入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;

Then, in one of the controller methods I have something like:

然后,在其中一个控制器方法中,我有如下内容:

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

Also, as Cerebro wrote, I had to uncomment out this line in my php.ini file and restart PHP:

而且,正如Cerebro所写的,我必须在php中取消注释。ini文件和重启PHP:

extension=php_openssl.dll

#4


7  

Change it to the following:

将其更改为以下内容:

$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


4  

send html email via codeiginater

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

#6


1  

Perhaps your hosting server and email server are located at same place and you don't need to go for smtp authentication. Just keep every thing default like:

也许您的主机服务器和电子邮件服务器位于同一个位置,您不需要进行smtp身份验证。保持所有的东西都是默认的:

$config = array(        
    'protocol' => '',
    'smtp_host' => '',
    'smtp_port' => '',
    'smtp_user' => 'yourname@gmail.com',
    'smtp_pass' => '**********'
    );

or

$config['protocol'] = '';
$config['smtp_host'] = '';
$config['smtp_port'] = ;
$config['smtp_user'] = 'yourname@gmail.com';
$config['smtp_pass'] = 'password';

it works for me.

它适合我。

#7


1  

It can be this:

可以是这样的:

If you are using cpanel for your website smtp restrictions are problem and cause this error. SMTP Restrictions

如果您正在使用cpanel,您的网站smtp限制是问题,并造成此错误。SMTP的限制

Error while sending an email with CodeIgniter

使用CodeIgniter发送电子邮件时出错

#8


0  

Another option I have working, in a linux server with Postfix:

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

First, configure CI email to use your server's email system: eg, in email.php, for example

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

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

Then configure your postfix to relay the mail to google (perhaps depending on the sender address). You'll probably need to put you user-password settings in /etc/postfix/sasl_passwd (docs)

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

This is much simpler (and less fragmente) if you have a linux box, already configured to send some/all of its outgoing emails to Google.

如果您有一个linux box,已经配置为向谷歌发送一些/所有的传出电子邮件,那么这将更简单(也更少碎片)。