Laravel集成SendGrid发送邮件失败:连接超时问题求助
Hey there, let's troubleshoot that SMTP timeout issue you're hitting with Laravel and SendGrid. The Connection could not be established with host smtp.sendgrid.net [Operation timed out #60] error almost always points to a connectivity block or misconfiguration, so let's walk through the most common fixes step by step:
This is the #1 mistake people make with SendGrid's SMTP setup:
- SendGrid's SMTP username is fixed as
apikey— not your API key's name or any custom username. Update your.envfile:MAIL_USERNAME=apikey - Your
MAIL_PASSWORDshould be your full, unmodified SendGrid API key (make sure this key has the Mail Send permission enabled in your SendGrid dashboard). - After updating, clear Laravel's config cache to ensure the new settings load:
php artisan config:clear
A timeout error usually means your server can't reach SendGrid's SMTP server over port 587. Here's how to check:
- Test connectivity directly from your server using
telnet:
If you get a timeout here, your server's firewall or cloud provider's security group (e.g., AWS Security Groups, DigitalOcean Firewalls) is blocking outbound traffic on port 587. You'll need to add a rule allowing outbound connections totelnet smtp.sendgrid.net 587smtp.sendgrid.neton port 587 (or 465 as an alternative). - You can also use
curlto test:curl -v smtp://smtp.sendgrid.net:587
Sometimes .env formatting issues (like extra spaces or misplaced quotes) can cause problems:
- Double-check your
.enventries for typos or whitespace. For example,MAIL_FROM_NAME="Company"is fine, but make sure there's no space before/after the=sign. - Verify the loaded config using Laravel Tinker:
Look for thephp artisan tinker >>> config('mail')host,port,username, andpasswordvalues to ensure they match what you set in.env.
If port 587 is blocked, switch to port 465 with SSL encryption:
MAIL_PORT=465 MAIL_ENCRYPTION=ssl
Again, clear the config cache and test sending an email.
Make sure your SendGrid account isn't restricted:
- Log into your SendGrid dashboard and check if your account is verified (you need to complete domain or sender verification to send emails).
- Look at the Email Activity tab for any additional error messages that might explain the block.
To rule out Laravel-specific issues, create a simple standalone PHP script to test SendGrid's SMTP:
<?php require 'vendor/autoload.php'; // Set up SMTP transport $transport = (new Swift_SmtpTransport('smtp.sendgrid.net', 587, 'tls')) ->setUsername('apikey') ->setPassword('your-full-sendgrid-api-key'); $mailer = new Swift_Mailer($transport); try { // Create test message $message = (new Swift_Message('SendGrid SMTP Test')) ->setFrom(['info@mail.nl' => 'Company']) ->setTo(['your-test-email@example.com']) ->setBody('This is a direct SMTP test from your server'); // Send email $sent = $mailer->send($message); echo "Success! Sent $sent message(s)."; } catch (Exception $e) { echo "Error: " . $e->getMessage(); }
Run this script on your server. If it times out, the issue is definitely with your server's network or SendGrid's access. If it works, go back to checking Laravel's config or cache.
内容的提问来源于stack exchange,提问作者Jenssen

