python - Append multiple recipient email and name into EmailMultiAlternatives -


hi have form mixin user inputs message , sends multiple recipients. using mandrill email client. able send email single recipient, fails when inputting more 1 user.

this 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 email " +  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 form submits email. use same input name receiver email , 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 1000 letters" maxlength="1000"></textarea> 

how go in splitting , matching lists recipients name , email , append emailmultialternatives() , template_data?

emailmultialternatives takes list of email addresses "to" parameter, simple approach change:

   msg = emailmultialternatives(subject, message, email, [remail]) 

to:

   msg = emailmultialternatives(subject, message, email, remail.split(',')) 

which break apart text of remail field list, @ each comma.


Comments