无法在 CodeIgniter 4 中发送服务器电子邮件

我只是挂断电话发送服务器电子邮件。


我正在准备这样的电子邮件发送代码:


$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;

}

它显示此错误消息:


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--

并在错误日志中给出错误:


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)

我想提一下,

  1. 国际扩展在服务器中启用。

  2. 我正在使用控制面板。

  3. 它在 Codeigniter 3 上运行良好。

  4. 邮件功能在我的服务器中使用 row 方法,如下所示:

$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);

请帮忙。


提前致谢。


汪汪一只猫
浏览 466回答 4
4回答

翻翻过去那场雪

codeigniter 提供替代类型(Mail、Sendmail 和 SMTP)检查您的 cpanel 外发电子邮件配置或要求您的提供商检查 php 的 mail() 配置

千巷猫影

在我的例子中,从phpinfo() > intl > ICU 版本是4.6 。哪个太旧了。将其更新到最新版本对我有用。

慕工程0101907

您需要在您的服务器上设置 MTA(邮件传输代理)。例如:Postfix 或 exim,在某些情况下 nullmailer 可以解决问题可能您可以连接到提供商的 SMTP 中继

牛魔王的故事

我今天遇到了这个,很惊讶地看到图书馆开箱即用。似乎是 CI 的电子邮件库中的一个问题:/**&nbsp; &nbsp; &nbsp;* Validate email for shell&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* Applies stricter, shell-safe validation to email addresses.&nbsp; &nbsp; &nbsp;* Introduced to prevent RCE via sendmail's -f option.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @see&nbsp; &nbsp; &nbsp;https://github.com/codeigniter4/CodeIgniter/issues/4963&nbsp; &nbsp; &nbsp;* @see&nbsp; &nbsp; &nbsp;https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36&nbsp; &nbsp; &nbsp;* @license https://creativecommons.org/publicdomain/zero/1.0/&nbsp; &nbsp; CC0 1.0, Public Domain&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* Credits for the base concept go to Paul Buonopane <paul@namepros.com>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param string $email&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return boolean&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected function validateEmailForShell(&$email)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $email = static::substr($email, 0, ++$atpos)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . idn_to_ascii(static::substr($email, $atpos), 0, INTL_IDNA_VARIANT_UTS46);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+@[a-z0-9.-]{1,253}\z#i', $email));&nbsp; &nbsp; }我没有时间研究这个方法的全部内容(已经浪费了几个小时!),但能够绕过它并成功发送电子邮件。创建App/Libraries/Email.php以覆盖有问题的方法:<?php namespace App\Libraries;class Email extends \CodeIgniter\Email\Email{&nbsp; &nbsp; protected function validateEmailForShell(&$email){&nbsp; &nbsp; &nbsp; &nbsp; return TRUE;&nbsp; &nbsp; }}然后让服务返回你的子类App/Config/Services.php:public static function email(bool $getShared=TRUE){&nbsp; &nbsp; return $getShared ? static::getSharedInstance('email') : new \App\Libraries\Email();}
打开App,查看更多内容
随时随地看视频慕课网APP