def sponsor_email(request, pks): sponsors = Sponsor.objects.filter(pk__in=pks.split(",")) address_list = [] for sponsor in sponsors: for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) initial = { 'from_': config.SPONSOR_FROM_EMAIL, } # Note: on initial entry, we've got the request from the admin page, # which was actually a POST, but not from our page. So be careful to # check if it's a POST and it looks like our form. if request.method == 'POST' and 'subject' in request.POST: form = SponsorEmailForm(request.POST, initial=initial) if form.is_valid(): data = form.cleaned_data # Send emails one at a time, rendering the subject and # body as templates. for sponsor in sponsors: address_list = [] for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) mail = EmailMessage(subject=subject, body=body, from_email=data['from_'], to=address_list, cc=data['cc'].split(","), bcc=data['bcc'].split(",")) mail.send() messages.add_message(request, messages.INFO, _(u"Email sent to sponsors")) return redirect(reverse('admin:sponsorship_sponsor_changelist')) else: form = SponsorEmailForm(initial=initial) context = { 'address_list': address_list, 'form': form, 'pks': pks, 'sponsors': sponsors, } return render(request, "sponsorship/email.html", context)
def sponsor_email(request, pks): sponsors = Sponsor.objects.filter(pk__in=pks.split(",")) address_list = [] for sponsor in sponsors: if sponsor.contact_email.lower() not in address_list: address_list.append(sponsor.contact_email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) initial = { 'from_': config.SPONSOR_FROM_EMAIL, } # Note: on initial entry, we've got the request from the admin page, # which was actually a POST, but not from our page. So be careful to # check if it's a POST and it looks like our form. if request.method == 'POST' and 'subject' in request.POST: form = SponsorEmailForm(request.POST, initial=initial) if form.is_valid(): data = form.cleaned_data # Send emails one at a time, rendering the subject and # body as templates. for sponsor in sponsors: address_list = [] if sponsor.contact_email.lower() not in address_list: address_list.append(sponsor.contact_email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) mail = EmailMessage( subject=subject, body=body, from_email=data['from_'], to=address_list, cc=data['cc'].split(","), bcc=data['bcc'].split(",") ) mail.send() messages.add_message(request, messages.INFO, _(u"Email sent to sponsors")) return redirect(reverse('admin:sponsorship_sponsor_changelist')) else: form = SponsorEmailForm(initial=initial) context = { 'address_list': address_list, 'form': form, 'pks': pks, 'sponsors': sponsors, } return render(request, "sponsorship/email.html", context)
def sponsor_email(request, pks): sponsors = Sponsor.objects.filter(pk__in=pks.split(",")) address_list = [] for sponsor in sponsors: for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) initial = { 'from_': config.SPONSOR_FROM_EMAIL, } # Note: on initial entry, we've got the request from the admin page, # which was actually a POST, but not from our page. So be careful to # check if it's a POST and it looks like our form. if request.method == 'POST' and 'subject' in request.POST: form = SponsorEmailForm(request.POST, initial=initial) is_valid = form.is_valid() # If the user has just edited the Subject or Body, then show # them what it will look like after the %% substitutions are # done. Only once they like the look of the output, and # re-submit the form without any changes, do we hit Send. if is_valid: data = form.cleaned_data sponsor = sponsors[0] # any old sponsor will do subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) is_valid = ( subject == data['sample_subject'] and body == data['sample_body'] ) print repr(subject) print repr(data['sample_subject']) form.data = form.data.copy() form.data['sample_subject'] = subject form.data['sample_body'] = body if is_valid: # Send emails one at a time, rendering the subject and # body as templates. for sponsor in sponsors: address_list = [] for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) mail = EmailMessage( subject=subject, body=body, from_email=data['from_'], to=address_list, cc=data['cc'].split(","), bcc=data['bcc'].split(",") ) mail.send() messages.add_message(request, messages.INFO, _(u"Email sent to sponsors")) return redirect(reverse('admin:sponsorship_sponsor_changelist')) else: form = SponsorEmailForm(initial=initial) context = { 'address_list': address_list, 'form': form, 'pks': pks, 'sponsors': sponsors, } return render(request, "sponsorship/email.html", context)
def sponsor_email(request, pks): sponsors = Sponsor.objects.filter(pk__in=pks.split(",")) address_list = [] for sponsor in sponsors: for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) initial = { 'from_': config.SPONSOR_FROM_EMAIL, } # Note: on initial entry, we've got the request from the admin page, # which was actually a POST, but not from our page. So be careful to # check if it's a POST and it looks like our form. if request.method == 'POST' and 'subject' in request.POST: form = SponsorEmailForm(request.POST, initial=initial) is_valid = form.is_valid() # If the user has just edited the Subject or Body, then show # them what it will look like after the %% substitutions are # done. Only once they like the look of the output, and # re-submit the form without any changes, do we hit Send. if is_valid: data = form.cleaned_data sponsor = sponsors[0] # any old sponsor will do subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) is_valid = ( subject == data['sample_subject'] and body == data['sample_body'] ) form.data = form.data.copy() form.data['sample_subject'] = subject form.data['sample_body'] = body if is_valid and 'send' in request.POST: # Send emails one at a time, rendering the subject and # body as templates. for sponsor in sponsors: address_list = [] for email in sponsor.contact_emails: if email.lower() not in address_list: address_list.append(email.lower()) if sponsor.applicant.email.lower() not in address_list: address_list.append(sponsor.applicant.email.lower()) subject = sponsor.render_email(data['subject']) body = sponsor.render_email(data['body']) mail = EmailMessage( subject=subject, body=body, from_email=data['from_'], to=address_list, cc=data['cc'].split(","), bcc=data['bcc'].split(",") ) mail.send() messages.add_message(request, messages.INFO, _(u"Email sent to sponsors")) return redirect(reverse('admin:sponsorship_sponsor_changelist')) else: form = SponsorEmailForm(initial=initial) context = { 'address_list': address_list, 'form': form, 'pks': pks, 'sponsors': sponsors, } return render(request, "sponsorship/email.html", context)