【问题标题】:Laravel pretend is not showing cc mail addressLaravel 假装没有显示抄送邮件地址
【发布时间】:2015-01-21 01:47:14
【问题描述】:

我在邮件配置中将pretend 更改为true。请查看http://laravel.com/docs/4.2/mail#mail-and-local-development

现在日志显示到这样的地址。

[2014-11-22 17:12:49] production.INFO: Pretending to mail message to: dinukathilanga@gmail.com, bbelekkaya@gmail.com [] []

但我也需要调试抄送电子邮件。我该怎么做?

这是我的代码。

Mail::send($view, $data, function($message) use ($to, $cc, $subject)
{
    foreach ($to as $toUser) {
        $message->to($toUser['email'], $toUser['first_name'] . ' ' . $toUser['last_name']);
    }

    foreach ($cc as $ccUser) {
        $message->cc($ccUser['email'], $ccUser['first_name'] . ' ' . $ccUser['last_name']);
    }

    $message->subject($subject);
});

【问题讨论】:

    标签: php email laravel laravel-4


    【解决方案1】:

    为了这个例子,创建一个名为 ExtendedMailer 的新类,并将文件保存在自动加载器能够找到的位置。根据您放置文件的位置,您可能需要在保存文件后运行composer dump-autoload

    <?php
    
    use Illuminate\Mail\Mailer;
    
    class ExtendedMailer extends Mailer
    {
        protected function logMessage($message)
        {
            parent::logMessage($message);
    
            $emails = implode(', ', array_keys((array) $message->getCc()));
    
            $this->logger->info("Pretending to mail message to: {$emails}");
        }
    }
    

    在您的应用程序能够加载类的地方创建一个新的服务提供者。如上,你可能需要运行composer dump-autoload

    下面的代码只是扩展了原来的 MailServiceProvider 但允许我们在 IoC 中绑定一个不同的类,你会注意到 new ExtendedMailer;我们之前创建的类。显然,如果您为类命名,请在此处反映该更改。

    <?php
    
    use Illuminate\Mail\MailServiceProvider;
    
    class ExtendedMailServiceProvider extends MailServiceProvider
    {
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register()
        {
            $me = $this;
    
            $this->app->bindShared('mailer', function($app) use ($me)
            {
                $me->registerSwiftMailer();
    
                // Once we have create the mailer instance, we will set a container instance
                // on the mailer. This allows us to resolve mailer classes via containers
                // for maximum testability on said classes instead of passing Closures.
                $mailer = new ExtendedMailer(
                    $app['view'], $app['swift.mailer'], $app['events']
                );
    
                $this->setMailerDependencies($mailer, $app);
    
                // If a "from" address is set, we will set it on the mailer so that all mail
                // messages sent by the applications will utilize the same "from" address
                // on each one, which makes the developer's life a lot more convenient.
                $from = $app['config']['mail.from'];
    
                if (is_array($from) && isset($from['address']))
                {
                    $mailer->alwaysFrom($from['address'], $from['name']);
                }
    
                // Here we will determine if the mailer should be in "pretend" mode for this
                // environment, which will simply write out e-mail to the logs instead of
                // sending it over the web, which is useful for local dev environments.
                $pretend = $app['config']->get('mail.pretend', false);
    
                $mailer->pretend($pretend);
    
                return $mailer;
            });
        }
    }
    

    在你的 config/app.php 中,你会发现一行看起来像

    'Illuminate\Mail\MailServiceProvider',
    

    您需要将其注释掉并添加如下一行

    'ExtendedMailServiceProvider',
    

    这样做的目的是将 Laravel 知道的邮件程序替换为您刚刚创建的邮件程序。您刚刚创建的与默认的相同,因为它只是对其进行了扩展,并向 logMessage 函数添加了功能。

    【讨论】:

    • 我通过链接改进问题。我只想使用假装配置。无需使用太多代码来检查 cc 值。
    • 我了解到您想使用伪装值。您会注意到框架内将数据输出到日志的方法根本不与抄送电子邮件交互。请参阅/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php 并查找方法logMessage()。之所以需要上面的代码是因为它允许您扩展框架,并将抄送电子邮件的日志记录添加到方法中
    【解决方案2】:

    请允许我在这里坦率地说。我是 Laravel 的新手,但我会尽力解释这一点。

    不过,我确实知道如何调试电子邮件平台,最简单的过程是通过 C 进行调试。

    我会尽量简洁。

    首先,尝试使用laravel-debugbar(最新版本为4)。
    这是一个能够过滤 PHP 调试栏的应用程序。
    通过使用这个应用程序,您将能够附加和编辑输出函数(这里是更多信息的链接:Debug bar

    如果这不起作用,请尝试通过 C 进行调试。

    首先,您将通过插入调试选项option -g来编译C程序。
    之后,您将 gdb 启动到平台中。
    第三,您将在 C 程序中打断点,具体来说,插入 break line_number 函数。
    之后,您将在 gdp 调试器中打印变量值。

    例如,您将键入诸如 print j(gdp) pi 之类的命令(我将发布获得此信息的网站;它将为您提供更广泛的演练)。

    这个过程有多种操作。
    我强烈建议您访问:Debug C program using gdb。希望这有帮助。

    【讨论】:

    • 感谢您的回复。但我只需要在邮件配置中使用伪装值。
    • 你的意思是你需要找到插入加密的值/函数,对吧?
    猜你喜欢
    • 2013-08-15
    • 2015-01-02
    • 2016-05-30
    • 2017-01-03
    • 1970-01-01
    • 2021-04-29
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多