I’m working on integrating email functionality into my Laravel application using Mailgun as the email service provider. Everything seems to be configured correctly, but I keep running into this frustrating error:
Swift_TransportException: Cannot send message without a sender address
Here’s my current configuration:
config/mail.php settings:
'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,
],
'mailgun' => [
'transport' => 'mailgun',
],
'ses' => [
'transport' => 'ses',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
],
Environment variables (.env):
MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=2525
MAIL_USERNAME=postmaster@sandbox*****************.mailgun.org
MAIL_PASSWORD=**********************************
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
Mailable class (NotificationEmail.php):
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NotificationEmail extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->from('[email protected]')
->markdown('emails.notification');
}
}
Email template (notification.blade.php):
@component('mail::message')
# System Alert Notification
ATTENTION: This email is sent because our monitoring system detected an issue that requires immediate attention.
Please click the button below to view the full report.
@component('mail::button', ['url' => ''])
View Report
@endcomponent
Regards,<br>
{{ config('app.name') }}
@endcomponent
Test route:
Route::get('/test-email', function () {
Mail::to('[email protected]')->send(new NotificationEmail());
return new NotificationEmail();
});
What am I missing in my configuration? Any help would be appreciated!