Home > OS >  Send an automatic email on Laravel 8
Send an automatic email on Laravel 8

Time:09-25

I'm doing a project with Laravel 8 where I need to send an automatic mail everytime the user click a button. I've created all the structure, even following some tutorial because I had no idea of how to do it, but even if i see the "Email sent" page, the email doesn't show on gmail. I really need some help(even if I'm sure that the problem is stupid, but I can't find the error). So there is the code. This is my .env:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

This is my route:

Route::get('send', 'App\Http\Controllers\mailController@sendEmail');

The mailController:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use DB;
use PDF;
use App\Mail\TestMail;

class mailController extends Controller {
    public function sendEmail()
    {
        $details = [
            'title' => 'Mail from veronica',
            'body' => 'This is for testing'
        ];

        Mail::to("[email protected]")->send(new TestMail($details));
        return "Email sent";
    }
}

The TestMail(the is inside App\Mail):

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Test Mail from Veronica')->view('mail');
    }
}

And in the end the mail.blade.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test Mail</title>
</head>
<body>
     <h1>{{$details['title']}}</h1>
     <p>{{$details['body']}}</p>
     <p>Thank you</p>
</body>
</html>

That's everything I've modified, as I said if I go to http://localhost:8000/send I can see "Email sent", but I don't recive the mail.

If I can I would like to ask another thing, the next step is to attach to the mail a .pdf with some informations, I know that I have to use ->attachData("document.pdf", $pdf), but where do I have to put it?

Thanks so much for your help!

CodePudding user response:

Your laravel app may be blocked by google service as unsecure, so you need to allow new app in your gmail account. Go this links:

  1. https://myaccount.google.com/lesssecureapps?pli=1
  2. https://accounts.google.com/b/0/DisplayUnlockCaptcha

And check security options.

After you have authorized the application, you need to wait a bit. Sometimes it takes 20-30 minutes for a change to take effect in GMAIL account. So be patient

CodePudding user response:

Try changing the MAIL_HOST=smtp.gmail.com to MAIL_HOST=smtp.googlemail.com

  • Related