为什么将null强制转换为数组会退出代码?

问题描述 投票:0回答:1
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;
    }

这是来自供应商/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php的代码

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

$failedRecipients = (array) $failedRecipients;

它不会产生任何错误,只是像该行将调用函数返回一样。因此,不会发送电子邮件。 $ failedRecipients为空。

Php版本是7.2.26,应该可以正常使用,在另一个项目上也相同。

更新

Zelay'da要求的信息:

$ message是这样的:。enter image description here

$ address将是电子邮件地址字符串,名称将为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个回答
0
投票

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 ?

© www.soinside.com 2019 - 2024. All rights reserved.