Sending email with gmail smtp with codeigniter email library

<?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

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).

伙计们,我该怎么解决这个问题?

357239 次浏览
$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 论坛

您需要在 PHP 配置中启用 SSL:

;extension=php_openssl.dll

取消评论。 : D

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

extension=php_openssl.dll

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

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

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

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

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

如果你有一个 linux 机器,已经配置好可以向 Google 发送一些/所有的邮件,那么这就简单多了(而且不那么支离破碎)。

根据线人档案(电子邮件库) ..。

如果不喜欢使用上述方法设置首选项,可以 只需创建一个名为 在 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

改为:

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

也许您的主机服务器和电子邮件服务器位于同一个地方,您不需要进行 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';

对我有用。

可以是这样的:

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

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

There is very simple answer to this . and the only solution that worked at the en was this configuration ..i only had to chnage the port to 587 and overwrite the codeignitor default "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);