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

G Suite替代主机SMTP与Django集成:收信正常发件异常咨询

Troubleshooting Django Registration Emails with G Suite SMTP

Hey there, let's break down the most common issues that could be preventing your Django app from sending registration confirmation emails via G Suite. Since your domain is properly configured and support@mysite.com receives emails fine, we can focus on the integration between Django and G Suite's SMTP service.

1. Verify Your Django Email Settings

First, double-check that your settings.py has all the correct SMTP configurations. Here's what a valid setup should look like:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'support@mysite.com'  # Must match your G Suite account
EMAIL_HOST_PASSWORD = 'your-app-specific-password'  # See critical note below
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'support@mysite.com'  # Should match EMAIL_HOST_USER
SERVER_EMAIL = 'support@mysite.com'  # Optional but recommended for admin-related emails

Critical note: If you have 2-step verification enabled on your G Suite account (which you should!), do not use your regular password for EMAIL_HOST_PASSWORD. You need to generate an App Password from your Google Account settings under "Security" > "App passwords".

2. Test SMTP Connection Outside Django

To rule out Django-specific issues, test sending an email directly using Python's smtplib library. Run this script on your server or local development environment:

import smtplib
from email.mime.text import MIMEText

# Replace with your actual details
sender = 'support@mysite.com'
recipient = 'your-personal-test-email@example.com'
app_password = 'your-generated-app-password'

# Create a simple test message
msg = MIMEText('This is a test email sent directly via G Suite SMTP.')
msg['Subject'] = 'G Suite SMTP Connection Test'
msg['From'] = sender
msg['To'] = recipient

try:
    with smtplib.SMTP('smtp.gmail.com', 587) as server:
        server.starttls()  # Enable TLS encryption
        server.login(sender, app_password)
        server.sendmail(sender, recipient, msg.as_string())
    print("Test email sent successfully!")
except Exception as e:
    print(f"Failed to send email: {str(e)}")

If this script fails, the issue is with your G Suite account or server network—not Django. Common errors here include authentication failures (wrong password/app password) or connection timeouts.

3. Check Django's Error Logs

Look for SMTP-related errors in your Django logs. If you're running the app locally, check the console output. For production, check your server's error logs (e.g., Apache/Nginx logs, or the output from your Django LOGGING configuration).

Common error messages to watch for:

  • SMTPAuthenticationError: Invalid credentials (wrong password or missing app password)
  • ConnectionRefusedError: Server can't reach smtp.gmail.com:587 (firewall/security block)
  • SMTPServerDisconnected: Connection dropped mid-process (usually due to TLS issues or incorrect port)

4. Confirm G Suite Account Permissions

Ensure your G Suite admin hasn't restricted SMTP access for your account:

  • Log into the G Suite Admin Console
  • Go to Security > Basic settings
  • Under "Less secure app access", make sure it's not set to "Block" (though note that using app passwords is far safer than enabling this)
  • If your organization uses advanced security settings, check if SMTP is allowed for your user account

5. Check Server Firewall/Security Groups

Some hosting providers block outbound connections on port 587 by default. Verify that your server's firewall or cloud security group allows outbound traffic to smtp.gmail.com on port 587. You can test this with:

telnet smtp.gmail.com 587

If the connection is successful, you'll see a response starting with 220 smtp.gmail.com ESMTP. If it times out, you need to adjust your firewall rules.

6. Ensure Consistent Sender Email

Make sure the email address you're using to send registration emails matches DEFAULT_FROM_EMAIL and EMAIL_HOST_USER. Django sometimes falls back to a default sender (like webmaster@localhost) if you don't explicitly set it in your registration view. Double-check your registration code to confirm you're using send_mail with the correct from_email parameter, or rely on the DEFAULT_FROM_EMAIL setting.

Start with these steps, and you should be able to pinpoint where the issue is. If you hit a specific error message, we can dive deeper into that!

内容的提问来源于stack exchange,提问作者Jason Howard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:03:26