Expected response code 250 but got code "550" Laravel swift mailer

Viewed 3928

I can send email from tinker but when I try to send email from code in laravel framework I get below error:

Swift_TransportException (550)
Expected response code 250 but got code "550", with message "550 Bad HELO - Host impersonating domain name [mydomain.com] "

This is my simple mail method to test sending email from laravel controller :

  Mail::send('errors.401', [], function ($message) { $message->to('my.email@gmail.com')->subject('this works!'); });

.env file:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=587
MAIL_USERNAME=info@mydomain.com
MAIL_PASSWORD=<password>
MAIL_ENCRYPTION=

I've done below steps but still it doesn't work:

composer dump-autoload
php artisan view:clear
php artisan cache:clear
php artisan config:cache

Update:

My code is on an ubuntu server which I configured dns server and bind9 to connect domain to server and I have an external mail server to handle emails.

So whenever an email request is created in ubuntu server, it'll forward it to external mail server.

where should I look for problem?

2 Answers

I've Fixed the problem after wasting lots of time, hope this answer stop wasting others time.

it was due to local domain name, we need to define a local name in our mail.php config file which makes our server not to confuse with external server domain name.

 'local_domain' => env('MAIL_HOST', 'mail.mydomain.com'), 

In my case I was using the method ->from($this->user->email, $this->user->name); on the MailMessage instance within the toMail function of my notification class.

public function toMail($notifiable)
{    
    return (new MailMessage)
        ->markdown('admin.email.feedback', [
            'user' => $this->user,
            'farms' => $farm_names,
            'message' => $this->message,
            'time' => $this->time])
        ->subject("Customer feedback")
        ->from($this->user->email, $this->user->name); // from
}

Since the default sending mail is setup in the config.mail and there is no password setup for the customer email address I received error:

Swift_TransportExceptionExpected response code 250 but got code "550", with message "550 Email Rejected, Invalid From Address - gmail.com"

Two solutions:
1] Remove from method and replace with replyTo method if I want to use default config.mail info.
2] Setup sending mail from customer account smtp settings by saving their email details.

Related