EllisLab text mark
Advanced Search
     
Sending email with gmail smtp with codeigniter email library
Posted: 18 October 2009 02:29 PM   [ Ignore ]
Joined: 2009-10-17
3 posts

hello every1 .
This is my 1st post here.
i have almost completed my project. But i can’t send a single mail with CI :(.
please help me .
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 dont want to use phpmailer now. (Actually i have tried to use phpmailer,but i falied )

 
Posted: 18 October 2009 03:12 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2006-05-08
72 posts

I had problems with this initially and got around it by loading the parameters into an array, then initialising the library using the array.

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

K-Point Internet Solutions
innovative ebusiness solutions


Web design and development in Northern Ireland

 
Posted: 19 October 2009 12:40 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-10-17
3 posts

It worked!!!
Thank u very much

 
Posted: 05 March 2010 12:02 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-09-17
8 posts

That solved the issue for me too

Does this mean using config/email.php is deprecated?

I was using $this->load->library(‘email’);
and removing config/email.php and adding the $config array directly to the controller and using $this->load->library(‘email’, $config); Worked

 
Posted: 22 March 2010 06:50 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-10-01
25 posts

hy bapjon,

how did u resolve the problem..actually, i did this last week and its working fine. i received d email to target email.btw, yesterday i run again d code and i got problem same as yours.. fsockopen error.. i really dun have any idea bout this error since the code are exactly same ( i used code given at first page). is dat problem with my server or else?thx for any helps guys.

 
Posted: 22 March 2010 06:52 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2009-10-01
25 posts

sorry, this is my code:

<?php

class Test1 extends Controller{
 
  function __construct()
  {
      parent::Controller();
  }
 
  function index()
  {
  $data[‘pagetitle’] = ‘Account Activation’;
  $email = $this->load->view(‘account/test1’,$data,TRUE);
     
  $config = Array(
  ‘protocol’ => ‘smtp’,
  ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
  ‘smtp_port’ => 465,
  ‘smtp_user’ => ‘#######@gmail.com’,
  ‘smtp_pass’ => ‘#######’,
  ‘mailtype’ => ‘html’,
  ‘wordwrap’ => TRUE
  );
  $this->load->library(‘email’, $config);
  $this->email->set_newline(”\r\n”);
 
  $this->email->from(’########@gmail.com’, ‘farah’);
  $this->email->to(’######@yahoo.com’);
 
  $this->email->subject(’ Testing testing testing….’);
 
  $this->email->message($email);
 
  if (!$this->email->send())
      show_error($this->email->print_debugger());
  else
      echo ‘Your e-mail has been sent!’;
  }

?>

 
Posted: 21 September 2010 12:24 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2010-09-21
1 posts

I’m sorry,

may i know the solve about this problem???i made a web n i found this problem to. This is my code :

              $config = Array(
                ‘protocol’ => ‘smtp’,
                ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,
                ‘smtp_port’ => ‘465’,
                ‘smtp_user’ => ‘zzzzz@gmail.com’,
                ‘smtp_pass’ => ‘yyyyyy’,
                ‘mailtype’  => ‘html’,
                ‘charset’  => ‘iso-8859-1’
              );
             
              $this->load->library(‘email’,$config);
              $this->email->set_newline(”\r\n”);

              $this->email->from($emailkontak, $namakontak);
              $this->email->to(‘yyyyyy@paseksuardika.com’);

              $this->email->subject(”[”.$tujuankontak.”]”.” “.$subjectkontak);
              $this->email->message($pesankontak); 

              $this->email->send();

Please help me to solve this problem. Thnx for advance…

 
Posted: 02 March 2011 07:58 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-02-04
5 posts
dsloan - 18 October 2009 07:12 PM

I had problems with this initially and got around it by loading the parameters into an array, then initialising the library using the array.

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

Worked great!  Tks!

 
Posted: 09 January 2012 08:25 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2011-11-04
1 posts

Thanks it worked for me!!......and with a little tweak….i figured out how to use custom FROM email mail address rather than your primary gmail address,which you just used to get Authorize with gmail…

Any other FROM email address should be Added into your gmail account,which can be done by going to google account settings and add a new account,just add your account in it…and use that VERIFIED email address…in your script!!....Cheers!!... smile

 
Posted: 27 March 2012 10:27 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2012-03-27
1 posts

Hi im a beginner and still a little bit stuck….

My Code

<?php

class Email extends Controller
{
  function Email()
  {
      parent::Controller(); 
      $this->load->library(‘email’);
  }


function index(){
  // Collect data
 

  // Config Server
$config_array = 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”);

 
  $this->email->from(‘xxx’);
  $this->email->to(‘xxx’);
 
  $this->email->subject(‘Website Contact Message’);
 

  $this->email->message(‘Hello World’);

  $this->email->send();
  echo $this->email->print_debugger();
}

}
?>

i am getting this response

You did not specify a SMTP hostname.
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

 
Posted: 27 September 2012 04:40 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-09-27
1 posts

Thank you for information.

 
Posted: 07 April 2013 11:35 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2013-03-22
7 posts
dsloan - 18 October 2009 03:12 PM

I had problems with this initially and got around it by loading the parameters into an array, then initialising the library using the array.

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

This helped me 2 ! ... smile ... Now there is a totally different error I’ll analyze know ... smile

Just REMOVING THE FILE “email.php” (or renaming it) in the config folder in CodeIgniter MAKES THE DIFFERENCE.

EDIT:
AND !!! ... then I had to install sendmail (Linux/debian: sudo apt-get install sendmail) because PHP - efter correcting the above error - still came up with “Unable to send email using PHP mail()” ...  - Now I’ve received the send mail. It works. smile

The solution works without ssl. - My method in a controller looks like this (works!):

public function dummymail()
 
{
  
  $config 
= array(
      
'protocol' => 'smtp',
      
'smtp_host' => 'mail.provider.net',
      
'smtp_port' => 25,
      
'smtp_user' => 'WRITE_YOUR_EMAIL_HERE',
      
'smtp_pass' => 'WRITE_YOUR_PASSWORD_HERE',
      
'charset' => 'utf-8',
      
'mailtype' => 'html'
      
);
  
$this->load->library('email',$config);
  
  
$this->email->set_newline("\r\n");
  
  
$this->email->from('WRITE_YOUR_EMAIL_HERE''Your name');
  
$this->email->to('WRITE_RECIPIENT_EMAIL_HERE');

  
$this->email->subject('Email Test');
  
$this->email->message('Testing at home the email class of CodeIgniter.');

  if(
$this->email->send()) {
   
echo 'Your email was sent.';
  
else {
   show_error
($this->email->print_debugger());
  
}
 } 

Later I’ll see if I can get it to work with ssl.