将多个收件人电子邮件和名称添加到EmailMultiAlternatives中 [英] Append multiple recipient email and name into EmailMultiAlternatives
问题描述
这是形式mixin
类FormListView(FormMixin,ListView )
def get(self,request,* args,** kwargs):
form_class = self.get_form_class()
self.form = self.get_form(form_class)
self.object_list = self.get_queryset()
context = self.get_context_data(object_list = self.object_list,form = self.form)
返回self.render_to_response(上下文)
类CardListNew(FormListView):
form_class = EmailForm
model = card
def get_queryset(self):
return card.objects.filter(pk__in = [1])。filter(is_published ='true')
def sendmail(request):
如果request.method =='POST':
form = EmailForm request.POST)
如果form.is_valid():
fullname = form.cleaned_data ['name']
email = form.cleaned_data ['email']
rname = form .cleaned_data ['rname']
rema il = form.cleaned_data ['remail']
subject =你收到一封电子邮件+ fullname
message = form.cleaned_data ['message']
connection = get_connection()
connection.open()
text_content = message
msg = EmailMultiAlternatives(subject,message,email,[remail])
template_data = {
'sender_name' :fullname,'sender_email':email,'receiver_name':rname,'receiver_email':remail,'message':message
}
html_content = render_to_string(email.html,template_data)
msg.attach_alternative(html_content,text / html)
msg.send()
connection.close()#清理
返回HttpResponseRedirect('/ thank-you /')
else:
form = EmailForm()
return render(request,'card.html')
这是提交电子邮件的表单。我目前使用相同的输入名称来接收电子邮件和接收者名称。
< form role =formaction =/ sendmail /method =post>
< input type =textname =nameclass =form-control inputid =input1autocomplete =offplaceholder =Enter Name>
< input type =textname =emailclass =form-control inputid =input2autocomplete =offplaceholder =Enter Email>
< input class ='form-control reciptest'name =rnameplaceholder ='收件人名称'type ='text'>
< input type =textclass =form-control emailtestname =remailplaceholder =收件人电子邮件>
< a class =btn btn-sm btn-defaultonClick =addInput('dynamicInput');>添加收件人< / a>
< a class =btn btn-sm btn-defaultonClick =removeInput('dynamicInput');>删除收件人< / a>
< textarea id =input3name =messageclass =form-control inputrows =6placeholder =不超过1000个字母maxlength =1000> < / textarea的>
如何拆分和匹配列表收件人姓名和电子邮件,并将其附加到EmailMultiAlternatives )和template_data?
EmailMultiAlternatives获取to参数的电子邮件地址列表,所以一个简单的方法可能是更改:
msg = EmailMultiAlternatives(主题,消息,电子邮件,[remail])
to:
msg = EmailMultiAlternatives ,消息,电子邮件,remail.split(','))
将remail字段放入列表中,每个逗号。
Hi I have a form mixin which the user inputs a message and sends it to multiple recipients. I am using Mandrill as my email client. I am currently able to send the email to a single recipient, but it fails when inputting more then one user.
This is the form mixin
class FormListView(FormMixin, ListView):
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
self.form = self.get_form(form_class)
self.object_list = self.get_queryset()
context = self.get_context_data(object_list=self.object_list, form=self.form)
return self.render_to_response(context)
class CardListNew(FormListView):
form_class = EmailForm
model = card
def get_queryset(self):
return card.objects.filter(pk__in=[1]).filter(is_published='true')
def sendmail(request):
if request.method == 'POST':
form = EmailForm(request.POST)
if form.is_valid():
fullname = form.cleaned_data['name']
email = form.cleaned_data['email']
rname = form.cleaned_data['rname']
remail = form.cleaned_data['remail']
subject = "You received an email from " + fullname
message = form.cleaned_data['message']
connection = get_connection()
connection.open()
text_content = message
msg = EmailMultiAlternatives(subject, message, email, [remail])
template_data = {
'sender_name': fullname, 'sender_email': email, 'receiver_name': rname, 'receiver_email': remail, 'message': message
}
html_content = render_to_string("email.html", template_data)
msg.attach_alternative( html_content , "text/html")
msg.send()
connection.close() # Cleanup
return HttpResponseRedirect('/thank-you/')
else:
form = EmailForm()
return render(request, 'card.html')
This is the form that submits the email. I currently use the same input name for the receiver email and receiver name.
<form role="form" action="/sendmail/" method="post">
<input type="text" name="name" class="form-control input" id="input1" autocomplete="off" placeholder="Enter Name">
<input type="text" name="email" class="form-control input" id="input2" autocomplete="off" placeholder="Enter Email">
<input class='form-control reciptest' name="rname" placeholder='Recipient Name' type='text'>
<input type="text" class="form-control emailtest" name="remail" placeholder="Recipient Email">
<a class="btn btn-sm btn-default" onClick="addInput('dynamicInput');">Add Recipient </a>
<a class="btn btn-sm btn-default" onClick="removeInput('dynamicInput');">Remove Recipient</a>
<textarea id="input3" name="message" class="form-control input" rows="6" placeholder="Not more than 1000 letters" maxlength="1000"></textarea>
How would I go about in splitting and matching the lists recipients name and email and append it into EmailMultiAlternatives() and template_data?
EmailMultiAlternatives takes a list of email addresses for the "to" parameter, so a simple approach could be to change:
msg = EmailMultiAlternatives(subject, message, email, [remail])
to:
msg = EmailMultiAlternatives(subject, message, email, remail.split(','))
which will break apart the text of the remail field into a list, at each comma.
这篇关于将多个收件人电子邮件和名称添加到EmailMultiAlternatives中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!