1

I would like to send email via my gmail address using CodeIgniter. I tried many solutions, but none of them work.

My code :

  $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'xxxxx@gmail.com',
        'smtp_pass' => 'yyyyy',
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'wordwrap' => TRUE
    );


    $this->load->library('email', $config);

    $this->email->from('xxxxx@gmail.com', 'admin');
    $this->email->to('zzzzz@gmail.com');
    $this->email->subject('Email test');
    $msg = "Testing email.";

    $this->email->message($msg);

    $this->email->send();

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

I have the following error :

Message: fwrite(): send of 5 bytes failed with errno=32 Broken pipe
Filename: libraries/Email.php

and

 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

For the moment, I'am working in localhost.

Thank you for helping me.

iAmoric
  • 1,787
  • 3
  • 31
  • 64
  • I think you didn't allow use of SMTP in your gmail account. You need to check in your google SMTP settings. – kishor10d Jun 12 '17 at 09:40

3 Answers3

1

In CodeIgniter, you can configure the email data by creating a email.php file. I am using this for a long time. so it could work for you as well.

Inside the file:

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com',
    'smtp_port' => 465,
    'smtp_user' => 'yourGmailId@gmail.com', // change it to yours
    'smtp_pass' => 'your password', // change it to yours
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE,
    'from' => 'your_from_email@bla.com',
    'subject' => 'test email'
);

Now in the controller, you can do this:

$this->config->load('email');

    $subject = $this->config->item('subject');
    $from = $this->config->item('from');

    $this->load->library('email');
    $this->email->set_newline("\r\n");

    $this->email->from($from, 'Yards & Acares');
    $data = array(
        'some_data' => $email            
    );

    $this->email->to($email);  // replace it with receiver mail id
    $this->email->subject($subject); // replace it with relevant subject 

    // Here is I have added an email template.
    $body = $this->load->view('admin/propMailer.php', $data, TRUE);

    $this->email->message($body);

    if ($this->email->send()) {
       //Do whatever you want if success
    } else {
       //Do whatever you want if failed 
        log_message('error', 'Unable to Send Email to Customer.' . print_r($this->email->print_debugger(array('headers', 'subject')), TRUE));

   }
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
0

Please use below mentioned solution

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

Also in php.ini please enable openssl

// Find the line.
;extension=php_openssl.dll

// Remove  ";" from this line to enable
extension=php_openssl.dll
Alex Mac
  • 2,970
  • 1
  • 22
  • 39
0

For sending Gmail email on localhost, you can take a look at my answer here

Do this on local development for testing codeigniter email functions.

Keith Asilom
  • 179
  • 4
  • 11