程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django19: send mail

編輯:Python

1、 Mail related agreements

(1)SMTP

        SMTP(Simple Mail Transfer Protocol), Simple mail transfer protocol (25 Port no. ). It is a set of specifications for transferring messages from the original address to the destination address , It controls the transfer of mail . Belong to “ push ” agreement

(2)IMAP

        IAMP(Internet Mail Access Protocol), Interactive mail access protocol , It's an application layer protocol (143 port ). Used to access mail on a remote server from a local mail client . Belong to “ Pull ” agreement .

(3)P0P3

        P0P3(Post Office Protocol3) For short , That is to say, No 3 A version , yes TCP/IP A member of the protocol family (110 port ). This agreement is mainly used to support the remote management of e-mail on the server using the client . Belong to “ Pull ” agreement .

notes :IMAP and POP3

Both are “ Pull ” Type a agreement , Responsible for downloading mail from the mail server .

  • IMAP With summary browsing function , You can preview part of the summary , Then download the whole email
  • IMAP It is a two-way agreement , The client operation can be fed back to the server
  • POP3 All messages must be downloaded , No summary function
  • POP3 It is a one-way protocol , The client operation failed to synchronize the server

2、Django email

        Django Configure the mail function in , Mainly for SMTP agreement , Responsible for sending emails . The principle is as follows :

  • to Django Authorize a mailbox
  • Django Use this mailbox to send mail to the corresponding recipient
  • django.core.mail Encapsulates the automatic sending of e-mail SMTP agreement

example : Authorization steps

Sign in QQ, modify QQ mailbox ==》 Set up ==》 Account ==》“POP3/IMAP… service ”

3、Django To configure

(1)settings.py To configure

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com' # tencent QQ mailbox smtp Server address
EMAIL_PORT = 25 # smtp The port number of the service
EMAIL_HOST_USER = '[email protected]' # Sent by qq mailbox
EMAIL_HOST_PASSWORD = '******' # qq I got it after sending text messages in my email qq Email authorization code
EMAIL_USE_TLS = False # And smtp When the server communicates , Whether to start TLS link ( Secure links ) Default False

(2) Function call

from django.core import mail
mail.send_mail(
subject, # subject
message, # The message content
from_email, # sender ( The mailbox is currently configured )
recipient_list=[‘[email protected]’] # Recipient mailing list
)

example : Email alert

(a)settings.py

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# 'middleware.mymiddleware.MyMW',
# 'middleware.mymiddleware.MyMW2',
'middleware.mymiddleware.ExceptionMW',
]
……
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com' # tencent QQ mailbox smtp Server address
EMAIL_PORT = 25 # smtp The port number of the service
EMAIL_HOST_USER = '[email protected]' # Sent by qq mailbox
EMAIL_HOST_PASSWORD = '******' # qq I got it after sending text messages in my email qq Email authorization code
EMAIL_USE_TLS = False # And smtp When the server communicates , Whether to start TLS link ( Secure links ) Default False
# Custom email address
EX_EMAIL = ['[email protected]']

(b)middleware.py

class ExceptionMW(MiddlewareMixin):
def process_exception(self, reqeust, exception):
print(exception)
print(traceback.format_exc())
mail.send_mail(subject='mysite3 Report errors ', message=traceback.format_exc(), from_email='[email protected]', recipient_list=settings.EX_EMAIL)
return HttpResponse(' Page busy ')

(c)views.py

@csrf_exempt
def test_upload(request):
test # Error testing
if request.method == 'GET':
return render(request, 'test_upload.html')
elif request.method == 'POST':
title = request.POST['title']
myfile = request.FILES['myfile']
Content.objects.create(title=title, picture=myfile)
return HttpResponse('-- Upload file successful --')

(d)urls.py

urlpatterns = [
path('admin/', admin.site.urls),
path('test_upload', views.test_upload),
]

visit :http://192.168.28.128:8000/test_upload

  mail :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved