Send an automatic email on Laravel 8

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
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0bdb1b9bcafa5a3b5a2beb1bdb5cd9d89959d91999cb0979d91999cde939f9d">[email protected]</a>
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95d8d4dcd9cad3c7dad8cad4d1d1c7d0c6c6a8f8ecf0f8f4fcf9d5f2f8f4fcf9bbf6faf8">[email protected]</a>
MAIL_FROM_NAME="${APP_NAME}"

This is my route:
Route::get('send', 'AppHttpControllers<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09d91999cb39f9e84829f9c9c9582b083959e94b59d91999c">[email protected]</a>');

The mailController:
<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use Mail;
use DB;
use PDF;
use AppMailTestMail;

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

        Mail::to("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d6c1c7c1cdd2c1d6c9c5cdc8e4c3c9c5cdc88ac7cbc9">[email protected]</a>")->send(new TestMail($details));
        return "Email sent";
    }
}

The TestMail(the is inside AppMail):
<?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="064b474f4a595355435448474b433b6b7f6b676f6a46616b676f6a2865696b">[email protected]</a>
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 AppMail;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

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('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1acb8aca0a8ad81a6aca0a8adefa2aeac">[email protected]</a>', 'Veronica')
                ->view('email');
    }
}

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

namespace AppHttpControllers;

use AppHttpControllersController;
use AppMailUscita;
use IlluminateHttpRequest;
use IlluminateSupportFacadesMail;

class mailController extends Controller
{
    /**
     * Ship the given order.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function send(Request $request)
    {
        Mail::to('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="17657274727e6172657a767e7b57707a767e7b3974787a">[email protected]</a>')->send(new Uscita());
    }
}

the route is the same as before:
Route::get('/send', 'AppHttpControllers<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="28454941446b47465c5a4744444d5a685b4d464c">[email protected]</a>');

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x