9

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('contact@abc.xy')
    ->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(['contact@abc.xy'])
    ->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.

Calendula
  • 93
  • 1
  • 1
  • 8

7 Answers7

18

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

MAIL_FROM_ADDRESS=you@email.com
MAIL_FROM_NAME="you@email.com"

Finally, run this command to flush mail cache config:

php artisan config:cache
gtamborero
  • 2,898
  • 27
  • 28
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();
}
Amine KOUIS
  • 1,686
  • 11
  • 12
  • 2
    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. – Chris Jul 11 '19 at 14:42
  • I have the same issue and for me it seems it's caused by accented characters in the sender's name – Tofandel Jul 27 '20 at 10:22
  • 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 [] blocked using b.barracudacentral.org; http://www.barracudanetworks.com/reputation/?pr=1&ip=` – Andis Oct 05 '22 at 16:35
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(['contact@abc.xy' => 'contact'])
    ->setReplyTo([$request_email => $request_name])
// ...
Adam Ducho
  • 61
  • 1
  • 4
  • 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 – ylerjen Jun 20 '21 at 20:30
3

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

MAIL_USERNAME=example@example.com

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

->from('example@example.com','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.

Oludre
  • 134
  • 6
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.

lewis4u
  • 14,256
  • 18
  • 107
  • 148
0

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

  • in .env MAIL_FROM_NAME=mymail@dot.com

  • in my Laravel code

    $m->from('mymail@dot.com', 'myOther@mail.com');

when i type 'mymail@dot.com' or text (ex: 'MAIL SENDER NAME' ) instead of 'myOther@mail.com', it work fine!

hamil.Dev
  • 137
  • 2
  • 9
-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

the_nuts
  • 5,634
  • 1
  • 36
  • 68