CodeIgniter4: Bug: Can't Send Email Using SMTP

PHP Version

8.3

CodeIgniter4 Version

4.6.0

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

No response

What happened?

I’ve tried to setup email sender using CodeIgniter Email class. I use SMTP configuration with SMTPCrypto=tls, SMTPPort=587 and other auth configuration. Then it shows this debug message:

hello: 
The following SMTP error was encountered:
starttls: 
The following SMTP error was encountered:
Unable to send email using SMTP. Your server might not be configured to send mail using this method.
Date: Wed, 12 Feb 2025 17:35:25 +0700
To: toha.samba@gmail.com
Subject: =?UTF-8?Q?SMTP=20Email=20Testing?=
From: "Masagi" <masagi@appdata.id>
Return-Path: <masagi@appdata.id>
Reply-To: <masagi@appdata.id>
User-Agent: Tarbiyya
X-Sender: masagi@appdata.id
X-Mailer: Tarbiyya
X-Priority: 3 (Normal)
Message-ID: <67ac796d34a286.70605141@appdata.id>
Mime-Version: 1.0


Content-Type: multipart/alternative; boundary="B_ALT_67ac796d34a404.14803652"

This is a multi-part message in MIME format.
Your email application may not support this format.

--B_ALT_67ac796d34a404.14803652
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is sample body of email testing using SMTP.


--B_ALT_67ac796d34a404.14803652
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

This is sample body of email testing using SMTP.

--B_ALT_67ac796d34a404.14803652--

For comparison, I try to use PHPMailer package with same configuration I’ve set in Config\Email class and it sent the message successfully. I use SMTP account from Hostinger.

Steps to Reproduce

This is my sample code:

// CodeIgniter Email
$email = service("email");
$email->setTo("toha.samba@gmail.com");
$email->setSubject("SMTP Email Testing");
$email->setMessage("This is sample body of email testing using SMTP.");
$email->send(false);
echo $email->printDebugger();

// PHPMailer
$mail = new PHPMailer(true);
$EmailConfig = new \Config\Email;
try {
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->Host       = $EmailConfig->SMTPHost;
    $mail->SMTPAuth   = true;
    $mail->Username   = $EmailConfig->SMTPUser;
    $mail->Password   = $EmailConfig->SMTPPass;
    $mail->SMTPSecure = $EmailConfig->SMTPCrypto; // 'tls'
    $mail->Port       = $EmailConfig->SMTPPort; // 587

    $mail->setFrom($EmailConfig->fromEmail, $EmailConfig->fromName);
    $mail->addAddress('toha.samba@gmail.com');
    $mail->isHTML(true);

    $mail->Subject = 'SMTP Email Testing';
    $mail->Body    = 'This is sample body of email testing using SMTP.';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Expected Output

I don’t know if I missed something in my configuration or if there is an error in the Email class.

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (12 by maintainers)

Most upvoted comments

@ip-qi OP was able to successfully send emails using PHPMailer.

@yllumi It looks like the SMTP timeout might be causing the issue. By default, SMTPTimeout is set to 5 seconds, which might not be enough, especially if the mail server takes longer to respond due to network latency or processing delays. Since you’re operating from Indonesia, this could be a factor. Let’s increase the timeout to 15 seconds and see how it goes.

// edit I just checked and PHPMailer sets it to even more - 30 seconds.

sendmail is work

getHostname get [192.168.1.1] , eq SERVER_ADDR

@ip-qi If you prepare a PR with AUTH PLAIN, I think we will accept it without problems - however, you need to prepare a config option for it.

Sure, i added /** * Which SMTP AUTH method to use (‘LOGIN’, ‘PLAIN’) * * @var string */ protected $SMTPAuthMethod = ‘LOGIN’; <- which should not break anything as majority uses this method

And also i added in email.rst appropriate changes to reflect additional config parameter

Also PR is on the way