简体   繁体   中英

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 = 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 (eg incorrect password) the error is exactly the same.

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;

}

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