django發郵件

django發郵件django


 

  • 配置setting信息
    • 1 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
      2 
      3 
      4 EMAIL_HOST = 'smtp.qq.com'
      5 EMAIL_PORT = 25
      6 EMAIL_HOST_USER = your email address 7 EMAIL_HOST_PASSWORD = 'our email key
      8 EMAIL_FROM = '<your email address>'
    • 我這裏用的qq提供的smtp,EMAIL_HOST 設置爲: 'smtp.qq.com'
    • EMAIL_PORT默認爲25
    • EMAIL_HOST_USER爲你的郵箱地址
    • EMAIL_HOST_PASSWORD設置:
      • 打開你的qq郵箱→設置→帳戶→POP3/SMTP服務→開啓
      • 發送短信,獲取密碼
    • EMAIL_FROM:一個字符串,尖括號裏面包裹的是你的email
  • 發送郵件
    • 1 def sendEmail(request): 2     email = request.POST.get('email')  # 獲取post請求裏面的email地址
      3     subject = ""   # 郵件的標題
      4     message = ""   # 郵件的正文
      5     sender = settings.EMAIL_FROM  # 郵件的發送者
      6     receiver = [email]  # 郵件的接受者(列表形式)
      7  send_mail(subject, message, sender, receiver) 8     return HttpResponse('發送成功!')
相關文章
相關標籤/搜索