Django Web应用程序中的SMTP问题 [英] SMTP issue in Django web application

查看:26
本文介绍了Django Web应用程序中的SMTP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm asked to add a feature to an existing program which is implemented using the Django/Python framework. This feature will allow the user to click on a button which it will show a small dialog/form to enter a value.

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

I did write some code which shows a message that the email is sent but in reality, it doesn't send it!

我的代码:

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配置:

In the project root, in the setting.py I have added SMTP configuration:

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

我通过键入以下内容来运行它:

I run it by typing:

python manage.py  SendEmailApp

我在这里想念什么?

推荐答案

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

Personally, I have sent emails before in this way in a Django project and it worked great. You will have to allow SMTP access to your email.

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()

旁注.安全对我来说不是问题,因此我没有对其进行检查.

Side note. Security was not an issue for me so I did not check into it.

希望这会有所帮助:)

这篇关于Django Web应用程序中的SMTP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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