Sending email using gmail smtp using email codeigniter

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

Now I do not want to use phpmailer. (Actually I tried to use phpmailer, but I failed).

How to solve this problem guys?

+59
php email smtp codeigniter
Oct 12 '09 at 15:11
source share
8 answers
 $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 CodeIgniter Forums

+98
Oct 26 '09 at 3:56
source share

You need to enable SSL in your PHP configuration. Download php.ini and find the line with the following:

;extension=php_openssl.dll

Uncomment it .: D

(removing the semicolon from the statement)

extension=php_openssl.dll

+18
Apr 07 '10 at 2:36
source share

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

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

I managed to get this to work by putting all the settings in the /config/email.php application .

 $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 this line in the php.ini file and restart PHP:

 extension=php_openssl.dll 
+12
May 28 '13 at 12:28
source share

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(); 
+7
Jan 23 '14 at 21:52
source share

send html email via codeiginater

  $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()); } 
+4
Feb 23 '11 at 10:32
source share

Your hosting server and email server may be located in the same place and you do not need to pass smtp authentication. Just save everything by default as:

 $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'; 

he works for me.

+1
May 7, '15 at 15:14
source share

It could be:

If you use cpanel for your site, SMTT restrictions are a problem and cause this error. SMTP Limitations

Error sending email using CodeIgniter

+1
Jul 21 '16 at 11:25
source share

Another option that I have is on a linux server with Postfix:

First configure the CI email address to use your server’s email: e.g. email.php , e.g.

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

Then configure your postfix to relay mail to google (possibly depending on the sender address). You probably need to set the user password settings in /etc/postfix/sasl_passwd (docs)

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

0
May 2 '11 at 17:38
source share



All Articles