Django Web 应用程序中的 SMTP 问题

2024-05-14

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

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

My code:

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',
    '[email protected] /cdn-cgi/l/email-protection',
    ['[email protected] /cdn-cgi/l/email-protection'],
    fail_silently=False)

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

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

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

#EMAIL_HOST_USER = '[email protected] /cdn-cgi/l/email-protection'  ;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('[email protected] /cdn-cgi/l/email-protection', 'yourEmailPassword')

    try:
        server.sendmail('[email protected] /cdn-cgi/l/email-protection', 'emailAddressBeingSentTo', 'messageBeingSent')
    except:
        print('An error occurred when trying to send an email')

    server.quit()

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

希望这可以帮助 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django Web 应用程序中的 SMTP 问题 的相关文章