'Send Email Using SendGrid in Laravel 8
I'm trying to setup SendGrid to send emails in my Laravel 8 project. I've been following this documentation:
https://sendgrid.com/docs/for-developers/sending-email/laravel/
I copied the .env mail setup the way they have it. I just add my sendgrid api key in the password field, and added my sendgrid verified email in the "MAIL_FROM_ADDRESS".
MAIL_MAILER=smtp
# MAIL_DRIVER=smtp # for laravel < 7
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=MY_SENDGRID_API_KEY
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="MY NAME"
MAIL_FROM_ADDRESS=MY VERIFIED EMAIL FROM SENDGRID
Then it asks me to do a php artisan make:mail TestEmail ("app/Mail/TestEmail.php") and copy the following code:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
$address = 'MY VERIFIED SENDGRID EMAIL ADDRESS';
$subject = 'This is a test!';
$name = 'MY NAME';
return $this->view('emails.test')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with([ 'test_message' => $this->data['message'] ]);
}
}
Then I created a view in "app/resources/views/emails/test.blade.php":
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
</head>
<body>
<h2>Test Email</h2>
<p>{{ $test_message }}</p>
</body>
</html>
Sendi
Then it tells me to send the email with the following code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\TestEmail;
use Illuminate\Support\Facades\Mail;
class SendGridEmailController extends Controller
{
public function sendMail()
{
$data = ['message' => 'This is a test!'];
Mail::to('MY GMAIL ACCONT')->send(new TestEmail($data));
}
}
For the above code, the documentation didn't say to create a controller or anything, but in my situation I did created a controller "SendGridEmailController" and added the above code there.
Then, in my routes/web.php file, I created a route:
Route::get('/test_email', [SendGridEmailController::class, 'sendMail']);
Then, when I try to run the code by going to the above route, I get this error:
"Address in mailbox given [mailtrap] does not comply with RFC 2822, 3.6.2."
I don't understand what this error is trying to say. I tried looking up at Google search and even here, but I couldn't find anything that relates to what I'm facing in Laravel.
Maybe I miss something or did not setup sendgrid properly?
I appreciate your help on this. Thank you!
Solution 1:[1]
Did you run this?
php artisan config:cache
Solution 2:[2]
Due to sendgrid's changes in policy in January 2021 Upgrade to API Keys for your SMTP integration. The SMTP environment variables approach above we'll throw an error and won't work however a way around it is to create a restricted API keys for sending mail only
the environment variable should look like this.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=2525
MAIL_USERNAME="apikey"
MAIL_PASSWORD="your secret key here"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=emailaddres
MAIL_FROM_NAME=app_name
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Naeim Safaee |
| Solution 2 | Alemoh Rapheal Baja |

