Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MrRobot21's avatar

Problem Sending mail

Hi Awesome People

I'm trying to send email in my application, but i'm getting the following error

Swift_TransportException in StreamBuffer.php line 269:
Connection could not be established with host smtp.gmail.com [A connection attempt failed because the 
connected party did not properly respond after a period of time, or established connection failed because 
connected host has failed to respond. #10060]

My configration in ".env" is as follows

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=null

And My Controller is "AvoContactController.php"

<?php

namespace App\Http\Controllers;

use View;
Use Mail;
use App\AvoContact;
use App\Http\Requests;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Routing\Controller as BaseController;


class AvoContactController extends BaseController {

    public function contactpage()
    {
        $contact = AvoContact::all();

        return view('pages.contact')->with(['contact' => $contact]);
    }

    public function store(ContactFormRequest $request)
    {
        Mail::send('emails.contact', [], function ($m) use($request){
            $m
              ->from($request->get('email'))
              ->to('sanjiarya2112@gmail.com')
              ->subject('Your Reminder!');
        });

        return 'Email Sent';

    }  

}

I did tried installing "guzzle" But it din't help i'm still getting the same error

Looking forward for much needed help @bashy @bobbybouwmann @pmall @mstnorris @willvincent @snapey

Thank You

0 likes
27 replies
petrit's avatar

Try this,

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

This works to me.

1 like
MrRobot21's avatar

@petrit Thank You for your response

but i'm still getting the same error, i just couldn't figure how this exactly works

Actually what is the purpose of setting user name and password in ".env" is it for sending from my gmail to any other mail

In my case i have a contact form where user enters his mail and message and i'm getting that mail id in my controller for "from adress",

when user send the message it should come to my gmail inbox

// My controller 
    public function store(ContactFormRequest $request)
    {
        Mail::send('emails.contact', [], function ($m) use($request){
            $m
              ->from($request->get('email'))  //getting user mail from form
              ->to('sanjiarya2112@gmail.com')  //message to be sent to this id
              ->subject('Your Reminder!');
        });

        return 'Email Sent';

    }  

I'm really not getting the flow, could you please point me in the right direction

Thanks for your time

MrRobot21's avatar

@MaverickChan Thanks for your response

i did checked 4 times everything seems fine, my doubt is

Am i in the right path, i meant the way i'm doing, any suggestion could be helpful

Thank You

petrit's avatar

You cannot use smtp for sending mails to sanjiarya2112@gmail.com, "from: written email from form". Smtp server requires authentication to your account, in this case to google account. That means that every sent mail will be sent from smtp authenticated account, in you case it will be sent from sanjiarya2112@gmail.com. You cannot overwrite it using smtp.

I suggest you reading this https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server.

MrRobot21's avatar

@petrit Thank You i'll go through that

But, if that is the case how can create a contact form for user to fill and send to my mail

i'm really confused

Thank You for your time

ohffs's avatar

Try port 465 and make sure you have the 'MAIL_ENCRYPTION=tls' as above - see if that helps.

spekkionu's avatar

You shouldn't be setting the from to the user's email address. The email is being sent by your gmail account not by the user. Instead set the reply-to address to the user's email and leave the from at its default.

MrRobot21's avatar

@spekkionu @usamaashraf Thanks for your response

@usamaashraf I did as you said but i'm getting a different error, i think it's progressing thank you

Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 d24sm25585520pfb.90 - gsmtp
"

My ".env" File

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=mypassword   // i did tried adding app key generated in 2 step verification same error
MAIL_ENCRYPTION=ssl

My "congig/mail.php"

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'sanjiarya2112@gmail.com', 'name' => 'Name'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',

And My "AvoContactController.php"

<?php

namespace App\Http\Controllers;

use View;
Use Mail;
use App\AvoContact;
use App\Http\Requests;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Routing\Controller as BaseController;


class AvoContactController extends BaseController {

    public function contactpage()
    {
        $contact = AvoContact::all();

        return view('pages.contact')->with(['contact' => $contact]);
    }

    public function store(ContactFormRequest $request)
    {
        Mail::send('emails.contact', [], function ($m) use($request){
            $m
              ->from('sanjiarya2112@gmail.com')
              ->to('sanjiarya2112@gmail.com')
              ->subject('Your Reminder!');
        });

        return 'Email Sent';

    }  

}

All i want is to create a functionality exactly like this https://laracasts.com/contact (just the form) I don't know what i'm missing i'm stuck here from 3 days, any help is much appreciated

Thank You

MrRobot21's avatar

@usamaashraf I did try that

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=Mypss
MAIL_ENCRYPTION=ssl

And i'm getting the same error

Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 p13sm46107604pfj.58 - gsmtp
"

it's really not helping me

may i know how to use "mailgun api" in laravel, people say it is easy to setup in laravel

Thank You

usama.ashraf's avatar

@MrRobot21 you'll have to change your MAIL_DRIVER to 'mailgun' and configure the API. That won't be a problem once you get through this part.

  1. Login to your gmail.
  2. Verify the app password you setup for your application.
  3. Logout.
  4. Try sending mail again via the app.
MrRobot21's avatar

@usamaashraf i created new app name called Laravel in 2-step verification and it generated and gave me a key and i pasted in my password field

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=lpavrqwnuvqtdfrg   // My App Key something like this
MAIL_ENCRYPTION=ssl

Still i'm getting the same error, except is every thing ok?

Thank You

usama.ashraf's avatar

Remove the 'from' method invocation on the message object in your controller:

 ->from('sanjiarya2112@gmail.com')      // Remove this.

I think it may be overriding some of the configuration.

For now, don't use Mailgun.

1 like
MrRobot21's avatar

@usamaashraf Thank you so much for your time, i really appreciate it

I did as you said, since then i'm getting a new error

RequestException in CurlFactory.php line 187:
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

And My Controller is

<?php

namespace App\Http\Controllers;

use View;
Use Mail;
use App\AvoContact;
use App\Http\Requests;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Routing\Controller as BaseController;


class AvoContactController extends BaseController {

    public function contactpage()
    {
        $contact = AvoContact::all();

        return view('pages.contact')->with(['contact' => $contact]);
    }

    public function store(ContactFormRequest $request)
    {
        Mail::send('emails.contact', [], function ($m) use($request){
            $m
              //later i did trying adding it(->from('sanjiarya2112@gmail.com')) back but result is same 
              ->to('mrtext21@gmail.com') 
              ->subject('Your Reminder!');
        });

        return 'Email Sent';

    }  

}

And the .env file is

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

And the mail.php is

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'sanjiarya2112@gmail.com', 'name' => 'My Name'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',

Is there any thing i'm missing, I tried a lot by doing different thing and even if use mailgun i'm getting the same error

Thank You

usama.ashraf's avatar

@MrRobot21 here you go:

  1. Follow this link: http://curl.haxx.se/ca/cacert.pem Copy the entire page and save it in a "cacert.pem" file.

  2. In your php.ini file insert or edit the following line: curl.cainfo = "[path-to-this-file-on-your-machine]\cacert.pem"

This should do it for you.

1 like
MrRobot21's avatar

@usamaashraf Thanks a lot for your time

please can tell me where i can find cacert.pem is it in laravel app? or An i suppose to create one if so where to create

and i'm using WAMP for only to store data, i'm running application in localhost:8000

i add curl.cainfo = "[path-to-this-file-on-your-machine]\cacert.pem" i need the path

Thank You

usama.ashraf's avatar

@MrRobot21 download the file from the link I gave, put it anywhere on your computer with the name "cacert.pem", give that path in the php.ini file, restart WAMP and it should work.

1 like
MrRobot21's avatar

@usamaashraf Thanks for the information

I did as you said created a file called cacert.pem and pasted the all the code in it

and i gave the path inphp.ini file and restarted the WAMP

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo = "C:\wamp\www\Laravel\Avocet\cacert.pem"

And the error is not coming anymore but, i'm getting the old error back

Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 fn3sm68484046pab.20 - gsmtp
"

My Controller is

<?php

namespace App\Http\Controllers;

use View;
Use Mail;
use App\AvoContact;
use App\Http\Requests;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Routing\Controller as BaseController;


class AvoContactController extends BaseController {

    public function contactpage()
    {
        $contact = AvoContact::all();

        return view('pages.contact')->with(['contact' => $contact]);
    }

    public function store(ContactFormRequest $request)
    {
      Mail::send('emails.contact', [], function ($m) use($request){
            $m
              //->from('sanjiarya2112@gmail.com')
              ->to('mrtext21@gmail.com')
              ->subject('Your Reminder!');
      });

          return 'Email Sent';

    }  

}

And The .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=sanjiarya2112@gmail.com
MAIL_PASSWORD=password  //not the app key it's my login pass
MAIL_ENCRYPTION=ssl

Is there anything i'm doing wrong

Thank You for your time i really appreciate what you doing here, thanks

usama.ashraf's avatar
Level 7

@MrRobot21 you have to use the app key.

The mechanism is an extra layer of security Google adds.

If you still get the same error, again, regenerate a new app key, logout out from your account (sanjiarya2112@gmail.com) and try to send mail again via your app.

3 likes
MrRobot21's avatar

@usamaashraf Thank You I'll give it a try till it works

And to be sure may i know my all other configration are correct, except the this issue

Thank You

MrRobot21's avatar

@usama.ashraf Thanks for the information and for your time

once it works i'll get back to you if it's ok

Thank You

MrRobot21's avatar

@usama.ashraf Thank a lot it's working, thanks for your time i really really appreciate it

I regenerated a new key and cleared all me history cache and reconfigure the .env and logout out of the account and finally it's working

Thanks a lot for your time it's really wort it

Please or to participate in this conversation.