4

I just hanged up to send server emails.

I'm preparing email sending code like this:

$email_config = Array(
    'charset' => 'utf-8',
    'mailType' => 'html'
);

$email = \Config\Services::email();
$email->initialize($email_config);

$email->setNewline("\r\n");
$email->setCRLF("\r\n");
$email->setFrom("test@mysite.com", "Sender name");

$email->setTo("receiver@gmail.com");
$email->setSubject("Test message");
$email->setMessage("Hello");

if ($email->send()) {
    echo "Email sent!";
} else {
    echo $email->printDebugger();
    return false;
}

It's showing this error message:

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
Date: Thu, 4 Jun 2020 05:21:47 -0500
From: "Sender name" <test@mysite.com>
Return-Path: <test@mysite.com>
Reply-To: <test@mysite.com>
User-Agent: CodeIgniter
X-Sender: test@mysite.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5ed8cb3ba9e500.94682473@mysite.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_5ed8cb3ba9e702.65790334"
=?UTF-8?Q?Test=20message?=
This is a multi-part message in MIME format.
Your email application may not support this format.

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

Hello


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

Hello

--B_ALT_5ed8cb3ba9e702.65790334--

And giving an error in error log:

Email: sendWithMail throwed Use of undefined constant INTL_IDNA_VARIANT_UTS46 - assumed 'INTL_IDNA_VARIANT_UTS46' (this will throw an Error in a future version of PHP)

I want to mention that,

  1. intl extension is enabled in server.
  2. I'm using cPanel.
  3. It was working fine on Codeigniter 3.
  4. The mail function is working with the row method in my server like this:
$to = 'receiver@gmail.com';
$subject = 'Test message';
$message = 'Hello';
$headers = 'From: test@mysite.com' . "\r\n" .
        'Reply-To: test@mysite.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

Please help.

Thanks in advance.

Encrypted
  • 628
  • 1
  • 10
  • 22

4 Answers4

0

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

on8tom
  • 1,766
  • 11
  • 24
  • It's working fine without installing anything in Codeigniter 3. Is it required to install anything you mentioned to send in Codeigniter 4? I didn't find anything on their docs. – Encrypted Jun 04 '20 at 14:13
0

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've checked with Mail & Sendmail. Both aren't working. But SMTP is working. But that's not the solution for me. I think I don't need to create any associated email on cPanel for this. Right? This is working good in Codeigniter 3. Why not in Codeigniter 4? – Encrypted Jun 04 '20 at 14:05
  • can you share the exact version of your codeigniter on composer.lock ? i will try to simulate this behavior – ocricci Jun 04 '20 at 16:32
  • I'm using 4.0.3 from my composer.lock – Encrypted Jun 04 '20 at 18:01
0

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();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mr.M
  • 1
  • 1
0

Got a solution from CI forum.

From phpinfo() > intl > ICU version was 4.6 in my case. Which is too old.

Updating that to the latest version worked for me.

Encrypted
  • 628
  • 1
  • 10
  • 22