skip to Main Content

I am using laravel 7. I want to receive email from user in my gmail account when user submit the contact form in live server.
My .env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygmail
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

My controller

public function saveContact(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required',
        'contact_no' => 'required',
        'message' => 'required',
    ]);

    $contact = new Contact();
    $contact->name = $request->name;
    $contact->email = $request->email;
    $contact->contact_no = $request->contact_no;
    $contact->message = $request->message;
    $contact->save();

    $data = array(
        'name' => $request->name,
        'email' => $request->email,
        'phone' => $request->contact_no,
        'user_message' => $request->message
    );

    Mail::to('mygmail')
        ->send(new ContactMail($data));

    return back()->with('success', 'Thank you for contacting us.');
}

My contactMail.php

public function build(Request $request)
{
    return $this->subject('New contact email')
        ->from($request->email)
        ->view('frontend.contact_email')
        ->with('data', $this->data);
}

I get this error, when i submit the form.

stream_socket_enable_crypto(): Peer certificate CN=domain.com' did not match expected CN=smtp.gmail.com’

2

Answers


  1. try this in yor .env file

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=465
    [email protected]
    MAIL_PASSWORD=yourapppassword
    MAIL_ENCRYPTION=ssl
    MAIL_MAILER=smtp
    

    You must also enable access to less secure applications in your Google Acount

    Login or Signup to reply.
  2. Set the keys verify_peer, verify_peer_name, and allow_self_signed to solve the error.
    You need to add the following of configmail.php :

    'stream' => [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true,
        ],
    ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search