Swift_TransportException问题:Laravel应用在Hostinger虚拟主机无法发邮件
Hey there, let’s work through this email sending issue you’re hitting with Laravel on Hostinger. I’ve helped a few developers fix similar problems, so let’s break down what might be going wrong and how to get your emails flowing.
1. First: Check Your Laravel Version (Critical!)
If you’re using Laravel 7 or newer, the old MAIL_DRIVER key was replaced with MAIL_MAILER. Your existing config attempts use the deprecated key, which could be causing transport failures right off the bat.
Update your .env to match the modern format first:
MAIL_MAILER=smtp MAIL_HOST=smtp.hostinger.com # Keep mx1.hostinger.in if that's your region's designated MX server MAIL_PORT=587 MAIL_USERNAME=your-full-email@yourdomain.tld MAIL_PASSWORD=your-email-password-or-app-password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=your-full-email@yourdomain.tld MAIL_FROM_NAME="${APP_NAME}"
Quick note: If you have 2FA enabled on your Hostinger email, skip your regular account password and use an app password instead. You can generate this in your hPanel under
Email > Manage > Security.
2. Fix Issues in Your Previous Config Attempts
Looking at the three configs you tried, here are specific problems that might have blocked success:
- For port 587:
MAIL_ENCRYPTIONshould betls, notnull—Hostinger requires encryption on this port. - When using
sendmaildriver: You don’t need SMTP host/port/username/password entries (sendmail uses the server’s local mail stack). But Hostinger often restricts sendmail on shared hosting, so SMTP is almost always more reliable. - Always use your full email address for
MAIL_USERNAME(e.g.,support@yourdomain.com), not just the username portion.
3. Clear Laravel’s Config Cache
If you’ve tweaked .env multiple times, cached old settings might be overriding your new config. Run these commands via SSH or Hostinger’s hPanel terminal:
php artisan config:clear php artisan config:cache php artisan cache:clear
This forces Laravel to pick up your latest email settings immediately.
4. Check Hostinger’s Email Restrictions
Hostinger shared hosting has a few guardrails that can block email sends:
- Verify you’re not hitting hourly/daily email limits (check hPanel >
Email > Limits). - Confirm your domain’s SPF and DKIM records are set up correctly. Misconfigured records can cause delivery failures or mark emails as spam. You can check these in hPanel >
Domains > Manage > DNS.
5. Test with a Simple Debug Route
To get a more specific error message than the generic Swift_TransportException, add a quick test route to your app:
// routes/web.php Route::get('/test-email', function () { try { Mail::raw('This is a Laravel test email from your Hostinger server', function ($message) { $message->to('your-personal-email@example.com') ->subject('Laravel Email Test'); }); return 'Email sent successfully!'; } catch (Exception $e) { return 'Error details: ' . $e->getMessage(); } });
Visit /test-email in your browser—this will show you exactly where the connection is failing (e.g., invalid credentials, port block, etc.).
6. Fallback: Use PHP’s Native Mail Function
If SMTP still won’t work, try switching to the mail driver (uses PHP’s built-in mail() function):
MAIL_MAILER=mail MAIL_FROM_ADDRESS=your-full-email@yourdomain.tld MAIL_FROM_NAME="${APP_NAME}"
This is simpler but less reliable for deliverability, but it’s a good test to rule out SMTP-specific issues.
If none of these steps work, check Hostinger’s server error logs (hPanel > Hosting > Logs > Error Log) for more context. Sometimes it’s a server-side firewall rule or hosting restriction that needs Hostinger’s support team to adjust.
内容的提问来源于stack exchange,提问作者Saurabh Mistry

