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!
