Search code examples
phpcodeigniteremailsmtpcodeigniter-4

CodeIgnititer 4: Unable to send email using PHP SMTP


I read all the other answers related to that question, but none of them help.

When I try to run the following setup either on my localhost or my production server, I get the following error message:

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

I installed CodeIgniter 4 and added the following to .env:

email.production.protocol = smtp
email.production.SMTPHost = my.server.com
email.production.SMTPUser = [email protected]
email.production.SMTPPass = MyPassword
email.production.SMTPCrypto = ssl
email.production.SMTPPort = 465
email.production.SMTPFromName = "Foo Bar"

For port 465 or 587 and crypto ssl or tsl I tried every possible option. In the app/Config/Email.php the setting public $newline = "\r\n"; is already set (Suggestion coming from here.

I am successfully able to run

telnet my.server.com 465
telnet my.server.com 587

Then I added the following code to the end of app/Config/Email.php:

public function __construct()
    {
        $this->protocol = $_ENV['email.production.protocol'];
        $this->SMTPHost = $_ENV['email.production.SMTPHost'];
        $this->SMTPUser = $_ENV['email.production.SMTPUser'];
        $this->SMTPPass = $_ENV['email.production.SMTPPass'];
        $this->SMTPPort = $_ENV['email.production.SMTPPort'];
        $this->SMTPCrypto = $_ENV['email.production.SMTPCrypto'];
        $this->fromEmail = $_ENV['email.production.SMTPUser'];
        $this->fromName = $_ENV['email.production.SMTPFromName'];
    }

In my Controller I added a function with:

$email = \Config\Services::email();
$email->setSubject("Test");
$email->setMessage("Test");
$email->setTo("[email protected]");
if ($email->send(false)) {
    return $this->getResponse([
        'message' => 'Email successfully send',
    ]);
} else {
    return $this
        ->getResponse(
            ["error" => $email->printDebugger()],
            ResponseInterface::HTTP_CONFLICT
        );
}

Calling this function produces the error message described above. I assume that this has nothing to do with the server configuration as the error message describes, because the is happening on localhost and production.

Update: This must have something to do with the CI setup. No matter what server I try, even with completely incorrect values (e.g. incorrect password) the error is exactly the same.


Solution

  • I usually use smtp gmail to send email for my client. the most important of 'send email by smtp gmail' is you must update your Gmail Security rules:

    1. In your Gmail Account, click on Manage your Google Account
    2. click tab Security
    3. then, turn 'less secure app access' to 'ON'

    After that, you set your 'app\config\EMail.php' like this:

        public $protocol = 'smtp';
        public $SMTPHost = 'smtp.gmail.com';
        public $SMTPUser = '[email protected]';
        public $SMTPPass = 'yourpassword';
        public $SMTPPort = 465;
        public $SMTPCrypto = 'ssl';
        public $mailType = 'html';
    

    Last, you can create sendEmai function on controller like this:

    $email = \Config\Services::email();
    
    $email->setFrom('[email protected]', 'Mr Sender');
    $email->setTo('[email protected]');
    
    
    $email->setSubject('Test Subject');
    $email->setMessage('Test My SMTP');
    
    if (!$email->send()) {
    
        return false;
    
     }else{
    
        return true;
    
    }