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
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 .
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 .
Django Configure the mail function in , Mainly for SMTP agreement , Responsible for sending emails . The principle is as follows :
example : Authorization steps
Sign in QQ, modify QQ mailbox ==》 Set up ==》 Account ==》“POP3/IMAP… service ”
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 :