ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Sending email with gmail smtp with codeigniter email library

October 18, 2009 2:29pm

Subscribe [9]
  • #1 / Oct 18, 2009 2:29pm

    bapjon

    1 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']    = '[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 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 )

  • #2 / Oct 18, 2009 3:12pm

    dsloan

    48 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();
  • #3 / Oct 19, 2009 12:40pm

    bapjon

    1 posts

    It worked!!!
    Thank u very much

  • #4 / Mar 04, 2010 11:02pm

    oste15

    5 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

  • #5 / Mar 22, 2010 6:50am

    miss_amylee

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

  • #6 / Mar 22, 2010 6:52am

    miss_amylee

    21 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!';
    }

    ?>

  • #7 / Sep 21, 2010 12:24pm

    Gde Dharma

    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’ => ‘[email protected]’,
                    ‘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(‘[email protected]’);

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

                  $this->email->send();

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

  • #8 / Mar 02, 2011 6:58pm

    HA

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

    Worked great!  Tks!

  • #9 / Jan 09, 2012 7:25pm

    lapak10

    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!!... 😊

  • #10 / Mar 27, 2012 10:27am

    clockworkweb

    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.

  • #11 / Sep 27, 2012 4:40am

    Sheena Lunatics

    1 posts

    Thank you for information.

  • #12 / Apr 07, 2013 11:35am

    futo

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

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

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

    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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases