Let's troubleshoot why you are not receiving the email in Laravel based on your provided setup. Here are the most common reasons and the steps to debug and fix them.
1. Check for Errors
First, make sure there are no errors being thrown in your application. After you submit the form:
- Do you see any error messages on the page?
- Do you see any errors in your
laravel.logfile (located instorage/logs/)? - If you have logging enabled and set to
debug, you should see details of the mail sending process.
Tip: If you're in a production environment, you may not see errors directly. Check your logs.
2. Mail Configuration
From your .env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=d****@gmail.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME=LaravelTraining
MAIL_FROM_ADDRESS=d****@gmail.com
Common Issues:
- Typo: The correct env variable should be
MAIL_MAILER=smtp, notMAIL_DRIVER=smtpfor Laravel 7 and above. - Make sure you have enabled Less secure app access in your Gmail account or set up an App Password if 2FA is on.
- Google often blocks signins by default for scripts or 3rd party apps. Check your email for "security alert" or similar messages from Google.
- Try using a different
MAIL_PORT(465forssl;587fortls). You have it correct fortls.
How to fix:
- Update your
.env:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=d****@gmail.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME=LaravelTraining
MAIL_FROM_ADDRESS=d****@gmail.com
- And in
config/mail.php, make sure you have:
'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,
'auth_mode' => null,
],
// ...
],
3. Cache Issues
If you change your .env, always run:
php artisan config:cache
php artisan cache:clear
4. Test the Mail System
Run this in tinker to see if mail works; it should not throw exceptions:
php artisan tinker
Mail::raw('Test email', function($msg) { $msg->to('[email protected]')->subject('Test'); });
Check your inbox.
5. Email is Being Sent, but Marked as Spam
- Check your Spam folder!
- Gmail and other providers can delay or block mail from automated apps.
6. Your Code Looks Good, but...
ContactController:
Mail::to('d****@yahoo.com')
->send(new ContactMail($request->all()));
- Are you sure the address is correct?
- Try sending it to another address or your own email for testing.
7. Add Validation and Error Handling
Wrap the Mail::to()->send() with a try/catch block to catch any errors:
public function sendmail(Request $request){
try {
Mail::to('d****@yahoo.com')->send(new ContactMail($request->all()));
Session::flash('flash', 'Sukses mengirim email');
} catch(\Exception $e) {
Session::flash('flash', 'Failed to send email: ' . $e->getMessage());
}
return redirect()->back();
}
8. Mail Sending in Development
If this is a dev machine, use Mailtrap to test sending emails properly, as Gmail can be tricky for development environments.
Summary: Step-by-step Checklist
- Update
.env: UseMAIL_MAILER, notMAIL_DRIVER. - Clear cache: Run
php artisan config:cache. - Try sending a mail via Tinker.
- Check your
laravel.logfor error messages. - Check your Spam/Junk folder.
- Ensure Gmail settings allow login from your app (app password or less secure apps).
- Wrap mail-sending in try/catch to get actual runtime error.
- If all fails, use a different SMTP server (Mailtrap, Mailgun, etc) for testing.
Example Wrap-up:
Update your .env:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your_real_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
Run:
php artisan config:cache
php artisan cache:clear
Test with:
// In routes/web.php for quick test
Route::get('/testmail', function () {
Mail::raw('Hello!', function($msg) {
$msg->to('[email protected]')->subject('Test');
});
return 'Sent!';
});
Visit /testmail and check your inbox and spam.
If you still do not receive email, closely check your storage/logs/laravel.log file for errors; the answer is likely there!
Let me know if you need further help or if you see any specific error messages.