简体   繁体   中英

Unable to send email with CodeIgniter

I am a novice to CI and PHP. Currently I'm using xammp to run PHP, Apache and MySql. Now I am trying to send an email with CI. But I constantly got an error as below.

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to ssl://smtp.googlemail.com:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. )

Filename: libraries/Email.php

Line Number: 1689

I have google about three hours and still not found the solution. I have already enabled ' openssl ' in Apache. I would like to describe coding for my Email Controller Class.

//Code of Email Controller 'Index' function

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

    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $config['protocol'] = 'smtp';
    $config['smtp_port'] = 465;
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    $config['smtp_user'] = "something@gmail.com";
    $config['smtp_pass'] = "xxx";

    $this->email->initialize($config);

    $this->email->from('something@gmail.com', 'NayLinAung');
    $this->email->to('something@gmail.com');
    $this->email->subject('This is an email test');
    $this->email->message("It is working, Great! You will be successful.");

    if ($this->email->send())
    {
        echo "Email was successfully sent.";
    }
    else {  
        show_error($this->email->print_debugger());
    }

When I change 'smtp' to 'mail', errors are not occurred but the email was not actually sent. In addition, I shutdown my Windows firewall.

Please, suggest any solutions for this error.

What I have found earlier is:

To use Google SMTP in CodeIgniter you need to make 2 (two) changes into your Gmail account setting: (NB Please be aware that it is now easier for an attacker to break into your account -says Google)

  1. Set off 2-step Verification .
  2. Allow less secure apps : ON (or Enable)

Now use 'smtp_host' as ssl://smtp.gmail.com instead of smtp.googlemail.com

This issue also discussed in this link .

Hope this help.

if you can use try with this IMAP library

Codeigniter Imap Library

you can connect with this code

$config['protocol'] = "imap";
$config['imap_host'] = 'xxx.xxx.xxx.xxx';
$config['imap_user'] = 'user@domain.com';
$config['imap_pass'] = 'Jk98S**Jlk';
$config['imap_port'] = '993';
$config['imap_mailbox'] = 'INBOX';
$config['imap_path'] = '/imap/ssl/novalidate-cert'; // use this string if you do not want use ssl
$config['imap_server_encoding'] = 'utf-8';
$config['imap_attachemnt_dir'] = './tmp/';
$this->load->library('email', $config);
$this->email->from($frommail);
$this->email->to($tomail);
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();

you can use also SMTP but if you have permition I advise you to use IMAP . it have more functions and it is newer than SMTP ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM