Django尝试使用send_mail()在python shell中发送电子邮件,但无法正常工作 [英] Django- Try sending email in python shell using send_mail(), but cannot work

查看:1071
本文介绍了Django尝试使用send_mail()在python shell中发送电子邮件,但无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Django项目中发送帐户激活链接,但它无法正常工作。所以我在shell中尝试非常基本的send_mail()函数,看看它是否在发送。

I am trying to send account activation link in my Django project, but it cannot work. So I try the very basic send_mail() function in shell and see if it is sending.

在settin.py

in the settin.py

    AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend', # necessary for django.auth
    'survey.modelbackend.EmailBackend' # custom backend to authenticate using the email field
)

    #settings for the email 
    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 25
    DEFAULT_FROM_EMAIL = 'webmaster@localhost'

(我正在使用电子邮件设置中的所有默认值)

(I am using all the default values in the settings for email)

在我输入的shell中

in the shell I typed

>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ...     ['myemailaddress@gmail.com'], fail_silently=False)

,并在上述行后返回1,但是我无法在自己的电子邮件中收到电子邮件

and it return 1 after the above line, but I cannot get the email in my own email

任何人都可以帮助解决这个问题,并解释为什么电子邮件不会发送到我的电子邮件?

Can anyone help with this problem and explain why the email is not sent to my email?

非常感谢。

推荐答案

尝试询问,这可以在settings.py

Hy, I've tried asking and this can successfully send email without any setting in the settings.py

中成功发送电子邮件,首先添加这两个导入

first, add these two imports

import smtplib
from email.mime.text import MIMEText as text

然后发送电子邮件的代码可能就像这样

then the code to send email could be like this

def send_email(sender,receiver,message,subject):
    sender = sender
    receivers = receiver
    m = text(message)
    m['Subject'] = subject
    m['From'] = sender
    m['To'] = receiver

   # message = message

    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, str(m))
       print "Successfully sent email"
    except SMTPException:
       print "Error: unable to send email" 

这个工作正如预期的那样。

This works just as expected.

这篇关于Django尝试使用send_mail()在python shell中发送电子邮件,但无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