2

I am building a Laravel 5.8 app in Localhost.

I configured my .env file this way:

MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=465 (took from my hosting provider)
MAIL_USERNAME=info@mydomain.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=ssl

I ran php artisan make:mail DemoEmail and I configured the file DemoMail.php like this:

<?php

namespace App\Mail;

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

class DemoEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The demo object instance.
     *
     * @var Demo
     */
    public $demo;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('info@mydomain.com')
                    ->view('mails.demo')
                    ->text('mails.demo_plain');
    }
}

Then, I built the views in views/mails/demo.blade.php and views/mails/demo_plain.blade.php. I registered the route in web.php, Route::get('/sendmail', 'MailController@send');. Finally, I generated and built the controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\DemoEmail; 
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function send()  
    {
        $objDemo = new \stdClass();
        $objDemo->sender = 'John';
        $objDemo->receiver = 'John Doe';

        Mail::to("myaccount@gmail.com")->send(new DemoEmail($objDemo));
        // Mail::to("myaccount@mydomain.com")->send(new DemoEmail($objDemo));
    }
}

When I send the email to my personal domain mail account, it arrives. When send the email to any gmail account, it does not arrive. I tried to check spam folders and it's empty. Also, I enabled the Less Secure App. No errors are thrown. Any ideas on why?

Enrico
  • 787
  • 12
  • 28
  • Is your mailbox a demo one? When you're on a trial some sites only let you send mail to the account you registered on? Also try sending a plain text email via Tinker to test it out maybe. – party-ring May 01 '19 at 14:51
  • nono it's my shared hosting with Hostgator. I am using wamp, do you think I need to change the php.ini file in order to configure the email? – Enrico May 01 '19 at 15:16
  • Have you checked your log file? Why you are changing `MailController` while you can set it up in `.env` file? – Prafulla Kumar Sahu May 02 '19 at 00:51
  • Yes I checked, no errors are thrown. what do you mean by setting the controller via .env file? Do you mean by assigning constants like MAIL_ADDRESS etc.? – Enrico May 02 '19 at 06:14
  • I have the same issue with laravel 8. also i use hostgator shared hosting – Dinesh Kumar May 16 '21 at 10:41
  • Did you guys find any solution? I'm getting the same issue :( – Prince Gautam Apr 06 '22 at 11:15

3 Answers3

1

Just Gmail accounts would indicate a dns issue maybe. Or a outdated TLS or SSL encryption. The .env file us key and may require a php artisan config:clear after changes are made. Just my 2 cent

GingaWRATH
  • 98
  • 4
0

I was having the same issue; my emails were not being delivered to gmail accounts while there were no problems with other providers such as hotmail.

My first approach was like that

Mail::send(
  "orders.templates.order-form",
  [
    "name" => $customer_name,
    "sales_representative_name" => \App\User::find(Auth::user()->id)->name,
    "sales_representative_phone" => \App\User::find(Auth::user()->id)->phones->first()->number,
    "items" => $order_items,
    "address" => $address,
    "net" => $net,
    "payment" => $payment,
    "balance" => $balance,
  ],
  function ($message) use ($name, $email) {
    $message->to($email, $name)->subject("Order Info");

    $swiftMessage = $message->getSwiftMessage();
    $headers = $swiftMessage->getHeaders();
    $headers->addTextHeader('From', 'noreply@app.xxx.co.uk');
    $headers->addTextHeader('X-Mailer', 'PHP/' . phpversion());
  }
);

order-form.blade.php

