Expected response code 354 but got code "503"
Asked Answered
T

10

12

I am trying to create a Contact Us Form in a Laravel Project but ran into the following error and would like to know how to solve this.

Expected response code 354 but got code "503", with message "503-All RCPT commands were rejected with this error: 503-"Your IP: 202.133.88.147 : Your domain gmail.com is not allowed in header 503-From" 503 Valid RCPT command must precede DATA "

The following is my .ENV file

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=26
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=null

Controller

  public function contactus()
    {
        return view('contactus');
    }

    public function sendContactMail(Request $request)
    {
        Mail::to('[email protected]')->send(new ContactUs($request));
        Session::flash('success','Message Sent Successfully!');
        return redirect()->back();
    }

ContactUs.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactUs extends Mailable
{
    protected $contactdata;
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(\Illuminate\Http\Request $request)
    {
        $this->contactdata = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->contactdata->email)
                    ->subject($this->contactdata->subject)
                    ->with([
                        'message'   =>  $this->contactdata->message,
                        'fullname'  =>  $this->contactdata->first_name.' '.$this->contactdata->last_name
                    ])
                    ->markdown('emails.contactus');
    }
}

Thanks in advance for the help.

Teresiateresina answered 19/9, 2019 at 21:41 Comment(10)
Did you double check the smtp port? The default one without encryption is 25Debase
You're not allowed to send mail from an @gmail account from anything other than gmail.Eagan
@I.Antonov: The error "Error 503: Valid RCPT command must precede DATA" usually happens when your SMTP (outgoing mail) connection was not properly authenticated by your email server. As @Debase put it, you're probably setting your port number wrong. Also I think your MAIL_ENCRYPTION setting should be MAIL_ENCRYPTION= instead of MAIL_ENCRYPTION=null.Voluminous
@Debase i tried smtp port 25, still same result.Teresiateresina
@KoalaYeung tried MAIL_ENCRYPTION=null, same resultTeresiateresina
@I-Antonov: Did you try MAIL_ENCRYPTION=?Voluminous
@KoalaYeung my mistake, i tried MAIL_ENCRYPTION= also. same result.Teresiateresina
Do you have a copy of the valid SMTP config from your vendor? Can you show us?Voluminous
Sorry, missed the point that you're sending email through Gmail. I think you need to reference their documentation about encryption and port. You'll probably need to use 587 port with tls encryption. Also your account needs to be either a Gmail account or a GSuite email account.Voluminous
Let us continue this discussion in chat.Teresiateresina
M
13

I solved this by replacing Global "From" Address in mail.php file to the same input at .env for MAIL_USERNAME= .

Mailable answered 1/12, 2019 at 20:48 Comment(0)
V
4

From the error message, you're probably sending email through Gmail's SMTP service.

Your .env should look like this:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

Also you need to make sure:

  1. Your domain mydomain.com is properly setup to use GSuite's Gmail. You cannot setup Gmail's SMTP to send email of domains that are not in GSuite nor is a Gmail account.

  2. If your account has 2-step-verificaton enabled, you'd need an App Password for SMTP login.

Voluminous answered 20/9, 2019 at 4:22 Comment(0)
M
3

In my case I fixed the issue by assigning MAIL_FROM_ADDRESS equal to MAIL_USERNAME. I don't know the explanation though.

MAIL_MAILER=smtp
MAIL_HOST=mail.domain.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl
[email protected]
Melisamelisande answered 31/7, 2021 at 8:15 Comment(0)
E
2

For me, the solution was this:

After correctly setting up your SMTP or mail driver of choice. Sending a mail with a "mail-from" different from your hostname e.g "[email protected]" and your hostname is yourhost.com The mail will always go through, however, sending a mail with a "mail-from" as such: "[email protected]" which is possibly different from your .env mail authentication i.e "MAIL_USERNAME" : This will throw an error if that mail account hasn't been set up on your hosted server or c-panel.

My .env file had:

[email protected]

But my code had:

public function build()
{
    return
      $this
        ->from('[email protected]')
        ->view('emails.clientNotification');
}

This fixed it:

public function build()
{
    return
      $this
        ->from(env('MAIL_FROM_ADDRESS'))
        ->view('emails.clientNotification');
}
Elwell answered 21/8, 2020 at 12:11 Comment(0)
R
0

Check the mail addres in config/mail.php file. Change it to something other than gmail.

Reavis answered 20/9, 2019 at 7:7 Comment(0)
E
0

Check the config/mail.php and makes sure all the variables are correctly defined in you .env file. For example if you mail.php has the code:

    'from' => [
         'address' => env('MAIL_FROM_ADDRESS'),
         'name' => env('MAIL_FROM_NAME'),
    ],

and the MAIL_FROM_ADDRESS is not defined in .env then it may give such an error

Enfranchise answered 20/8, 2020 at 20:16 Comment(0)
L
0

In my case, the error occurred because of a case-sensitive issue of a variable in the env file.

The names of environment variables are case-sensitive.

In my .env file I have a variable, mail_username=******* but in the mail.php file mail_username variable accessed in uppercase like this 'username' => env('MAIL_USERNAME'). That's why this error occurred.

By convention, environment variables are in uppercase.
Levenson answered 4/11, 2020 at 9:46 Comment(0)
I
0

TL:DR - Sending mail to an email that does not exist on your domain could cause the error.

None of the above is related to my own case. The cause of my problem was that I was sending an email to internal an account that does not exist.

my env configuration used [email protected] and am trying to send mail to [email protected] that does not exist.

So when I remove the user with [email protected] from the list, the error gets fixed

Interatomic answered 10/8, 2021 at 7:22 Comment(0)
T
0

i Had the same problem and i fixed the error.

  1. what you have to do is use the same email set up by your server. for example you cant user from gmail coz gmail isn't your domain on your server.
  • you have to define like so.

       public function build()
    
    
    {
      return $this->from('[email protected]')}
    

-or 2. you can remove the (from) everywhere you put it and just add this ([email protected]) to you .env file and then edit your config/mail.php Like So

  'from' => [
     'address' => env('MAIL_FROM_ADDRESS'),
     'name' => env('MAIL_FROM_NAME'),
],
Tellurium answered 26/3, 2022 at 19:55 Comment(0)
G
0

This error also appears if you are using MAIL_DRIVER=mailgun on a 7.x Laravel and below. Use MAIL_MAILER=mailgun until you upgrade.

Gaiter answered 7/8, 2023 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.