繁体   English   中英

CodeIgnititer 4:无法使用 PHP SMTP 发送 email

[英]CodeIgnititer 4: Unable to send email using PHP SMTP

我阅读了与该问题相关的所有其他答案,但没有一个有帮助。

当我尝试在本地主机生产服务器上运行以下设置时,我收到以下错误消息:

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

我安装了 CodeIgniter 4 并将以下内容添加到.env

email.production.protocol = smtp
email.production.SMTPHost = my.server.com
email.production.SMTPUser = My@Mail.com
email.production.SMTPPass = MyPassword
email.production.SMTPCrypto = ssl
email.production.SMTPPort = 465
email.production.SMTPFromName = "Foo Bar"

对于端口465587和加密ssltsl ,我尝试了所有可能的选项。 app/Config/Email.php中设置public $newline = "\r\n"; 已经设置(建议来自这里

我成功地能够运行

telnet my.server.com 465
telnet my.server.com 587

然后我将以下代码添加到app/Config/Email.php的末尾:

public function __construct()
    {
        $this->protocol = $_ENV['email.production.protocol'];
        $this->SMTPHost = $_ENV['email.production.SMTPHost'];
        $this->SMTPUser = $_ENV['email.production.SMTPUser'];
        $this->SMTPPass = $_ENV['email.production.SMTPPass'];
        $this->SMTPPort = $_ENV['email.production.SMTPPort'];
        $this->SMTPCrypto = $_ENV['email.production.SMTPCrypto'];
        $this->fromEmail = $_ENV['email.production.SMTPUser'];
        $this->fromName = $_ENV['email.production.SMTPFromName'];
    }

在我的 Controller 中,我添加了一个 function :

$email = \Config\Services::email();
$email->setSubject("Test");
$email->setMessage("Test");
$email->setTo("myaddress@example.com");
if ($email->send(false)) {
    return $this->getResponse([
        'message' => 'Email successfully send',
    ]);
} else {
    return $this
        ->getResponse(
            ["error" => $email->printDebugger()],
            ResponseInterface::HTTP_CONFLICT
        );
}

调用此 function 会产生上述错误消息。 我假设这与错误消息描述的服务器配置无关,因为这是在本地主机和生产环境中发生的。

更新:这一定与 CI 设置有关。 无论我尝试使用什么服务器,即使使用完全不正确的值(例如不正确的密码),错误也完全相同。

我通常使用 smtp gmail 为我的客户发送 email。 “通过 smtp gmail 发送 email”最重要的是您必须更新您的 Gmail 安全规则:

  1. 在您的 Gmail 帐户中,点击管理您的 Google 帐户
  2. 单击选项卡安全
  3. 然后,将“安全性较低的应用程序访问权限”设置为“开启”

之后,您可以像这样设置“app\config\EMail.php”:

    public $protocol = 'smtp';
    public $SMTPHost = 'smtp.gmail.com';
    public $SMTPUser = 'your.googleaccount@gmail.com';
    public $SMTPPass = 'yourpassword';
    public $SMTPPort = 465;
    public $SMTPCrypto = 'ssl';
    public $mailType = 'html';

最后,您可以像这样在 controller 上创建 sendEmai function:

$email = \Config\Services::email();

$email->setFrom('emailsender@gmail.com', 'Mr Sender');
$email->setTo('emailreceiver@gmail.com');


$email->setSubject('Test Subject');
$email->setMessage('Test My SMTP');

if (!$email->send()) {

    return false;

 }else{

    return true;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM