Django不发送电子邮件

时间:2015-04-10 14:30:37

标签: python django email

我正在尝试使用Django的send_mail()从本地计算机发送邮件,但遇到了一些挑战。

我在settings.py设置了电子邮件设置:

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = '1025'
EMAIL_USE_TLS = True
LANGUAGE_CODE = 'en-us'

views.py

def send_greetings(request):
    from django.core.mail import send_mail
    send_mail('Invitation', 'Hi, how are you doing today?', 'info@greetings.com', [example@gmail.com], fail_silently=False)

如果我尝试上面的代码,我会得到:

SMTPException at /mysite/
STARTTLS extension not supported by server.

如果在#EMAIL_USE_TLS = True文件中发表评论settings.py。它没有显示任何错误,但我的Gmail帐户中没有收到任何电子邮件。

如果我改变了EMAIL_PORT = 25我得到了:

error at /mysite/
[Errno 10061] No connection could be made because the target machine actively refused it

2 个答案:

答案 0 :(得分:1)

对我来说这很完美,试试这个:

<强> settings.py

# Change HOST, HOST_USER, and HOST_PASSWORD
EMAIL_USE_TLS=True
EMAIL_HOST='mail.example.com'
EMAIL_HOST_USER='test@example.com'
EMAIL_HOST_PASSWORD='test123'
EMAIL_PORT=26

<强> views.py

def send_greetings(request):
     ...
     subject = 'email subject'
     message = "Hello"
     from_email = settings.EMAIL_HOST_USER
     to_email = "youremail@example.com"
     recipient = [to_email]
     send_mail(subject, message, from_email, recipient, fail_silently=True)
     ...

答案 1 :(得分:0)

你能尝试以下吗? send_mail返回一个您未在方法中返回的值。

def send_greetings(request):
    from django.core.mail import send_mail
    result = send_mail('Invitation', 'Hi, how are you doing today?', 'info@greetings.com', [example@gmail.com], fail_silently=False)
    return result