title: CodeIgnititer 4: Unable to send email using PHP SMTP
body: <p>I read all the other answers related to that question, but none of them help.</p>
  <p>When I try to run the following setup either on my <strong>localhost</strong> or my <strong>production server</strong>, I get the following error message:</p>
  <pre><code>Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
  </code></pre>
  <p>I installed CodeIgniter 4 and added the following to <code>.env</code>:</p>
  <pre><code>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 = &quot;Foo Bar&quot;
  </code></pre>
  <p>For port <code>465</code> or <code>587</code> and crypto <code>ssl</code> or <code>tsl</code> I tried every possible option.
  In the <code>app/Config/Email.php</code> the setting <code>public $newline = &quot;\r\n&quot;;</code> is already set (Suggestion coming from <a href="https://stackoverflow.com/questions/45252547/unable-to-send-email-using-php-smtp-your-server-might-not-be-configured-to-send">here</a>.</p>
  <p>I am successfully able to run</p>
  <pre><code>telnet my.server.com 465
  telnet my.server.com 587
  </code></pre>
  <p>Then I added the following code to the end of <code>app/Config/Email.php</code>:</p>
  <pre><code>public function __construct()
      {
          $this-&gt;protocol = $_ENV['email.production.protocol'];
          $this-&gt;SMTPHost = $_ENV['email.production.SMTPHost'];
          $this-&gt;SMTPUser = $_ENV['email.production.SMTPUser'];
          $this-&gt;SMTPPass = $_ENV['email.production.SMTPPass'];
          $this-&gt;SMTPPort = $_ENV['email.production.SMTPPort'];
          $this-&gt;SMTPCrypto = $_ENV['email.production.SMTPCrypto'];
          $this-&gt;fromEmail = $_ENV['email.production.SMTPUser'];
          $this-&gt;fromName = $_ENV['email.production.SMTPFromName'];
      }
  </code></pre>
  <p>In my Controller I added a function with:</p>
  <pre><code>$email = \Config\Services::email();
  $email-&gt;setSubject(&quot;Test&quot;);
  $email-&gt;setMessage(&quot;Test&quot;);
  $email-&gt;setTo(&quot;myaddress@example.com&quot;);
  if ($email-&gt;send(false)) {
      return $this-&gt;getResponse([
          'message' =&gt; 'Email successfully send',
      ]);
  } else {
      return $this
          -&gt;getResponse(
              [&quot;error&quot; =&gt; $email-&gt;printDebugger()],
              ResponseInterface::HTTP_CONFLICT
          );
  }
  </code></pre>
  <p>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.</p>
  <p>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.</p>