In this tutorial, we’ll walk you through the process of setting up SMTP for sending emails in a Laravel application using Hostinger’s email servers. SMTP (Simple Mail Transfer Protocol) allows your application to send emails securely and efficiently.
1. Get Your SMTP Settings
Log in to your Hostinger account and navigate to your email section. You'll need the following SMTP settings:
- SMTP Server: smtp.hostinger.com
- SMTP Port: 587 (Recommended for TLS) or 465 (for SSL)
- Username: your-email@example.com
- Password: your-email-password
- From Email: your-email@example.com
2. Update .env File
The next step is to configure the SMTP settings in your Laravel application. Open your `.env` file and add the following settings:
MAIL_MAILER=smtp MAIL_HOST=smtp.hostinger.com MAIL_PORT=587 MAIL_USERNAME=your-email@example.com MAIL_PASSWORD=your-email-password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=your-email@example.com MAIL_FROM_NAME="${APP_NAME}"
3. Configure Laravel Mail
After configuring the `.env` file, Laravel automatically uses these SMTP settings for sending emails. You can test email functionality by running the following command to send a test email:
php artisan tinker Mail::raw('This is a test email!', function ($message) { $message->to('recipient@example.com')->subject('Test Email'); });
4. Verify Email Settings
After running the above command, check the recipient's inbox for the test email. If it’s received successfully, your SMTP setup is complete. If not, make sure the credentials in your `.env` file are correct.
Troubleshooting
If you’re facing issues with the SMTP setup, try the following:
- Check your email account settings and ensure you have enabled SMTP and IMAP.
- Verify the email password and ensure no typos in your `.env` file.
- Test with different ports (465 for SSL or 587 for TLS).
- If you're using 2FA (Two-Factor Authentication), make sure you use an app-specific password.