Send an automatic email on Laravel 8

Viewed 957

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
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=myemail@gmail.com
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("receivermail@gmail.com")->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!

3 Answers

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

I was able to make it work! I've followed Sanam Patel's idea and used MailTrap to send the email and it worked! Following my solution, maybe it can be usefull for someone with the same problem.

This is the .env file(quite similar to the original one):

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

In the config/mail.php I've changed the default to mailgun:

'default' => env('MAIL_MAILER', 'mailgun'),

and the smtp:

'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],

with php artisan make:mail Mail I've created my Mail.php, that I've made this way:

<?php

namespace App\Mail;

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

class Mail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('mymail@gmail.com', 'Veronica')
                ->view('email');
    }
}

The view email is a simple blade. Then the mailController.php is:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Mail\Uscita;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class mailController extends Controller
{
    /**
     * Ship the given order.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function send(Request $request)
    {
        Mail::to('receivermail@gmail.com')->send(new Uscita());
    }
}

the route is the same as before:

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

And all this work perfectly, now I will insert the requested data and I'm done. Thank y'all for the help!

Related