How to send email to settings.MANAGER in django recipient_list

时间:2016-07-11 23:00:57

标签: django django-forms

In Django, I wish to have the send_email send the email to settings.MANAGERS and to the ticket issuer as well. So trying to set those in recipient_list porduced no luck. I am using class based view.

# views.py

...
from django.conf import settings
...

class TicketCreate(CreateView):
    model = Ticket
    fields = ['title', 'question_detail',]
    raise_exception = False   
    success_url = reverse_lazy('ticket_list')     
    template_name = 'ticket/ticket_form.html' 

    def form_valid(self, form):
        form.instance.owner = self.request.user     

        send_mail(
            subject=form.cleaned_data.get('title').strip(),
            message=form.cleaned_data.get('question_detail'),
            from_email=form.cleaned_data.get('request.user.email'),
            recipient_list=['settings.MANAGERS','request.user.email',],  
       )

       return super(TicketCreate, self).form_valid(form)  

# settings.py
...
MANAGERS = [
    ('Manager', 'email_id@somedomain.com'),
]

What would be the solution to achieve this?

2 个答案:

答案 0 :(得分:0)

MANAGERS是一个元组,只有每个元组中的第二个元素是相关的。这将有效:

sender = form.cleaned_data.get('request.user.email')
recipients = [a[1] for a in settings.MANAGERS]
recipients.append(sender)

send_mail(
    subject=form.cleaned_data.get('title').strip(),
    message=form.cleaned_data.get('question_detail'),
    from_email=sender,
    recipient_list=recipients,  
)

mail_managers函数可以为您完成大部分工作 - 唯一的区别是它不允许您控制发件人电子邮件(它使用SERVER_EMAIL)。

请注意,您应该非常小心代表其他人发送邮件 - 仅建议您控制发件人的域名。否则,您的邮件很有可能被捕获为垃圾邮件,并且发送服务器存在相关的信誉风险。

答案 1 :(得分:0)

感谢solarissmoke,因为我只让登录用户发送票证,并且所有用户都需要使用有效的电子邮件地址进行身份验证,我认为用户不需要再次输入电子邮件地址。因此,从用户数据中获取电子邮件并将其插入到故障单中。

因此,我修改了一下,现在所有的工作都很酷。再次感谢,你对垃圾邮件的看法是正确的 - 一切都在我的控制之下。

sender=self.request.user.email
recipients = [a[1] for a in settings.MANAGERS]
recipients.append(sender)

send_mail(
    subject=form.cleaned_data.get('title').strip(),
    message=form.cleaned_data.get('question_detail'),
    from_email=sender,
    recipient_list=recipients,  
)