如何从Django表单发送电子邮件

时间:2019-07-07 18:35:05

标签: python django forms email

正如标题所述,我正在尝试通过表单发送电子邮件,但不起作用:

如您所见:

<form method="POST" action=''>
      {% csrf_token %}
      <div class="control-group form-group">
        <div class="controls">
          <label>Nombre completo:</label>
          <input type="text" class="form-control" id="nombre" required data-validation-required-message="Introduzca su nombre.">
          <p class="help-block"></p>
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Asunto:</label>
          <input type="text" class="form-control" id="asunto" required data-validation-required-message="Introduzca el asunto del correo.">
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Correo Electrónico:</label>
          <input type="email" class="form-control" id="email" required data-validation-required-message="Introduzca su correo electrónico.">
        </div>
      </div>
      <div class="control-group form-group">
        <div class="controls">
          <label>Mensaje:</label>
          <textarea rows="10" cols="100" class="form-control" id="contenido" required data-validation-required-message="Escriba su mensaje" maxlength="999" style="resize:none"></textarea>
        </div>
      </div>
      <button type="submit" class="btn btn-primary">Enviar mensaje</button>
    </form>

这是我的观点:

def contact(request):

contactdata = contactData.objects.get()
members = teamMembers.objects.filter(existencia=True)
templates = Templates.objects.get(isSelected=True)
categoria = Clasificacion.objects.filter(existencia=True)
enterprise = enterprisedata.objects.get()
content = request.POST.get('contenido', '')
name = request.POST.get('nombre', '')
email = request.POST.get('email', '')
subject = request.POST.get('asunto', '')
if request.method == 'POST' and email and name:
    send_mail(subject, content, email, ['kike1996@hotmail.com'], fail_silently=False)
contexto = {'categoria':categoria,'templates':templates,'members':members,
            'contactdata':contactdata,'enterprise':enterprise}
return render(request, 'contact/contact.html', contexto)

我正在通过POST请求调用表单,但未发送任何内容!。

希望可以帮助我,谢谢!。

2 个答案:

答案 0 :(得分:2)

如果要发送邮件,则必须在项目的settings.py文件中配置此设置

例如,如果您想通过gmail帐户发送电子邮件:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "your gmail address"
EMAIL_HOST_PASSWORD = 'your password'
EMAIL_PORT = '587'

还请记住在您的Google帐户中 enable less secure apps 发送电子邮件。  在视图中,您可以这样尝试:

if request.method == 'POST' and email and name:
    send_mail(subject, content, settings.EMAIL_HOST_USER, ['kike1996@hotmail.com'], fail_silently=False)

答案 1 :(得分:0)

本质上,这是从djagno发送电子邮件的代码行。

from django.core.mail import send_mail

send_mail(subject, content, from_email, to_list, fail_silently=False)

主题和内容部分很直观,然后剩下的就是设置from_email和to_list(在列表中甚至可以是1个电子邮件)。

您必须在设置文件中定义以下变量。

EMAIL_USE_TLS = True
EMAIL_HOST = smtp.gmail.com
EMAIL_HOST_USER = user@gsuiteaccount.com
EMAIL_HOST_PASSWORD = password
EMAIL_PORT = 587

Django允许使用gsuite帐户发送电子邮件。要设置gsuit,请转到您的Google帐户设置,依次选择安全性->帐户权限->访问不太安全的应用,然后启用此选项。

关于此选项:https://support.google.com/accounts/answer/6010255 希望这可以帮助您。