How do we solve error 554 5.5.1 (no valid recipients) when using PHP Swiftmailer?
Asked Answered
E

7

10

When testing out our mail server we stumbled accross an error that prevents us from sending mails via PHP, though regular sending/receiving per Mail-in-a-box works without any problems. We are running a separate Ubuntu 18.04 server that only has Mail-in-a-box with all its needed components running.

Output in the error.log text file

PHP Fatal error: Uncaught Swift_TransportException: Expected response code 354 but got code "554", with message "554 5.5.1 Error: no valid recipients\r\n"

PHP file

$request_email = $_POST['request-email'];
$request_name = $_POST['request-name'];
$request_text = $_POST['request-text'];

$transport = (new Swift_SmtpTransport('data.abc.xy', 587, 'tls'))
    ->setUsername('[email protected]')
    ->setPassword('*******')
    ->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));

$mailer = (new Swift_Mailer($transport));

$message = (new Swift_Message('Name: '. $request_name))
    ->setFrom(['' . $request_email => '' . $request_name])
    ->setTo(['[email protected]'])
    ->setBody('E-Mail: ' . $request_email . $request_text)
    ->setContentType("text/html");

$result = $mailer->send($message);

What we have tried is to reinstall all of Mail-in-a-box and all of the components and checking everything for spelling mistakes. The ricipient does exist on our mail server and can receive and send mails manually via the client.

Eads answered 10/2, 2019 at 13:6 Comment(1)
Also check MAIL_USERNAME and MAIL_FROM_ADDRESS both refer to same domain.Arbour
C
12

The 554 5.5.1 error is the SMTP error "no valid recipients". It can occur if you've misspelled the recipient addresses but it can also occur if you are not properly authenticating to the outgoing server.

So the problem is that abc.xy is not a registered domain so you can't send an email to this address. I think it's not something related to your code.

You can catch the Swift_TransportException error and handle it in your own codebase like :

try {
    $result = $mailer->send($message);
} 
catch (Swift_TransportException $e) {
    echo $e->getMessage();
}
Carton answered 10/2, 2019 at 13:48 Comment(3)
Bad authentication! I was sending emails through a server, which would do the authentication based on source IP and the existence of the From-address. I had a bad From-address and was therefore not considered authenticated.Cameroncameroon
I have the same issue and for me it seems it's caused by accented characters in the sender's nameStowage
I also got same error, however after enabling SMTP logging, it turned out, that server IP address was blacklisted by spam filter: 554 5.7.1 Service unavailable; Client host [<redacted ip>] blocked using b.barracudacentral.org; http://www.barracudanetworks.com/reputation/?pr=1&ip=Azotobacter
L
18

I have solved this error (554) just adding to laravel (.env file) these 2 lines:

[email protected]
MAIL_FROM_NAME="[email protected]"

Finally, run this command to flush mail cache config:

php artisan config:cache
Leffler answered 25/6, 2019 at 9:50 Comment(0)
C
12

The 554 5.5.1 error is the SMTP error "no valid recipients". It can occur if you've misspelled the recipient addresses but it can also occur if you are not properly authenticating to the outgoing server.

So the problem is that abc.xy is not a registered domain so you can't send an email to this address. I think it's not something related to your code.

You can catch the Swift_TransportException error and handle it in your own codebase like :

try {
    $result = $mailer->send($message);
} 
catch (Swift_TransportException $e) {
    echo $e->getMessage();
}
Carton answered 10/2, 2019 at 13:48 Comment(3)
Bad authentication! I was sending emails through a server, which would do the authentication based on source IP and the existence of the From-address. I had a bad From-address and was therefore not considered authenticated.Cameroncameroon
I have the same issue and for me it seems it's caused by accented characters in the sender's nameStowage
I also got same error, however after enabling SMTP logging, it turned out, that server IP address was blacklisted by spam filter: 554 5.7.1 Service unavailable; Client host [<redacted ip>] blocked using b.barracudacentral.org; http://www.barracudanetworks.com/reputation/?pr=1&ip=Azotobacter
M
6

I had the same problem in Laravel and fixed it by changing "from" to "replyTo". I believe the problem is when sender's and app server's domains differs.

// in Laravel
// ...
namespace App\Notifications;
// ...
    public function __construct(User $from, string $body)
    {
        $this->from = $from;
        $this->body = $body;
    }
// ...
    public function toMail($notifiable)
    {
        $message = new MailMessage;
        $message->subject('some subject');

        // "no valid recipients" error:
        // $message->from($this->from->email, $this->from->name_full);
        
        // works:
        // sender globally set in .env (MAIL_FROM_NAME, MAIL_FROM_ADDRESS)
        $message->replyTo($this->from->email, $this->from->name_full);
        
        $message->line(new HtmlString(nl2br($this->body)));

        return $message;
    }
// Swiftmailer respectively
// ...
$message = (new Swift_Message('Name: '. $request_name))
    ->setFrom(['[email protected]' => 'contact'])
    ->setReplyTo([$request_email => $request_name])
// ...
Meras answered 25/9, 2020 at 8:32 Comment(1)
Damn, I'm trying to fix this for ages and now I just reached your answer which solved it in 2 seconds... Thanks a lot mate 👍Mangosteen
G
3

I solved this issue by ensuring that the email address set in env file

[email protected]

is the same as the "from" address in my custom notification mail class' toMail function

->from('[email protected]','My app name')

It seems as though some email services find it odd that the app is sending an email using one credential (env credentials) but a different email address is being masked as the sender.

A better solution would probably be to never set a "from" address in your notification classes.

Gazzo answered 6/12, 2020 at 23:17 Comment(0)
V
0

One more thing is that it might be everything OK with your code, but the receiver address simply doesn't exist. We have a lot of customers from other companies and with time people simply change their job and the email address they used is deleted

"receiver email address doesn't exist anymore"

so if you try to send an email to non existing email address you will get that same error.

Vibrations answered 20/1, 2023 at 8:12 Comment(0)
A
0

in my case, the MAIL_FROM_NAME value in the .env file is different from 'name' in my laravel code. Ex:

when i type '[email protected]' or text (ex: 'MAIL SENDER NAME' ) instead of '[email protected]', it work fine!

Atonality answered 28/7, 2023 at 0:57 Comment(0)
T
-1

In my case, I got this error because I had forgotten to set the MX record on the recipient's domain, so the app didn't know where to deliver the email.

Another reason can be the domain being blacklisted, check spamhaus

Tame answered 17/9, 2021 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.