0
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;
    }

This is a code from vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php

It worked fine first, also we have another project where same library is used and it works fine on that project. But on one project on line

$failedRecipients = (array) $failedRecipients;

it does not give any error, just acts as if this line would call return from function. And so email is not sent. $failedRecipients is null.

Php version is 7.2.26 and it should work fine on it, on another project it is same.

Update

Info requested by Zelay'da:

$message is this:. enter image description here

$address would be email address string, name would be null it code comes there. I have taken them from $message->getTo(). Transport object is vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/SpoolTransport.php

Complete class:

<?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;
    }
}
Darius.V
  • 737
  • 1
  • 13
  • 29

1 Answers1

0

SwiftMailer knows which recipients failed, but Laravel not exposing the related parameter to help us get that information:

/**
 * 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;
}

You can access to failed recipients for errors using Mail::failures()

if(count(Mail::failures()) > 0){
                //$errors = 'Failed to send confirmation email, please try again.';
                $message = "Email not send";
            }
return $message;

Here is complete class :https://github.com/swiftmailer/swiftmailer/blob/master/lib/classes/Swift/Mailer.php

Swiftmailer only takes care to hand the email over to the mail-server.

Everything else is not related to Swiftmailer, See here for Bounce Email handling with PHP ?

  • But I dont need failed recipients. I just want to send email. I am not using laravel, Mail class you mentioned is probably from laravel? I am using symfony. – Darius.V Feb 24 '20 at 12:16
  • *it does not give any error*, You cant solve your problem without knowing error ! so, return `$values` in response and see if they are empty or not, then try to solve. –  Feb 24 '20 at 12:18
  • By $values what do you mean? Output of send() function? If so - it returns null. Btw I just cloned same project to another directory and launched docker and sending mail works. But it still does not answer the question why it does not work in my original directory. Some guys say its php bug. – Darius.V Feb 24 '20 at 12:36
  • I meant output of values like message, name etc.. ! You dont have your complete class so its difficult to explain what was wrong but probably was *The Transport used to send messages* and wasnt *@return Swift_Transport* at the end of class in answer. hard to say exact error. without seeing complete class you have. –  Feb 24 '20 at 12:41
  • You should use correct tags symfony and docker. I edited your question. –  Feb 24 '20 at 13:03
  • Updated question – Darius.V Feb 24 '20 at 16:12
  • Class you added to question is the one in answer ! you should add your own class not the correct one I linked in answer. *$failedRecipients is null* means you dont pass parameters (values) to class. –  Mar 05 '20 at 09:01
  • You mean I should add class which calls send() function? I know that $failedRecipients is null, I dont pass parameter. It is not required parameter, so I dont pass it. But it should still work. And it works on other projects. And even same project - I reinstalled it in different folder and it works :D – Darius.V Mar 07 '20 at 09:39