def _send_mail(locale): subject = _(u'[SUMO] You have a new private message from [{sender}]') subject = subject.format(sender=inbox_message.sender.username) context = { 'sender': inbox_message.sender.username, 'message': inbox_message.message, 'url': reverse('messages.read', kwargs={'msgid': inbox_message.id}), 'unsubscribe_url': reverse('users.edit_settings'), 'host': Site.objects.get_current().domain } text_template = 'messages/email/private_message.ltxt' html_template = None msg = EmailMultiAlternatives(subject, render_email(text_template, context), settings.TIDINGS_FROM_ADDRESS, [inbox_message.to.email]) if html_template: msg.attach_alternative(render_email(html_template, context), 'text/html') send_messages([msg])
def _send_mail(locale): subject = _(u'[SUMO] You have a new private message from [{sender}]') subject = subject.format(sender=inbox_message.sender.username) context = { 'sender': inbox_message.sender.username, 'message': inbox_message.message, 'url': reverse('messages.read', kwargs={'msgid': inbox_message.id}), 'unsubscribe_url': reverse('users.edit_settings'), 'host': Site.objects.get_current().domain} text_template = 'messages/email/private_message.ltxt' html_template = None msg = EmailMultiAlternatives( subject, render_email(text_template, context), settings.TIDINGS_FROM_ADDRESS, [inbox_message.to.email]) if html_template: msg.attach_alternative( render_email(html_template, context), 'text/html') send_messages([msg])
def send_group_email(announcement_id): """Build and send the announcement emails to a group.""" try: announcement = Announcement.objects.get(pk=announcement_id) except Announcement.DoesNotExist: return group = announcement.group users = User.objects.filter(groups__in=[group]) plain_content = bleach.clean(announcement.content_parsed, tags=[], strip=True).strip() email_kwargs = {"content": plain_content, "domain": Site.objects.get_current().domain} text_template = "announcements/email/announcement.ltxt" html_template = None @safe_translation def _make_mail(locale, user): subject = _("New announcement for {group}").format(group=group.name) msg = EmailMultiAlternatives( subject, render_email(text_template, email_kwargs), settings.TIDINGS_FROM_ADDRESS, [user.email] ) if html_template: msg.attach_alternative(render_email(html_template, email_kwargs), "text/html") return msg messages = [] for u in users: # Localize email each time. locale = u.profile.locale or settings.LANGUAGE_CODE messages.append(_make_mail(locale, u)) send_messages(messages)
def resend_confirmation(request, template): """Resend confirmation email.""" if request.method == 'POST': form = EmailConfirmationForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] try: reg_prof = RegistrationProfile.objects.get( user__email=email) if not reg_prof.user.is_active: form = try_send_email_with_form( RegistrationProfile.objects.send_confirmation_email, form, 'email', reg_prof) else: form = try_send_email_with_form( RegistrationProfile.objects.send_confirmation_email, form, 'email', reg_prof, text_template='users/email/already_activated.ltxt', html_template='users/email/already_activated.html', subject=_('Account already activated')) except RegistrationProfile.DoesNotExist: # Send already active email if user exists try: user = User.objects.get(email=email, is_active=True) current_site = Site.objects.get_current() email_kwargs = {'domain': current_site.domain, 'login_url': reverse('users.login')} subject = _('Account already activated') @email_utils.safe_translation def _make_mail(locale): mail = email_utils.make_mail( subject=subject, text_template='users/email/already_activated.ltxt', html_template='users/email/already_activated.html', context_vars=email_kwargs, from_email=settings.DEFAULT_FROM_EMAIL, to_email=user.email) return mail email_utils.send_messages( [_make_mail(request.LANGUAGE_CODE)]) except User.DoesNotExist: # Don't leak existence of email addresses. pass # Form may now be invalid if email failed to send. if form.is_valid(): return render( request, template + 'resend_confirmation_done.html', {'email': email}) else: form = EmailConfirmationForm() return render(request, template + 'resend_confirmation.html', { 'form': form})
def _send_mail(locale, user, context): subject = _('Your username on %s') % site_name mail = email_utils.make_mail( subject=subject, text_template=text_template, html_template=html_template, context_vars=context, from_email=settings.TIDINGS_FROM_ADDRESS, to_email=user.email) email_utils.send_messages([mail])
def send_contributor_notification(based_on, revision, document, message): """Send notification of review to the contributors of revisions.""" text_template = 'wiki/email/reviewed_contributors.ltxt' html_template = 'wiki/email/reviewed_contributors.html' url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'revisions_url': url, 'host': Site.objects.get_current().domain} msgs = [] @email_utils.safe_translation def _make_mail(locale, user): if revision.is_approved: subject = _(u'A revision you contributed to has ' 'been approved: {title}') else: subject = _(u'A revision you contributed to has ' 'been reviewed: {title}') subject = subject.format(title=document.title) mail = email_utils.make_mail( subject=subject, text_template=text_template, html_template=html_template, context_vars=c, from_email=settings.TIDINGS_FROM_ADDRESS, to_email=user.email) msgs.append(mail) for r in based_on: # Send email to all contributors except the reviewer and the creator # of the approved revision. if r.creator in [revision.creator, revision.reviewer]: continue user = r.creator if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE _make_mail(locale, user) email_utils.send_messages(msgs)
def _send_mail(locale, user, context): subject = _('Your username on %s') % site_name msg = EmailMultiAlternatives( subject, email_utils.render_email(text_template, context), settings.TIDINGS_FROM_ADDRESS, [user.email]) if html_template: msg.attach_alternative( email_utils.render_email(html_template, context), 'text/html') email_utils.send_messages([msg])
def save(self, domain_override=None, subject_template_name='registration/password_reset_subject.txt', text_template=None, html_template=None, use_https=False, token_generator=default_token_generator, from_email=None, request=None): """ Based off of django's but uses jingo and handles html and plain-text emails """ for user in self.users_cache: if not domain_override: current_site = get_current_site(request) site_name = current_site.name domain = current_site.domain else: site_name = domain = domain_override c = { 'email': user.email, 'domain': domain, 'site_name': site_name, 'uid': int_to_base36(user.id), 'user': user, 'token': token_generator.make_token(user), 'protocol': use_https and 'https' or 'http', } subject = email_utils.render_email(subject_template_name, c) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) @email_utils.safe_translation def _make_mail(locale): mail = email_utils.make_mail( subject=subject, text_template=text_template, html_template=html_template, context_vars=c, from_email=from_email, to_email=user.email) return mail if request: locale = request.LANGUAGE_CODE else: locale = settings.WIKI_DEFAULT_LANGUAGE email_utils.send_messages([_make_mail(locale)])
def _send_mail(locale, user, context): subject = _('Your username on %s') % site_name msg = EmailMultiAlternatives( subject, email_utils.render_email(text_template, context), settings.TIDINGS_FROM_ADDRESS, [user.email]) if html_template: msg.attach_alternative( email_utils.render_email( html_template, context), 'text/html') email_utils.send_messages([msg])
def send_reviewed_notification(revision, document, message): """Send notification of review to the revision creator.""" if revision.reviewer == revision.creator: log.debug('Revision (id=%s) reviewed by creator, skipping email' % revision.id) return log.debug('Sending reviewed email for revision (id=%s)' % revision.id) url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = { 'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'url': url, 'host': Site.objects.get_current().domain } msgs = [] @email_utils.safe_translation def _make_mail(locale, user): if revision.is_approved: subject = _(u'Your revision has been approved: {title}') else: subject = _(u'Your revision has been reviewed: {title}') subject = subject.format(title=document.title) template = 'wiki/email/reviewed.ltxt' msg = email_utils.render_email(template, c) msgs.append( EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])) for user in [revision.creator, revision.reviewer]: if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE _make_mail(locale, user) email_utils.send_messages(msgs)
def send_reviewed_notification(revision, document, message): """Send notification of review to the revision creator.""" if revision.reviewer == revision.creator: log.debug('Revision (id=%s) reviewed by creator, skipping email' % revision.id) return log.debug('Sending reviewed email for revision (id=%s)' % revision.id) url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'revisions_url': url, 'host': Site.objects.get_current().domain} msgs = [] @email_utils.safe_translation def _make_mail(locale, user): if revision.is_approved: subject = _(u'Your revision has been approved: {title}') else: subject = _(u'Your revision has been reviewed: {title}') subject = subject.format(title=document.title) mail = email_utils.make_mail( subject=subject, text_template='wiki/email/reviewed.ltxt', html_template='wiki/email/reviewed.html', context_vars=c, from_email=settings.TIDINGS_FROM_ADDRESS, to_email=user.email) msgs.append(mail) for user in [revision.creator, revision.reviewer]: if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE _make_mail(locale, user) email_utils.send_messages(msgs)
def send_contributor_notification(based_on, revision, document, message): """Send notification of review to the contributors of revisions.""" template = 'wiki/email/reviewed_contributors.ltxt' url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'url': url, 'host': Site.objects.get_current().domain} msgs = [] for r in based_on: # Send email to all contributors except the reviewer and the creator # of the approved revision. if r.creator in [revision.creator, revision.reviewer]: continue user = r.creator if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE with email_utils.uselocale(locale): if revision.is_approved: subject = _(u'A revision you contributed to has ' 'been approved: {title}') else: subject = _(u'A revision you contributed to has ' 'been reviewed: {title}') subject = subject.format(title=document.title) msg = email_utils.render_email(template, c) msgs.append(EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])) email_utils.send_messages(msgs)
def send_reviewed_notification(revision, document, message): """Send notification of review to the revision creator.""" if revision.reviewer == revision.creator: log.debug('Revision (id=%s) reviewed by creator, skipping email' % revision.id) return log.debug('Sending reviewed email for revision (id=%s)' % revision.id) url = reverse('wiki.document_revisions', locale=document.locale, args=[document.slug]) c = {'document_title': document.title, 'approved': revision.is_approved, 'reviewer': revision.reviewer, 'message': message, 'url': url, 'host': Site.objects.get_current().domain} msgs = [] for user in [revision.creator, revision.reviewer]: if hasattr(user, 'profile'): locale = user.profile.locale else: locale = settings.WIKI_DEFAULT_LANGUAGE with email_utils.uselocale(locale): if revision.is_approved: subject = _(u'Your revision has been approved: {title}') else: subject = _(u'Your revision has been reviewed: {title}') subject = subject.format(title=document.title) template = 'wiki/email/reviewed.ltxt' msg = email_utils.render_email(template, c) msgs.append(EmailMessage(subject, msg, settings.TIDINGS_FROM_ADDRESS, [user.email])) email_utils.send_messages(msgs)
def send_group_email(announcement_id): """Build and send the announcement emails to a group.""" try: announcement = Announcement.objects.get(pk=announcement_id) except Announcement.DoesNotExist: return group = announcement.group users = User.objects.filter(groups__in=[group]) plain_content = bleach.clean(announcement.content_parsed, tags=[], strip=True).strip() email_kwargs = { 'content': plain_content, 'domain': Site.objects.get_current().domain } text_template = 'announcements/email/announcement.ltxt' html_template = None @safe_translation def _make_mail(locale, user): subject = _('New announcement for {group}').format(group=group.name) msg = EmailMultiAlternatives(subject, render_email(text_template, email_kwargs), settings.TIDINGS_FROM_ADDRESS, [user.email]) if html_template: msg.attach_alternative(render_email(html_template, email_kwargs), 'text/html') return msg messages = [] for u in users: # Localize email each time. locale = u.profile.locale or settings.LANGUAGE_CODE messages.append(_make_mail(locale, u)) send_messages(messages)
def _send_mail(locale): subject = _(u'[SUMO] You have a new private message from [{sender}]') subject = subject.format(sender=inbox_message.sender.username) context = { 'sender': inbox_message.sender.username, 'message': inbox_message.message, 'message_html': inbox_message.content_parsed, 'message_url': reverse('messages.read', kwargs={'msgid': inbox_message.id}), 'unsubscribe_url': reverse('users.edit_settings'), 'host': Site.objects.get_current().domain} mail = make_mail(subject=subject, text_template='messages/email/private_message.ltxt', html_template='messages/email/private_message.html', context_vars=context, from_email=settings.TIDINGS_FROM_ADDRESS, to_email=inbox_message.to.email) send_messages([mail])
def _send_email(self, confirmation_profile, url, subject, text_template, html_template, send_to, **kwargs): """ Send an email using a passed in confirmation profile. Use specified url, subject, text_template, html_template and email to send_to. """ current_site = Site.objects.get_current() email_kwargs = {'activation_key': confirmation_profile.activation_key, 'domain': current_site.domain, 'activate_url': url, 'login_url': reverse('users.login')} email_kwargs.update(kwargs) # RegistrationProfile doesn't have a locale attribute. So if # we get one of those, then we have to get the real profile # from the user. if hasattr(confirmation_profile, 'locale'): locale = confirmation_profile.locale else: locale = confirmation_profile.user.profile.locale @email_utils.safe_translation def _make_mail(locale): msg = EmailMultiAlternatives( subject, email_utils.render_email(text_template, email_kwargs), settings.DEFAULT_FROM_EMAIL, [send_to]) if html_template: msg.attach_alternative( email_utils.render_email( html_template, email_kwargs), 'text/html') return msg email_utils.send_messages([_make_mail(locale)])
def _send_email(self, confirmation_profile, url, subject, text_template, html_template, send_to, **kwargs): """ Send an email using a passed in confirmation profile. Use specified url, subject, text_template, html_template and email to send_to. """ current_site = Site.objects.get_current() email_kwargs = { 'activation_key': confirmation_profile.activation_key, 'domain': current_site.domain, 'activate_url': url, 'login_url': reverse('users.login') } email_kwargs.update(kwargs) # RegistrationProfile doesn't have a locale attribute. So if # we get one of those, then we have to get the real profile # from the user. if hasattr(confirmation_profile, 'locale'): locale = confirmation_profile.locale else: locale = confirmation_profile.user.profile.locale @email_utils.safe_translation def _make_mail(locale): msg = EmailMultiAlternatives( subject, email_utils.render_email(text_template, email_kwargs), settings.DEFAULT_FROM_EMAIL, [send_to]) if html_template: msg.attach_alternative( email_utils.render_email(html_template, email_kwargs), 'text/html') return msg email_utils.send_messages([_make_mail(locale)])
def send_group_email(announcement_id): """Build and send the announcement emails to a group.""" try: announcement = Announcement.objects.get(pk=announcement_id) except Announcement.DoesNotExist: return group = announcement.group users = User.objects.filter(groups__in=[group]) plain_content = bleach.clean(announcement.content_parsed, tags=[], strip=True).strip() email_kwargs = {'content': plain_content, 'content_html': announcement.content_parsed, 'domain': Site.objects.get_current().domain} text_template = 'announcements/email/announcement.ltxt' html_template = 'announcements/email/announcement.html' @safe_translation def _make_mail(locale, user): subject = _('New announcement for {group}').format( group=group.name) mail = make_mail(subject=subject, text_template=text_template, html_template=html_template, context_vars=email_kwargs, from_email=settings.TIDINGS_FROM_ADDRESS, to_email=user.email) return mail messages = [] for u in users: # Localize email each time. locale = u.profile.locale or settings.LANGUAGE_CODE messages.append(_make_mail(locale, u)) send_messages(messages)
def make_contributor(request): """Adds the logged in user to the contributor group""" group = Group.objects.get(name=CONTRIBUTOR_GROUP) request.user.groups.add(group) @email_utils.safe_translation def _make_mail(locale): mail = email_utils.make_mail( subject=_('Welcome to SUMO!'), text_template='users/email/contributor.ltxt', html_template='users/email/contributor.html', context_vars={'username': request.user.username}, from_email=settings.DEFAULT_FROM_EMAIL, to_email=request.user.email) return mail email_utils.send_messages([_make_mail(request.LANGUAGE_CODE)]) if 'return_to' in request.POST: return HttpResponseRedirect(request.POST['return_to']) else: return HttpResponseRedirect(reverse('landings.get_involved'))