Invalid SMTP configurations: The connection will not get established if the SMTP configurations provided are invalid. For example, if the email protocol, credentials, host, or port is invalid, the connection will not get established.
SMTP authentication is disabled: If the email sending account does not have SMTP authentication enabled, it will not work. For example, if you're using Google, the SMTP connection will not get established unless you turn on the Less Secure Sign-In.
Firewall issues: Sometimes, the firewall may prevent outgoing requests that can potentially affect SMTP connections.
Invalid connection requirements: If you have not specified SSL or TLS, the connection will not get established (depending on the SMTP provider).
Formatting errors: In rare cases, I've experienced errors with CodeIgniter mail with SMTP where the use of single quotes for the configuration caused issues with establishing SMTP connections.
Solution - Here's How To Resolve It
There is no proper way to fix this error. Instead, try the actions below and see which one works for you.
Try disabling your anti-virus guard or firewall before proceeding.
Visit your SMTP provider and ensure the configurations (SMTP host, port, username, and password) are valid.
If your email sending account requires additional configuration, make sure to provide the configuration. For example, with Google, make sure to turn on Less Secure Sign-In.
Use the designated TLS or SSL port per your SMTP provider's guidelines.
Use double quotes rather than single quotes to specify the SMTP configuration.
The snippet shown below addresses the use of double-quotes. Therefore, you may use the snippet below and see if it resolves the error.
$config = array(
'protocol' => "smtp", // make sure this is set as SMTP to enable connections to SMTP servers, else it will send via inbuilt php mail.
'smtp_host' => "your.smtp.host", // use double-quotes'smtp_port' => YOUR-PORT (as number),'smtp_user' => "YOUR-EMAIL-ADDRESS" 'smtp_pass' => "YOUR-EMAIL-PASSWORD",
'mailtype' => "text",
'charset' => "utf-8",
'wordwrap' => true,
'wrapchars' => 50,
'crlf' => "/r/n",
'newline' => "/r/n"
$this->load->library('email'); // load mail library
$this->email->initialize($config); // initialize with smtp configs
$this->email->send(); // email sent