我正在尝试使用 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.
如果在 settings.py 文件中注释 #EMAIL_USE_TLS = True。它没有显示任何错误,但我的 Gmail 帐户没有收到任何电子邮件。
如果我更改 EMAIL_PORT = 25,我会得到:
error at /mysite/
[Errno 10061] No connection could be made because the target machine actively refused it
请您参考如下方法:
对我来说这很有效,试试这个:
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)
...
