Unable to send email in laravel in server

*爱你&永不变心* 提交于 2019-12-24 10:48:53

问题


I've following configuration in environment file,

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

I've made user registration from RegistrationController and send email to verify that user. It works perfectly in the local LAMP server with ubantu. But, following error is encountered in C Panel.

Swift_TransportException Connection could not be established with host smtp.gmail.com [Network is unreachable #101]

Here is the register function in controller,

protected function register(Request $request)
{
    /** @var User $user */
    $validatedData = $request->validate([
        'name'     => 'required|string|max:255',
        'email'    => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);

    try {
        $validatedData['password']      = bcrypt(array_get($validatedData, 'password'));
        $validatedData['activate_code'] = str_random(30).time();
        if($request->has('company') && $request->company=='on')
        {
            $validatedData['role_id']   = 4;
        }
        else
        {
            $validatedData['role_id']   = 5;
        }
        $user                           = app(User::class)->create($validatedData);
    } catch (\Exception $exception) {
        logger()->error($exception);
        return redirect()->back()->with('alert-danger', 'Unable to create new user.');
    }

    $user->notify(new UserRegisteredSuccessfully($user));
    return redirect()->back()->with('alert-success', 'Successfully created a new account. Please check your email and activate your account.');
}

I've already seen such issues and many question but none works in my case. Any kind of help is appreciated.


回答1:


Update your .env file like:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD='mypassword'
MAIL_ENCRYPTION=tls



回答2:


firstable, once you have created gmail account for mailing from your website, You must give a permission your gmail account for mailing from other app. to do this...

1) go to your gmail account and click on "settings -> FORWARDING and POP/IMAP" link. And follow these instructions like below picture...

2) go to this link https://myaccount.google.com/lesssecureapps?utm_source=google-account&utm_medium=web and enable this like below picture...

3) make sure thst if your web app is working on http protocol use MAIL_ENCRYPTION=tls and MAIL_PORT=587 otherwise MAIL_ENCRYPTION=ssl and MAIL_PORT=465




回答3:


You need to do some changes with your .env config and with your account

1) Change mail port to 587 and encryption

 MAIL_PORT=587
 MAIL_ENCRYPTION=tls

2) Enable IMAP in your email account to use that protocol

3) Enable access for the app to your account




回答4:


change MAIL_DRIVER=smtp to MAIL_DRIVER=sendmail

After completion of .env edit, enter this command in your terminal for clear cache: php artisan config:cache



来源:https://stackoverflow.com/questions/53869248/unable-to-send-email-in-laravel-in-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!