Django Web 应用程序中的 SMTP 问题

SMTP issue in Django web application

我被要求向使用 Django/Python 框架实现的现有程序添加功能。此功能将允许用户单击一个按钮,它会显示一个小 dialog/form 以输入一个值。

我确实写了一些代码来显示电子邮件已发送的消息,但实际上并没有发送!

我的代码:

from django.shortcuts import render
from django.core.mail import send_mail


# Create your views here.
def index(request):

    send_mail('Request for a Clause',
    'This is an automated email. Jeff, please submit the case for 1234567',
    'akohan@mycompay.com',
    ['jjohnson@mycompany.com'],
    fail_silently=False)

    return render(request, 'send/index.html')

在项目根目录中,在 setting.py 我添加了 SMTP 配置:

EMAIL_HOST = 'mail.mycompany.com'
EMIAL_PORT = 587

#EMAIL_HOST_USER = 'akohan@mycompany.com'  ;no need it is on the white list
#EMAIL_HOST_PASSWORD = '' ;no need it is on the white list

EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

我 运行 输入:

python manage.py  SendEmailApp

我在这里错过了什么?

就我个人而言,我以前在 Django 项目中以这种方式发送过电子邮件,并且效果很好。您必须允许 SMTP 访问您的电子邮件。

import smtplib

def sendEmail():

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('yourEmailAddress@gmail.com', 'yourEmailPassword')

    try:
        server.sendmail('yourEmailAddress@gmail.com', 'emailAddressBeingSentTo', 'messageBeingSent')
    except:
        print('An error occurred when trying to send an email')

    server.quit()

旁注。安全对我来说不是问题,所以我没有检查它。

希望对您有所帮助:)