【问题标题】:Why casting null to array exits the code?为什么将 null 转换为数组会退出代码?
【发布时间】:2020-06-07 23:25:28
【问题描述】:
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
    {
        $failedRecipients = (array) $failedRecipients;

        if (!$this->transport->isStarted()) {
            $this->transport->start();
        }

        $sent = 0;

        try {
            $sent = $this->transport->send($message, $failedRecipients);
        } catch (Swift_RfcComplianceException $e) {
            foreach ($message->getTo() as $address => $name) {
                $failedRecipients[] = $address;
            }
        }

        return $sent;
    }

这是来自 vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php 的代码

首先它运行良好,我们还有另一个使用相同库的项目,它在该项目上运行良好。但是在一个项目上线

$failedRecipients = (array) $failedRecipients;

它没有给出任何错误,就像这一行将调用函数返回一样。所以电子邮件不会发送。 $failedRecipients 为空。

Php 版本是 7.2.26,它应该可以正常工作,在另一个项目上它是一样的。

更新

Zelay'da 要求的信息:

$message 是这样的:。

$address 将是电子邮件地址字符串,name 将为 null 代码出现在此处。我从 $message->getTo() 中获取了它们。传输对象是 vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SpoolTransport.php

完整的课程:

<?php

/*
 * This file is part of SwiftMailer.
 * (c) 2004-2009 Chris Corbyn
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Swift Mailer class.
 *
 * @author Chris Corbyn
 */
class Swift_Mailer
{
    /** The Transport used to send messages */
    private $transport;

    /**
     * Create a new Mailer using $transport for delivery.
     */
    public function __construct(Swift_Transport $transport)
    {
        $this->transport = $transport;
    }

    /**
     * Create a new class instance of one of the message services.
     *
     * For example 'mimepart' would create a 'message.mimepart' instance
     *
     * @param string $service
     *
     * @return object
     */
    public function createMessage($service = 'message')
    {
        return Swift_DependencyContainer::getInstance()
            ->lookup('message.'.$service);
    }

    /**
     * Send the given Message like it would be sent in a mail client.
     *
     * All recipients (with the exception of Bcc) will be able to see the other
     * recipients this message was sent to.
     *
     * Recipient/sender data will be retrieved from the Message object.
     *
     * The return value is the number of recipients who were accepted for
     * delivery.
     *
     * @param array $failedRecipients An array of failures by-reference
     *
     * @return int The number of successful recipients. Can be 0 which indicates failure
     */
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
    {
        $failedRecipients = (array) $failedRecipients;

        if (!$this->transport->isStarted()) {
            $this->transport->start();
        }

        $sent = 0;

        try {
            $sent = $this->transport->send($message, $failedRecipients);
        } catch (Swift_RfcComplianceException $e) {
            foreach ($message->getTo() as $address => $name) {
                $failedRecipients[] = $address;
            }
        }

        return $sent;
    }

    /**
     * Register a plugin using a known unique key (e.g. myPlugin).
     */
    public function registerPlugin(Swift_Events_EventListener $plugin)
    {
        $this->transport->registerPlugin($plugin);
    }

    /**
     * The Transport used to send messages.
     *
     * @return Swift_Transport
     */
    public function getTransport()
    {
        return $this->transport;
    }
}

【问题讨论】:

    标签: php docker symfony swiftmailer


    【解决方案1】:

    SwiftMailer 知道哪些收件人失败,但 Laravel 没有公开相关参数来帮助我们获取该信息:

    /**
     * Send the given Message like it would be sent in a mail client.
     *
     * All recipients (with the exception of Bcc) will be able to see the other
     * recipients this message was sent to.
     *
     * Recipient/sender data will be retrieved from the Message object.
     *
     * The return value is the number of recipients who were accepted for
     * delivery.
     *
     * @param array $failedRecipients An array of failures by-reference
     *
     * @return int The number of successful recipients. Can be 0 which indicates failure
     */
    
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
    {
        $failedRecipients = (array) $failedRecipients;
    
        // FIXME: to be removed in 7.0 (as transport must now start itself on send)
        if (!$this->transport->isStarted()) {
            $this->transport->start();
        }
    
        $sent = 0;
    
        try {
            $sent = $this->transport->send($message, $failedRecipients);
        } catch (Swift_RfcComplianceException $e) {
            foreach ($message->getTo() as $address => $name) {
                $failedRecipients[] = $address;
            }
        }
    
        return $sent;
    }
    

    您可以使用Mail::failures()

    访问失败的收件人以查找错误
    if(count(Mail::failures()) > 0){
                    //$errors = 'Failed to send confirmation email, please try again.';
                    $message = "Email not send";
                }
    return $message;
    

    这是完整的课程:https://github.com/swiftmailer/swiftmailer/blob/master/lib/classes/Swift/Mailer.php

    Swiftmailer 只负责将电子邮件交给邮件服务器。

    其他一切都与 Swiftmailer 无关,请参阅此处查看 Bounce Email handling with PHP ?

    【讨论】:

    • 但我不需要失败的收件人。我只想发邮件。我没有使用laravel,您提到的Mail类可能来自laravel?我正在使用 symfony。
    • 它没有给出任何错误,你不能在不知道错误的情况下解决你的问题!所以,返回$values作为响应,看看它们是否为空,然后尝试解决。
    • $values 是什么意思? send() 函数的输出?如果是这样 - 它返回 null。顺便说一句,我刚刚将同一个项目克隆到另一个目录并启动了 docker 并发送邮件工作。但它仍然没有回答为什么它在我的原始目录中不起作用的问题。有些人说它的 php 错误。
    • 我的意思是输出消息、名称等值。!您没有完整的课程,因此很难解释出了什么问题,但可能是 The Transport used to send messages 而不是 @return Swift_Transport 在课程结束时回答。很难说确切的错误。没有看到完整的课程。
    • 你应该使用正确的标签 symfony 和 docker。我编辑了你的问题。
    猜你喜欢
    • 2021-04-23
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多