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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1555145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:03:32  来源:igfitidea点击:

Sending email with gmail smtp with codeigniter email library

phpcodeigniteremailsmtp

提问by Shiv Kumar

<?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']    = '[email protected]';
        $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('[email protected]', 'myname');
        $this->email->to('[email protected]'); 

        $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

使用 PORT 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?

我如何解决这个问题伙计们?

回答by Teej

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

回答by Cerebro

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

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

;extension=php_openssl.dll

;extension=php_openssl.dll

Uncomment it. :D

取消注释。:D

(by removing the semicolon from the statement)

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

extension=php_openssl.dll

extension=php_openssl.dll

回答by Luke

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

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

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.

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

回答by RobinCominotto

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'] = "[email protected]"; 
$config['smtp_pass'] = "yourpassword";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";

$ci->email->initialize($config);

$ci->email->from('[email protected]', 'Blabla');
$list = array('[email protected]');
$ci->email->to($list);
$this->email->reply_to('[email protected]', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->send();

回答by Chamil Sanjeewa

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 protected]', 'Website');
    $list = array('[email protected]', '[email protected]');
    $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());
    }

回答by leonbloy

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

我有另一个选择,在带有 Postfix 的 linux 服务器中:

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

首先,配置 CI 电子邮件以使用您服务器的电子邮件系统:例如,在email.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)

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

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 机器,并且已经配置为将其部分/全部外发电子邮件发送给 Google,那么这会简单得多(并且不那么零散)。

回答by Shiplu

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' => '[email protected]',
    'smtp_pass' => '**********'
    );

or

或者

$config['protocol'] = '';
$config['smtp_host'] = '';
$config['smtp_port'] = ;
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'password';

it works for me.

这个对我有用。

回答by Adrian P.

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 发送电子邮件时出错