Reputation: 707
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.
Upvotes: 12
Views: 47793
Reputation: 578
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.
Upvotes: 0
Reputation: 1
i Had the same problem and i fixed the error.
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'),
],
Upvotes: 0
Reputation: 147
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
Upvotes: 0
Reputation: 1432
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]
Upvotes: 3
Reputation: 817
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.
Upvotes: 0
Reputation: 13527
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');
}
Upvotes: 2
Reputation: 388
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
Upvotes: 0
Reputation: 131
I solved this by replacing Global "From" Address in mail.php file to the same input at .env for MAIL_USERNAME=
.
Upvotes: 13
Reputation: 1428
Check the mail addres in config/mail.php file. Change it to something other than gmail.
Upvotes: 0
Reputation: 7843
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:
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.
If your account has 2-step-verificaton enabled, you'd need an App Password for SMTP login.
Upvotes: 4