Can't send server emails in CodeIgniter 4 Can't send server emails in CodeIgniter 4 codeigniter codeigniter

Can't send server emails in CodeIgniter 4


You need to setup an MTA on your server (Mail Transfer Agent).

For instance: Postfix or exim, and in some cases nullmailer will do the trick

Possibly you can connect to an SMTP relay of your provider


codeigniter provide alternative types ( Mail, Sendmail and SMTP ) check your cpanel outgoing email configuration or ask your provider to check for php's mail() configuration


I ran into this today, and was surprised to see the library failed out of the box. Seems to be an issue in CI's email lib here:

/**     * Validate email for shell     *     * Applies stricter, shell-safe validation to email addresses.     * Introduced to prevent RCE via sendmail's -f option.     *     * @see     https://github.com/codeigniter4/CodeIgniter/issues/4963     * @see     https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36     * @license https://creativecommons.org/publicdomain/zero/1.0/    CC0 1.0, Public Domain     *     * Credits for the base concept go to Paul Buonopane <paul@namepros.com>     *     * @param string $email     *     * @return boolean     */    protected function validateEmailForShell(&$email)    {        if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))        {            $email = static::substr($email, 0, ++$atpos)                . idn_to_ascii(static::substr($email, $atpos), 0, INTL_IDNA_VARIANT_UTS46);                    }        return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+@[a-z0-9.-]{1,253}\z#i', $email));    }

I don't have time to investigate what this method is all about (wasted a few hours on this already!), but was able to bypass it and send emails successfully.Create App/Libraries/Email.php to override the problematic method:

<?php namespace App\Libraries;class Email extends \CodeIgniter\Email\Email{    protected function validateEmailForShell(&$email){        return TRUE;    }}

Then make the service return your subclass in App/Config/Services.php:

public static function email(bool $getShared=TRUE){    return $getShared ? static::getSharedInstance('email') : new \App\Libraries\Email();}