1. 程式人生 > 實用技巧 >基於django 服務-傳送郵件

基於django 服務-傳送郵件

本文基於qq郵箱服務來發送郵件,去郵箱的設定裡去設定-賬戶下:

點選生成授權碼(需要你的手機發送簡訊文字)獲取到授權碼後進行django的基本設定:

Settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'  # 在這裡填入您的QQ郵箱賬號
EMAIL_HOST_PASSWORD = 'edlszopuwgrvdjba'  # 請在這裡填上您自己郵箱的授權碼
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_SSL = True

Views.py:

from django.shortcuts import HttpResponse
from django.core.mail import send_mail
from xxx.settings import dev


def check_mail(request):
    msg = '確認已經收到的郵箱哦。'
    try:
        send_mail(
            subject='請注意這是Django郵件測試',
            message=msg,
            from_email=dev.EMAIL_HOST_USER,
            recipient_list=["[email protected]"]

        )
    except Exception as e:
        return HttpResponese(e)
    else:
        return HttpResponese('測試郵件已發出請注意查收')