def send_optout_message(self): email = self.cleaned_data.get('email') mobile = self.cleaned_data.get('mobile') if email: for application in Application.objects.active().filter(email__iexact=email): # send opt-out email context = { 'application': application } subject = render_to_string_ctx('studygroups/email/optout_confirm-subject.txt', context).strip('\n') html_body = render_html_with_css('studygroups/email/optout_confirm.html', context) text_body = render_to_string_ctx('studygroups/email/optout_confirm.txt', context) notification = EmailMultiAlternatives(subject, text_body, settings.DEFAULT_FROM_EMAIL, [application.email]) notification.attach_alternative(html_body, 'text/html') notification.send() # Find all signups with mobile with email and delete if mobile: applications = Application.objects.active().filter(mobile=mobile) if email: # don't send text to applications with a valid email in opt out form applications = applications.exclude(email__iexact=email) for application in applications: # This remains for old signups without email address context = { 'application': application } message = render_to_string_ctx('studygroups/email/optout_confirm_text.txt', context) try: send_message(application.mobile, message) except twilio.TwilioRestException as e: logger.exception("Could not send text message to %s", to, exc_info=e) application.delete()
def send_reminder(reminder): to = [su.email for su in reminder.study_group.application_set.filter(accepted_at__isnull=False, contact_method=Application.EMAIL)] if reminder.study_group_meeting: # this is a reminder and we need RSVP links for email in to: # TODO hardcoded domain domain = 'https://chicago.p2pu.org' url = reverse('studygroups_rsvp') yes_qs = rsvp.gen_rsvp_querystring( email, reminder.study_group.pk, reminder.study_group_meeting.meeting_time, 'yes' ) yes_link = '{0}{1}?{2}'.format(domain,url,yes_qs) no_qs = rsvp.gen_rsvp_querystring( email, reminder.study_group.pk, reminder.study_group_meeting.meeting_time, 'no' ) no_link = '{0}{1}?{2}'.format(domain,url,no_qs) email_body = reminder.email_body email_body = re.sub(r'<!--RSVP:YES.*-->', yes_link, email_body) email_body = re.sub(r'<!--RSVP:NO.*-->', no_link, email_body) send_mail( reminder.email_subject.strip('\n'), email_body, settings.DEFAULT_FROM_EMAIL, [email], fail_silently=False ) else: send_mail(reminder.email_subject.strip('\n'), reminder.email_body, settings.DEFAULT_FROM_EMAIL, to, fail_silently=False) # send SMS tos = [su.mobile for su in reminder.study_group.application_set.filter(accepted_at__isnull=False, contact_method=Application.TEXT)] errors = [] for to in tos: if reminder.study_group_meeting: #TODO - insert RSVP link send_message(to, reminder.sms_body) try: send_message(to, reminder.sms_body) except twilio.TwilioRestException as e: errors.push[e] if len(errors): #TODO: log errors raise Exception(errors) reminder.sent_at = timezone.now() reminder.save()
def send_reminder(reminder): # mark it as sent, we don't retry any failures reminder.sent_at = timezone.now() reminder.save() to = [ su.email for su in reminder.study_group.application_set.active().filter( accepted_at__isnull=False).exclude(email='') ] if reminder.study_group_meeting: send_meeting_reminder(reminder) else: context = { "reminder": reminder, "facilitator_message": reminder.email_body, "domain": 'https://{0}'.format(settings.DOMAIN), } subject, text_body, html_body = render_email_templates( 'studygroups/email/facilitator_message', context) to += [reminder.study_group.facilitator.email] sender = '{0} <{1}>'.format( reminder.study_group.facilitator.first_name, settings.DEFAULT_FROM_EMAIL) try: reminder_email = EmailMultiAlternatives( reminder.email_subject.strip('\n'), text_body, sender, [], bcc=to, reply_to=[reminder.study_group.facilitator.email], ) reminder_email.attach_alternative(html_body, 'text/html') reminder_email.send() except Exception as e: logger.exception('Could not send reminder to whole study group', exc_info=e) # send SMS if reminder.sms_body != '': applications = reminder.study_group.application_set.active().filter( accepted_at__isnull=False).exclude(mobile='') applications = applications.filter(mobile_opt_out_at__isnull=True) tos = [su.mobile for su in applications] for to in tos: try: send_message(to, reminder.sms_body) except TwilioRestException as e: logger.exception("Could not send text message to %s", to, exc_info=e)
def send_meeting_change_notification(old_meeting, new_meeting): study_group = new_meeting.study_group to = [ su.email for su in study_group.application_set.active().filter( accepted_at__isnull=False).exclude(email='') ] context = { 'old_meeting': old_meeting, 'new_meeting': new_meeting, 'learning_circle': study_group, } with use_language(study_group.language), timezone.override( pytz.timezone(study_group.timezone)): subject = render_to_string_ctx( 'studygroups/email/meeting_changed-subject.txt', context).strip('\n') html_body = render_to_string_ctx( 'studygroups/email/meeting_changed.html', context) text_body = html_body_to_text(html_body) sms_body = render_to_string_ctx( 'studygroups/email/meeting_changed-sms.txt', context).strip('\n') notification = EmailMultiAlternatives(subject, text_body, settings.DEFAULT_FROM_EMAIL, bcc=to) notification.attach_alternative(html_body, 'text/html') try: notification.send() except Exception as e: logger.exception('Could not send meeting change notification', exc_info=e) applications = study_group.application_set.active().filter( accepted_at__isnull=False).exclude(mobile='') applications = applications.filter(mobile_opt_out_at__isnull=True) tos = [su.mobile for su in applications] for to in tos: try: send_message(to, sms_body) except TwilioRestException as e: logger.exception("Could not send text message to %s", to, exc_info=e)
def send_reminder(reminder): # mark it as sent, we don't retry any failures reminder.sent_at = timezone.now() reminder.save() to = [ su.email for su in reminder.study_group.application_set.active().filter( accepted_at__isnull=False).exclude(email='') ] if reminder.study_group_meeting: # this is a reminder and we need RSVP links for email in to: yes_link = reminder.study_group_meeting.rsvp_yes_link(email) no_link = reminder.study_group_meeting.rsvp_no_link(email) application = reminder.study_group_meeting.study_group.application_set.active( ).filter(email__iexact=email).first() unsubscribe_link = application.unapply_link() email_body = reminder.email_body email_body = re.sub(r'\(<!--RSVP:YES-->.*\)', yes_link, email_body) email_body = re.sub(r'\(<!--RSVP:NO-->.*\)', no_link, email_body) email_body = re.sub(r'\(<!--UNSUBSCRIBE-->.*\)', unsubscribe_link, email_body) try: reminder_email = EmailMultiAlternatives( reminder.email_subject.strip('\n'), email_body, settings.DEFAULT_FROM_EMAIL, [email], reply_to=[reminder.study_group.facilitator.email], ) reminder_email.send() except Exception as e: logger.exception('Could not send email to ', email, exc_info=e) # Send to organizer without RSVP & unsubscribe links try: url = reverse('studygroups_facilitator') dash_link = 'https://{}{}'.format(settings.DOMAIN, url) email_body = reminder.email_body email_body = re.sub(r'\(<!--RSVP:YES-->.*\)', dash_link, email_body) email_body = re.sub(r'\(<!--RSVP:NO-->.*\)', dash_link, email_body) email_body = re.sub(r'\(<!--UNSUBSCRIBE-->.*\)', dash_link, email_body) send_mail(reminder.email_subject.strip('\n'), email_body, settings.DEFAULT_FROM_EMAIL, [reminder.study_group.facilitator.email], fail_silently=False) except Exception as e: logger.exception('Could not send email to ', reminder.study_group.facilitator.email, exc_info=e) else: email_body = reminder.email_body # TODO i18n email_body = '{0}\n\nTo leave this learning circle and stop receiving messages, click here: https://{1}{2}'.format( email_body, settings.DOMAIN, reverse('studygroups_optout')) # TODO - all emails should contain the unsubscribe link to += [reminder.study_group.facilitator.email] try: reminder_email = EmailMultiAlternatives( reminder.email_subject.strip('\n'), email_body, settings.DEFAULT_FROM_EMAIL, [], bcc=to, reply_to=[reminder.study_group.facilitator.email], ) reminder_email.send() except Exception as e: logger.exception('Could not send reminder to whole study group', exc_info=e) # send SMS if reminder.sms_body != '': applications = reminder.study_group.application_set.active().filter( accepted_at__isnull=False).exclude(mobile='') applications = applications.filter(mobile_opt_out_at__isnull=True) tos = [su.mobile for su in applications] for to in tos: try: send_message(to, reminder.sms_body) except TwilioRestException as e: logger.exception("Could not send text message to %s", to, exc_info=e)