【问题标题】:Sending email with gmail smtp with codeigniter email library使用带有 codeigniter 电子邮件库的 gmail smtp 发送电子邮件
【发布时间】:2021-03-26 21:42:50
【问题描述】:
<?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 错误,索引键是 validate 所以使用$config['validate'] = TRUE

标签: php codeigniter email smtp


【解决方案1】:
$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,注意所有的Array元素都用单引号括起来,只有$this-&gt;email-&gt;set_newline的参数用双引号括起来。这很重要,否则不起作用。
  • 那是因为在 PHP(和其他一些语言)中,\n 只有在双引号中才表示换行。在单引号中,它表示文字字符 \n.
  • 解决方案是,thnx:对于 $this->email->set_newline("\r\n")
  • @NaveenKumar 仍然无法发送电子邮件。在$result = $this-&gt;email-&gt;send();没有得到任何回复
  • @ThorpeObazee 先生,我们可以在邮件配置文件中添加更多配置吗?比如,$config['subjectEnquiry'] => "查询邮箱";或 $config['subjectSuccess'] => "成功添加数据";
【解决方案2】:

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

;extension=php_openssl.dll

取消注释。 :D

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

extension=php_openssl.dll

【讨论】:

  • 这仅适用于 Windows 服务器。使用 phpinfo() 查看是否有可用的正确套接字以及 openssl 是否在您的服务器上。
【解决方案3】:

根据 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 和编码方面比较新。谢谢你:)
【解决方案4】:

将其更改为以下内容:

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

    【讨论】:

      【解决方案6】:

      我在使用 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)

      如果您有一个已配置为将其部分/全部外发电子邮件发送给 Google 的 linux 机器,这将更简单(并且碎片更少)。

      【讨论】:

        【解决方案7】:

        也许您的托管服务器和电子邮件服务器位于同一位置,您无需进行 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";
        【解决方案8】:

        可以是这样的:

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

        Error while sending an email with CodeIgniter

        【讨论】:

          【解决方案9】:

          对此有一个非常简单的答案。唯一有效的解决方案是这个配置..我只需要将端口更改为 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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-12-08
            • 2015-11-10
            • 2016-10-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多