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.
I solved this by replacing Global "From" Address in mail.php file to the same input at .env for MAIL_USERNAME=
.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With