0

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 = My@Mail.com
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("myaddress@example.com");
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.

PeterPan
  • 195
  • 2
  • 16

1 Answers1

1

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 = 'your.googleaccount@gmail.com';
    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('emailsender@gmail.com', 'Mr Sender');
$email->setTo('emailreceiver@gmail.com');


$email->setSubject('Test Subject');
$email->setMessage('Test My SMTP');

if (!$email->send()) {

    return false;

 }else{

    return true;

}
  • Thank you for your answer but I don't have a gmail email address. – PeterPan Apr 19 '22 at 18:40
  • @ondri-nurdiansyah Keep in mind that **starting May 30, 2022**, Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. - [Less secure apps & your Google Account](https://support.google.com/accounts/answer/6010255?hl=en#:~:text=To%20help%20keep%20your%20account,only%20your%20username%20and%20password.) – steven7mwesigwa Apr 20 '22 at 00:02
  • last year, i have email from my organization. It use 'roundcube' as mailserver and i use PHPMailer Library to send email by PHP. the setting and function on controller are looks alike – Ondri Nurdiansyah Apr 20 '22 at 01:53
  • @OndriNurdiansyah I am using my own mail server and I need to use SMTP. In other php libraries my mail server is working. I assume the problem is with the code. – PeterPan Apr 20 '22 at 07:46