Laravel SMTP Mail Sending

Viewed 213

I'm building a project with Laravel version 7.28 and I'm trying to send notification to my users. I'm based on https://www.itsolutionstuff.com/post/laravel-7-send-email-exampleexample.html

.env file

MAIL_MAILER=smtp
MAIL_HOST=mail.google.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="lorem ipsum"

web.php

    Route::get('/send-notification',   'MailController@sendNotification');

MailController.php

<?php

namespace App\Http\Controllers;

use App\Mail\Test;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function sendNotification()
    {
        $details = [
            'title' => 'title',
            'body' => 'body'
        ];
        Mail::to('info@londonistinvestments.com')->send(new Test($details));
        echo 'email has sent';
    }
}

Under app folder, I have a directory named Mail and under it I have Test.php. I created it with "php artisan make:mail MyTestMail" command on terminal.

Here is Test.php file

<?php

namespace App\Mail;

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

class Test 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('Notification - New Lead')->view('email.test');
    }
}

Finally test view that is under resources/views/email

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Notification</title>
</head>
<body>

<h1>lorem isppsum</h1>

<p>thank you</p>

</body>
</html>

I'm using Xampp, PHP version is 7.2.34. I'm able to send emails on local but when I upload to my shared hosting that uses PHP version 7.3 I get error. Which is;

error

I didn't understand the problem. I also tried to connect mail.gmail.com on ports 25, 465 and 587 with ssl and tls. What sould I do?

1 Answers

The shared hosting provider is likely blocking those ports.

I had a similar situation with trying to email (integration with mailgun but still using those ports) from shared hosting (A2 Hosting) and after going through their support was informed that those ports are not available and it would not work.

I ended up having to switch to VPS to solve my problem.

Related