【问题标题】:recipient_list isn't getting the email address in django收件人列表没有在 django 中获取电子邮件地址
【发布时间】:2021-02-14 10:25:33
【问题描述】:

我在 django 中发送 smtp 电子邮件时遇到问题。我尝试使用 User model => get_email = User.objects.filter(is_admin=True).values_list('email') 获取电子邮件地址 但是当我将它传递给收件人列表时,它找不到电子邮件地址。这是我的views.py:

from django.shortcuts import render
from feedbacks.models import Feedback
from django.contrib import messages
from django.core.mail import send_mail
from django.conf import settings

from django.contrib.auth import get_user_model

User = get_user_model()


def feedback(request):
    status = Feedback.objects.all()
    get_email = User.objects.filter(is_admin=True).values_list('email')
    print(get_email)

    if request.method == 'POST':
        name = request.POST["name"]
        student_id = request.POST["student_id"]
        adviser_init = request.POST["adviser_init"]
        phone = request.POST["phone"]
        email = request.POST["email"]
        issues = request.POST["issues"]

        obj = Feedback.objects.create(name=name, student_id=student_id, 
                                adviser_init=adviser_init, phone=phone,
                                  email=email, issues=issues)
        obj.save()
        try:
            subject = 'Student Feedback'
            message = "Mail from Student ID:" + student_id + "\nIssue:" + issues + ""
            email_from = settings.EMAIL_HOST_USER
            send_mail(subject, message, email_from, [get_email])
            messages.success(request, 'Your issue has been sent to our admin. '
                                  'Check feedback status for update. Thank You!')
        except:
            messages.error(request, 'Feedback Saved but not send to admin.')
    context = {
        'status': status
    }
    return render(request, 'feedback/feedback.html', context)

【问题讨论】:

    标签: python django smtp sendmail mail-server


    【解决方案1】:

    收件人列表必须是 list 对象,而不是 Queryset 或我相信的任何其他类似对象的数组。因此,将以下修改添加到您的 get_mail 变量中:

    
        mail_qs = User.objects.filter(is_admin=True).values_list('email', flat=True)
        get_mail = list(mail_qs)
    

    如果查询集不为空,应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多