Append multiple recipient email and name into Emai

2019-08-31 06:03发布

问题:

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?

回答1:

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.