【问题标题】:"Contact Me" form that sends emails to me向我发送电子邮件的“联系我”表单
【发布时间】:2022-01-29 19:32:17
【问题描述】:

我有以下 HTML 页面:

{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Contact Me</title>
    <link rel="stylesheet" href="{% static 'projects/style_contact.css' %}">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div class="container">
      <div class="text">Contact Me</div>

      <form method="POST">
        {% csrf_token %}
        <div class="form-row">

          <div class="input-data">
            <div class="underline"></div>
            <input type="text" name="name" required placeholder="Name">
          </div>

          <div class="input-data">
            <div class="underline"></div>
            <input type="text" name="email" required placeholder="Email Address">
          </div>
        </div>

        <div class="form-row">
          <div class="input-data">
            <div class="underline"></div>
            <input type="password" name="password"required placeholder="Password">
          </div>
          
        </div>
        <div class="form-row">
          <div class="input-data textarea">
            <div class="underline"></div>
            <input type="text" name="message" required placeholder="Write your message">
          </div>
        </div>
        <div class="form-row submit-btn">
          <div class="input-data">
            <div class="inner"></div>
            <input type="submit" value="submit">
          </div>
        </div>

      </form>
    </div>

  </body>
</html>

我希望能够从该表单发送电子邮件。我知道所有电子邮件配置都需要放在 settings.py 文件中,但是我如何确保该电子邮件的合法所有者发送消息。 该表单需要获取该特定电子邮件地址的正确密码,以便合法所有者是唯一可以实际发送邮件的人。 有任何想法吗?? 这就是我的意思: 我的电子邮件地址是 nn@nn.com(不是真的)。我也有密码。我朋友的电子邮件地址是 n2@nn.com(不是真的)。我没有密码。我将如何阻止我使用我的朋友电子邮件通过 Django 表单发送电子邮件。因为这是一个安全问题。有什么想法吗??

settings.py 文件为:

    """
Django settings for src project.

Generated by 'django-admin startproject' using Django 4.0.1.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path
import os



# Build paths inside the project like this: BASE_DIR / 'subdir'.

BASE_DIR = Path(__file__).resolve().parent.parent #this was the original
# BASE_DIR =  os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-1!2hpx5ba$=lnvq#zu_98shz6@tj&uf#u0@pxh&tu+nm+l%5wr'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True


#Email Stuff 
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = '' # This is the 'from'
EMAIL_HOST_PASSWORD = '' # This is the password of the 'from'





ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'projects',
    
]

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',
]

ROOT_URLCONF = 'src.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'src.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
        
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT =  os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Email settings
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
EMAIL_USE_TLS = False
# EMAIL_USE_SSL = False

我想我的问题是:我可以将电子邮件配置放在视图文件中吗?

【问题讨论】:

  • 我应该包含views.py文件吗??
  • 显示 settings.py 文件
  • 我已经上传了设置文件

标签: python html django email


【解决方案1】:

改变这个

# Email settings
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
EMAIL_USE_TLS = False
# EMAIL_USE_SSL = False

改为添加这个

EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'from@gmail.com' # this email will be used to send emails
EMAIL_HOST_PASSWORD = 'xyz' # host email password required
# now sign in with your host gmail account in your browser
# open following link and turn it ON
# https://myaccount.google.com/lesssecureapps
# otherwise you will get SMTPAuthenticationError at /contactus
# this process is required because google blocks apps authentication by default
EMAIL_RECEIVING_USER = ['to@gmail.com'] # email on which you will receive messages sent from website

【讨论】:

  • 你是否在浏览器中打开了不太安全
  • 谢谢,但我知道该怎么做。但是,假设我知道您的电子邮件地址,我应该无法使用您的电子邮件地址发送电子邮件。你能告诉我如何解决这个问题吗?
  • 你收到这个错误了吗
猜你喜欢
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多