10

Every time I submit the contact form on my Laravel application I receive the error message in the title. I've followed the recommendation in this discussion, but it has had no effect even after php artisan cache:clear and php artisan config:cache. Here's the relevant code:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl

config/mail.php

<?php

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.gmail.org'),
    'port' => env('MAIL_PORT', 587),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),    
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

I was under the impression from the documentation that the global 'from' wouldn't fire unless no other from address was provided, but in my controller for the mail, I specified the address supplied to the contact form as the 'from,' is that a conflict point somehow? It doesn't seem to be from the error message details.

Because the contact form is not a distinct view but the bottom of the mainpage view, the Controller function lives in PageController

public function postContact(Request $request) {
    $this->validate($request, [

      'email' => 'required|email',
      'subject' => 'required|min:3',
      'message' => 'required|min:10'
    ]);

    $data = array(
      'email' => $request->email,
      'subject' => $request->subject,
      'mailbody' => $request->message
    );

    Mail::send('emails.contact', $data, function($message) use ($data) {
      $message->from($data['email']);
      $message->to('username@gmail.com');
      $message->subject($data['subject']);
    });
  }
Community
  • 1
  • 1
user968270
  • 4,251
  • 5
  • 21
  • 20
  • change the port from 465 to 587 and MAIL_ENCRYPTION= ssl. Also allow google app less secure from google settings. – Gaurav Apr 04 '17 at 03:37
  • This got rid of the old error but now I'm getting "Swift_TransportException in StreamBuffer.php line 268: Connection could not be established with host smtp.gmail.com [ #0]" – user968270 Apr 05 '17 at 16:31
  • Check this answer http://stackoverflow.com/questions/32693528/expected-response-code-220-but-got-code-with-message-in-laravel?noredirect=1&lq=1 – Meera Apr 06 '17 at 11:10
  • That is in fact the answer I link in the original question above as having read it, applied the suggestions, and not having them work. – user968270 Apr 06 '17 at 12:44

4 Answers4

6

I think you should define mail sender (MAIL_FROM_ADDRESS) as your gmail which you want to use send emails with.

for example, if your MAIL_USERNAME at .env is example@gmail.com you should define your MAIL_FROM_ADDRESS (or of course $mail->from()) as example@gmail.com.

I don't think gmail allows you send emails as another user (another address).

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
Uğur Arıcı
  • 1,180
  • 1
  • 10
  • 16
1

Similar problem 587 port

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=mypassword
Takamura
  • 302
  • 1
  • 4
  • 16
0
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=address@gmail.com
MAIL_PASSWORD=secreat
MAIL_ENCRYPTION=ssl

Update above code in Your .env file and then run these commands in terminal

  1. composer dump-autoload

  2. php artisan serve

sparsh turkane
  • 1,165
  • 12
  • 13
0

I was getting the same error.. It is working fine when i changed EMAIL_ENCRYPTION It was 'encryption' => env('MAIL_ENCRYPTION', 'tps'), before I change it to 'encryption' => env('MAIL_ENCRYPTION', ''),

Pyuri Sahu
  • 449
  • 4
  • 4