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

How do I solve this problem guys?

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
NanoBot
  • 837
  • 1
  • 7
  • 10
  • `$config['validation'] = TRUE` is wrong, the index key is **validate** so use `$config['validate'] = TRUE` – Delmo Oct 15 '14 at 14:22

9 Answers9

128
$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 the CodeIgniter Forums

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Teej
  • 12,764
  • 9
  • 72
  • 93
  • 12
    +1, Notice all Array elements are enclosed in single quotes, and only parameters of `$this->email->set_newline` is enclosed in double quotes. this is very important or else it does not work. – Naveen Kumar May 02 '12 at 06:56
  • 12
    that is because in PHP(and a few other languages) `\n` only means new line if it is in double quotes. In single quotes it means the literal characters \n. – DMin Aug 04 '12 at 06:46
  • 1
    The solution was,thnx: for the $this->email->set_newline("\r\n") – TooCooL Dec 09 '15 at 13:05
  • 1
    @NaveenKumar still not able to send the email. not getting any response in `$result = $this->email->send();` – Pathik Vejani Mar 30 '16 at 19:29
  • 1
    @ThorpeObazee Sir Can we Add some more configuration in the mail config file. like, $config['subjectEnquiry'] => "Enquiry Email"; or $config['subjectSuccess'] => "successfully added the data"; – always-a-learner Mar 30 '17 at 08:54
  • 1
    I have found the article which describe how to send emails in a CodeIgniter application using SMTP and using the well known and maintained email sending class. https://www.cloudways.com/blog/send-email-codeigniter-smtp/ – Owais Alam Aug 23 '17 at 11:43
  • 1
    I have found the article which demonstrate how to send emails in a CodeIgniter application using SMTP. https://www.cloudways.com/blog/send-email-codeigniter-smtp/ – Owais Alam Aug 23 '17 at 13:13
  • please someone tell me , in email library which code should i use . i don't have email library how can i get email library . i am new in CI – prakash pokhrel Jan 16 '18 at 08:42
  • i used same code...but in my mail box ..got email that "Sign-in attempt was blocked" what more settings is needed ? my IMAP is enabled already – Manjiri Parab Dec 02 '18 at 07:01
  • The important point is the use of `$CI->email->set_newline("\r\n");` line. Other wise no issue of declaring and initializing the `$config` array. Obviously we should enable `extension=php_openssl.dll` – int soumen Dec 21 '18 at 07:05
  • Using the above works, but if I change **'charset' => 'utf-8'** the message is not delivered. I'm using CodeIgniter 3.1.9 hosted on GoDaddy. I've read that Gmail changes all messages to utf-8 anyway. Any idea why 'utf-8' would fail to get delivered? – Genki Oct 09 '19 at 00:08
20

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

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

I was able to get this to work by putting all the settings into 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;

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

extension=php_openssl.dll
Luke
  • 18,811
  • 16
  • 99
  • 115
19

You need to enable SSL in your PHP config. Load up php.ini and find a line with the following:

;extension=php_openssl.dll

Uncomment it. :D

(by removing the semicolon from the statement)

extension=php_openssl.dll

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
Cerebro
  • 207
  • 2
  • 2
  • 1
    This is only for Windows Servers. Use phpinfo() to see if you have the proper sockets available and if openssl is on your server. – jjwdesign Oct 25 '11 at 18:28
7

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();
RobinCominotto
  • 959
  • 8
  • 18
5

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());
    }
Jakub
  • 20,418
  • 8
  • 65
  • 92
Chamil Sanjeewa
  • 319
  • 1
  • 4
  • 12
1

Perhaps your hosting server and email server are located at same place and you don't need to go for smtp authentication. Just keep every thing default like:

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

it works for me.

Shiplu
  • 460
  • 6
  • 13
1

It can be this:

If you are using cpanel for your website smtp restrictions are problem and cause this error. SMTP Restrictions

Error while sending an email with CodeIgniter

Community
  • 1
  • 1
Adrian P.
  • 5,060
  • 2
  • 46
  • 47
1

Another option I have working, in a linux server with Postfix:

First, configure CI email to use your server's email system: eg, in email.php, for example

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

Then configure your postfix to relay the mail to google (perhaps depending on the sender address). You'll probably need to put you user-password settings in /etc/postfix/sasl_passwd (docs)

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

leonbloy
  • 73,180
  • 20
  • 142
  • 190
0

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);
Syscall
  • 19,327
  • 10
  • 37
  • 52
Ady
  • 67
  • 1
  • 4