问题
I am using Laravel 7 and I want to send an email using shared hosting default mail service.
This is my .env file email configration
MAIL_DRIVER=smtp
MAIL_HOST=hostname
MAIL_PORT=465
MAIL_USERNAME=support@sitename.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl
And this is my default mail in config/mail.php
'default' => env('MAIL_MAILER', 'smtp'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
],
'ses' => [
'transport' => 'ses',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
But there is no email sent to the user, and it doesn't display any errors.
What may I have done wrong?
回答1:
You can catch the error by Swift_TransportException
, like this :
try {
Mail::send('emails.reminder', function ($m) {
$m->from('hello@app.com', 'Your Application');
$m->to('user@gmail.com', 'Sender Name')->subject('Your Reminder!');
});
}catch(\Swift_TransportException $e){
$response = $e->getMessage(); // catch the error here
}
来源:https://stackoverflow.com/questions/62771490/sending-an-email-using-a-shared-hosting