php 在CI4中加载视图文件发送电子邮件

lb3vh1jj  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(78)

我的代码遇到了一个问题。我实际上是用View()加载视图的,但是它没有发送电子邮件,而是呈现了视图。我很困惑为什么会发生这种情况?这是我的代码。

namespace App\Libraries;

use App\Libraries\Options; // Loading Options Library

class CustomEmail
{
    private $email;
    private $option;

    public function setProtcols()
    {
        $this->option = new Options(); // Loading Options Library
        $this->option->load(); // Loading Options

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

        $config['charset']  = 'utf-8';
        $config['wordWrap'] = true;
        $config['mailType'] = 'html';

        if ($this->option->key->smtp_active == 1) {
            $config['protocol'] = 'smtp';
            $config['SMTPHost'] = $this->option->key->smtp_host;
            $config['SMTPUser'] = $this->option->key->smtp_username;
            $config['SMTPPass'] = $this->option->key->smtp_password;
            $config['SMTPPort'] = $this->option->key->smtp_port;
            $config['SMTPCrypto'] = $this->option->key->smtp_encryption;
        }

        $this->email->initialize($config);
    }

    public function sendMail($to, $subject, $data, $view = 'emails/default')
    {
        $this->setProtcols();
        $this->email->clear();

        $data = array_merge($data, [
            'base_url'                      => base_url(),
            'web_name'                      => $this->option->key->web_name,
            'address'                       => $this->option->key->address,
            'company_registration_number'   => $this->option->key->company_registration_number,
            'whatsapp_number'               => $this->option->key->whatsapp_number,
            'phone_number'                  => $this->option->key->phone,
        ]);

        $body = view($view, $data);
        
        $this->email->setFrom($this->option->key->email, $this->option->key->web_name);
        $this->email->setTo($to);
        $this->email->setSubject($subject);
        $this->email->setMessage($body);

        if ($this->email->send()) {
            return true;
        } else {
            return false;
        }
    }
}

预期输出是通过不呈现/显示视图来发送包含视图中HTML的电子邮件。

uqjltbpv

uqjltbpv1#

视图中有一个undefined变量,我刚刚定义了它,它就开始工作了。
这是没有留下任何错误日志的东西,即使在调试模式下,我也没有留下任何痕迹。
我试图查看最后一个响应,我得到的HTML代码!并在视图中间的某个地方结束,我只是转到该视图文件,看到那里有一个变量,我进一步检查了我的库,我知道我正在传递$phone,而在视图中我在视图中写入$number
我希望这对其他人有帮助。

相关问题