Php Can';t在CodeIgniter 4中发送服务器电子邮件
我刚刚挂断了发送服务器电子邮件 我正在准备这样的电子邮件发送代码:Php Can';t在CodeIgniter 4中发送服务器电子邮件,php,codeigniter,email,codeigniter-4,Php,Codeigniter,Email,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_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--
请帮忙
提前感谢。您需要在服务器(邮件传输代理)上设置MTA 例如:Postfix或exim,在某些情况下,nullmailer会起作用
可能您可以连接到提供商的SMTP中继codeigniter提供替代类型(Mail、Sendmail和SMTP)检查您的cpanel传出电子邮件配置,或者让提供商检查php的Mail()配置我今天遇到了这个问题,并惊讶地看到库开箱即用失败。似乎是CI电子邮件库中的一个问题:
/**
* 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));
}
然后让服务在App/Config/Services.php
中返回您的子类:
public static function email(bool $getShared=TRUE){
return $getShared ? static::getSharedInstance('email') : new \App\Libraries\Email();
}
从CI论坛获得解决方案 在我的病例中,从phpinfo()>intl>ICU版本4.6。太老了
将其更新到最新版本对我来说很有效。我已经通过Mail&Sendmail进行了检查。两者都不起作用。但是SMTP正在工作。但这不是我的解决办法。我想我不需要为此在cPanel上创建任何关联的电子邮件。对吗?这在Codeigniter 3中运行良好。为什么不在Codeigniter 4中?能否在composer.lock上共享Codeigniter的确切版本?我将尝试模拟这种行为,因为我使用的是我的作曲家的4.0.3。lockIt运行良好,没有在Codeigniter 3中安装任何东西。是否需要安装您提到的任何东西来发送Codeigniter 4?我在他们的文件上什么也没找到。
<?php namespace App\Libraries;
class Email extends \CodeIgniter\Email\Email{
protected function validateEmailForShell(&$email){
return TRUE;
}
}
public static function email(bool $getShared=TRUE){
return $getShared ? static::getSharedInstance('email') : new \App\Libraries\Email();
}