You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Fix the SendGrid SMTP Authentication Credentials

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 .env file:
    MAIL_USERNAME=apikey
    
  • Your MAIL_PASSWORD should 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
    
2. Verify Server Firewall/Security Group Rules

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:
    telnet smtp.sendgrid.net 587
    
    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 to smtp.sendgrid.net on port 587 (or 465 as an alternative).
  • You can also use curl to test:
    curl -v smtp://smtp.sendgrid.net:587
    
3. Confirm Laravel is Loading Your Config Correctly

Sometimes .env formatting issues (like extra spaces or misplaced quotes) can cause problems:

  • Double-check your .env entries 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:
    php artisan tinker
    >>> config('mail')
    
    Look for the host, port, username, and password values to ensure they match what you set in .env.
4. Try an Alternative Port/Encryption

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.

5. Check Your SendGrid Account Status

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.
6. Test SMTP Connectivity Outside Laravel

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:09:17