<!doctype html>
<html lang="en">
<head>
    <style>
        table {
            font-family: arial, sans-serif;
            border-collapse: collapse;
            width: 100%;
        }

        td, th {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
    </style>
    <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>Order Info</title>
</head>
<body>

<div style="padding: 25px; display: flex; flex-direction: row; justify-content: space-between">
    <div style="width: 50%; float: left; flex: 1;">
        <h4><strong>{{strtoupper($sales_representative_name)}} - Sales Representative</strong></h4>
        <h5><strong>(+44 {{$sales_representative_phone}})</strong></h5>
    </div>
    <div style="width: 50%; float: right; flex: 1; display: flex; justify-content: center; align-items: center;">
        <img src="https://www.xxx.co.uk/wp-content/uploads/2021/05/Untitled-1-1-1-copy.png" style="max-height: 75%; max-width: 75%;" alt="xxx">
    </div>
</div>
<div style="margin-top: 50px; padding: 25px;">
    <p><strong>Date:</strong> {{date('d.m.Y')}}</p>
</div>
<div style="margin-top: 5px; padding: 25px;">
    <p>
        Dear {{$name}}, thank you for choosing us. Your order will be produced and installed on the date you agreed with your seller. If you cancel the order within the first 24 hours of payment, you will be given a full refund. If you cancel within the first 3 days, half of the price will be refunded.
    </p>
</div>
<div style="margin-top: 5px; padding: 25px;">
    <table>
        <tr>
            <th style="text-align: center;">Description</th>
            <th style="text-align: center;">Total</th>
        </tr>
        <tr>
            <td>
                @foreach($items as $item)
                    {{$item->total}} BLINDS FOR {{\App\Blind::find($item->blind_id)->code}} FABRIC CODE, FRAME: {{$item->frame_type}}
                    <br />
                    <br />
                @endforeach
            </td>
            <td style="text-align: center;">£ {{number_format($net, 2, '.', ',')}}</td>
        </tr>
        <tr>
            <td style="border-left-style:hidden; border-bottom-style:hidden; text-align: right;">
                PAYMENT
            </td>
            <td style="text-align: center;">£ {{number_format($payment, 2, '.', ',')}}</td>
        </tr>
        <tr>
            <td style="border-left-style:hidden; border-bottom-style:hidden; text-align: right;">
                BALANCE
            </td>
            <td style="text-align: center;">£ {{number_format($balance, 2, '.', ',')}}</td>
        </tr>
    </table>
</div>
<div style="margin-top: 5px; padding: 25px;">
    <h4>Delivery Address:</h4>
    <br />
    @if($address)
        @if($address->street) {{$address->street}} <br> @endif
        @if($address->town) {{$address->town}} <br> @endif
        @if($address->city) {{strtoupper($address->city)}} <br> @endif
        {{$address->postcode ?? ''}}
    @else
        You do not have any primary address in our database yet. Please contact with us to update your delivery address.<br>
    @endif
</div>
<div style="margin-top: 5px; padding: 25px;">
    <h4><u>Fabrics to be used:</u></h4>
</div>
<div style="margin-top: 5px; padding: 25px;">
    @php
        $blind_unique_items = $items->unique(function ($item) {
            return $item->blind_id;
        })->values();
    @endphp
    @foreach($blind_unique_items as $item)
        @if (file_exists( public_path().'/img/blinds/'.\App\Blind::find($item->blind_id)->code.'.jpg'))
            <img src="https://app.xxx.co.uk/img/blinds/{{\App\Blind::find($item->blind_id)->code}}.jpg"
                 width="300" height="150"  alt="{{\App\Blind::find($item->blind_id)->code}}"/>
            <br />
            <br />
        @endif
    @endforeach
</div>
<div style="margin-top: 5px; padding: 25px;">
    <h4><u>Frames to be used:</u></h4>
</div>
<div style="margin-top: 5px; padding: 25px;">
    @php
        $frame_unique_items = $items->unique(function ($item) {
            return $item->frame_type;
        })->values();
    @endphp
    @foreach($frame_unique_items as $item)
        @if (file_exists( public_path().'/img/frames/'.$item->frame_type.'.jpg'))
            <img src="https://app.xxx.co.uk/img/frames/{{$item->frame_type}}.jpg"
                 width="300" height="150"  alt="{{$item->frame_type}}"/>
            <br />
            <br />
        @endif
    @endforeach
</div>
<div style="margin-top: 25px; padding: 25px;">
</div>
</body>
</html>

But it did not work for gmail. So I've changed my approach.

Applied this command on project root folder.

php artisan make:mail OrderInfoMail

OrderInfoMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\Mime\Email;

class OrderInfoMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this
            ->subject('Order Confirmation')
            ->from('noreply@app.xxx.co.uk', 'XXX Portal')
            ->view('orders.templates.order-form')
            ->with([
                'name' => $this->data->name,
                'sales_representative_name' => $this->data->sales_representative_name,
                'sales_representative_phone' => $this->data->sales_representative_phone,
                "items" => $this->data->items,
                "address" => $this->data->address,
                "net" => $this->data->net,
                "payment" => $this->data->payment,
                "balance" => $this->data->balance,
            ]);

        $this->withSymfonyMessage(function (Email $message) {
            $message->getHeaders()->addTextHeader(
                'X-Mailer', 'PHP/' . phpversion()
            );
        });

        return $this;
    }
}

And instead of using Mail::send, I've used the code below

$email = 'a@b.com'; // pls change
$name = 'ab';// pls change

$data = new \stdClass();
$data->name = $name;
$data->sales_representative_name = \App\User::find(Auth::user()->id)->name;
$data->sales_representative_phone = \App\User::find(Auth::user()->id)->phones->first()->number;
$data->items = $order_items;
$data->address = $address;
$data->net = $net;
$data->payment = $payment;
$data->balance = $balance;

Mail::to($email)->send(new \App\Mail\OrderInfoMail($data));

SUCCESS!

enter image description here

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
-1

May be its too late but, i had the same issue(In my local computer). But issue was cleared after I upload my project the server.

Dinesh Kumar
  • 131
  • 3
  • 11