【问题标题】:Send email from gmail with Django and Python使用 Django 和 Python 从 gmail 发送电子邮件
【发布时间】:2017-04-01 08:40:33
【问题描述】:

我发现了很多关于使用 django 发送电子邮件的帖子,但我对 gmail 有一个特定的问题:

error image

这是我的设置:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "myemail@gmail.com"
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = 'mypassword
EMAIL_PORT = 587
EMAIL_USE_TLS = True

我的看法是:

from django.core.mail import EmailMessage, send_mail

    if request.method == 'POST':
        name = request.POST.get('name','')
        email = request.POST.get('email','')
        subject = request.POST.get('subject','')
        text = request.POST.get('text','')

        message = 'From: ' + email + '\n\n\n' + text

        send_mail(subject, message, 'myemail@gmail.com', ['myemail@gmail.com'], fail_silently=False)

你能告诉我为什么会出现这个错误吗? 我还检查了谷歌设置,如“从不太安全的应用程序访问”等

提前谢谢你!!!

编辑

我已经允许不太安全的应用并点击了解锁验证码

【问题讨论】:

标签: python django email smtp gmail


【解决方案1】:

您好,请检查不带 gmail 扩展名 ie 的添加电子邮件 ID

EMAIL_HOST_USER = "myemail"

【讨论】:

    【解决方案2】:

    你也可以试试这个。

    import smtplib
    
    def send_mail(request):
        try:
            subject = "Email Subject"
            description ="description"
    
            gmail_user =  "from@gmail.com" # email id from where you want send mail
            gmail_pwd ="password"
            FROM = 'Admin: <from@gmail.com>'
            TO = "to@gmail.com" #email id where you want send mail
            TEXT = description
            SUBJECT = subject
            server = smtplib.SMTP_SSL()
            server = smtplib.SMTP("smtp.gmail.com", 587)
            server.ehlo()
            server.starttls()
    
            server.login(gmail_user, gmail_pwd)
            message = """From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
            server.sendmail(FROM, TO, message)
            server.quit()    
        except Exception,e:
            print 'exception',e
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 2016-09-09
      • 2013-10-17
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多