【问题标题】:How to fix Allowed memory size exhausted in Laravel Swiftmailer?如何修复 Laravel Swiftmailer 中耗尽的允许内存大小?
【发布时间】:2020-03-31 12:00:24
【问题描述】:

你好,我有这个问题。 我将 Laravel 项目从 5.4 更新到 5.7。 在我无法再发送邮件之后。我确保我有正确的 Swiftmailer 包和所有版本。 经过一番研究,我发现我的应用程序在函数 addContent() 中的 Mailer.php 处崩溃。

protected function addContent($message, $view, $plain, $raw, $data)
    {
        if (isset($view)) {
            /* At this Point my application is crashing.
            $message->setBody($this->renderView($view, $data), 'text/html');
            */
        }

        if (isset($plain)) {
            $method = isset($view) ? 'addPart' : 'setBody';

            $message->$method($this->renderView($plain, $data), 'text/plain');
        }

        if (isset($raw)) {
            $method = (isset($view) || isset($plain)) ? 'addPart' : 'setBody';

            $message->$method($raw, 'text/plain');
        }
    }

我的 php 错误日志显示此错误消息:

"PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 32768 bytes) in C:\\webroot\\projectname\\vendor\\symfony\\debug\\Exception\\FatalErrorException.php on line 1, referer: http://localhost/intranet/projectname/other-applications/create"

谁有一些技巧可以解决我的问题?

【问题讨论】:

  • 首先要弄清楚问题实际上是出在 Swift Mailer 上,还是出在您尝试在那里渲染的模板上 - 如果您在那个地方只使用 $foo = $this->renderView($view, $data);,它还会崩溃吗?
  • @Cbroe 如果我尝试这样做,它仍然会崩溃if (isset($view)) { $foo = $this->renderView($view, $data); echo "HEre"; dd($view); }

标签: php laravel swiftmailer


【解决方案1】:

经过更多研究,我发现它似乎在我构建消息的控制器中崩溃了。

这是我的控制器:

<?php

namespace App\Mail;

use App\Models\Application;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ApplicationCreated extends Mailable
{
    use Queueable, SerializesModels;

    public $isForSecretary;
    public $isForHeadPhysician;
    public $isForManagement;

    public $application;
    public $customMsg;

    /**
     * ApplicationCreated constructor.
     * @param Application $application
     * @param $isForSecretary
     * @param $isForHeadPhysician
     * @param null $customMsg
     */
    public function __construct(Application $application, $isForSecretary, $isForHeadPhysician, $customMsg = null)
    {
        $this->application = $application;
        $this->isForSecretary = $isForSecretary;
        $this->isForHeadPhysician = $isForHeadPhysician;
        $this->customMsg = $customMsg;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

        $applicant = $this->application->applicant;

        return $this->view('application.responses.emails.application_created')
            ->subject('Reiseantrag: ' . $applicant->title . ' ' . $applicant->lastname)
            ->withApplication($this->application)
            ->withMail($this)
            ->with('customMsg', $this->customMsg);
    }
}

在构建函数中,它似乎在 return 语句的某个地方崩溃了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2020-06-25
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多