def add_members(group_name, emails, user): res = {'status':False} try: group = Group.objects.get(name=group_name) is_public = group.public is_admin = False if user: membergroup = MemberGroup.objects.get(group=group, member=user) is_admin = membergroup.admin if is_public or is_admin: email_list = emails.strip().lower().split(',') for email in email_list: email = email.strip() email_user = UserProfile.objects.filter(email=email) member = False if email_user.count() == 1: member = MemberGroup.objects.filter(member=email_user[0], group=group).exists() if not member: mail = MailResponse(From = 'no-reply@' + BASE_URL, To = email, Subject = "You've been subscribed to %s Mailing List" % (group_name)) if email_user.count() == 1: _ = MemberGroup.objects.get_or_create(member=email_user[0], group=group) message = "You've been subscribed to %s Mailing List. <br />" % (group_name) message += "To see posts from this list, visit <a href='http://%s/posts?group_name=%s'>http://%s/posts?group_name=%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) message += "To manage your mailing list settings, subscribe, or unsubscribe, visit <a href='http://%s/groups/%s'>http://%s/groups/%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) else: pw = password_generator() new_user = UserProfile.objects.create_user(email, pw) _ = MemberGroup.objects.get_or_create(group=group, member=new_user) message = "You've been subscribed to %s Mailing List. <br />" % (group_name) message += "An account to manage your settings has been created for you at <a href='http://%s'>http://%s</a><br />" % (BASE_URL, BASE_URL) message += "Your username is your email, which is %s and your auto-generated password is %s <br />" % (email, pw) message += "If you would like to change your password, please log in at the link above and then you can change it under your settings. <br />" message += "To see posts from this list, visit <a href='http://%s/posts?group_name=%s'>http://%s/posts?group_name=%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) message += "To manage your mailing lists, subscribe, or unsubscribe from groups, visit <a href='http://%s/groups'>http://%s/my_groups</a><br />" % (BASE_URL, BASE_URL) mail.Html = message logging.debug('TO LIST: ' + str(email)) relay_mailer.deliver(mail, To = [email]) res['status'] = True else: res['code'] = msg_code['PRIVILEGE_ERROR'] except Group.DoesNotExist: res['code'] = msg_code['GROUP_NOT_FOUND_ERROR'] except MemberGroup.DoesNotExist: res['code'] = msg_code['NOT_MEMBER'] except: res['code'] = msg_code['UNKNOWN_ERROR'] logging.debug(res) return res
def save(self, domain_override=None, subject_template_name='registration/password_reset_subject.txt', email_template_name='registration/password_reset_email.html', extra_email_context=None, html_email_template_name="", use_https=False, token_generator=default_token_generator, from_email=None, request=None, *args, **kwargs): """ Generates a one-use only link for resetting password and sends to the user. """ logger.info("reset form") from django.core.mail import send_mail UserModel = get_user_model() email = self.cleaned_data["email"] active_users = UserModel._default_manager.filter(email__iexact=email, is_active=True) for user in active_users: # Make sure that no email is sent to a user that actually has # a password marked as unusable if not user.has_usable_password(): continue if not domain_override: domain = get_current_site(request).domain else: domain = domain_override c = { 'email': user.email, 'domain': domain, 'site_name': WEBSITE.title(), 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'user': user, 'token': token_generator.make_token(user), 'protocol': 'https' if use_https else 'http', } subject = loader.render_to_string(subject_template_name, c) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) email = loader.render_to_string(email_template_name, c) from smtp_handler.utils import relay_mailer from lamson.mail import MailResponse mail = MailResponse(From=DEFAULT_FROM_EMAIL, To=user.email, Subject=subject, Body=email) relay_mailer.deliver(mail, To=user.email)
def email_user(self, subject, message, from_email=None): """ Sends an email to this User. """ from smtp_handler.utils import relay_mailer from lamson.mail import MailResponse mail = MailResponse(From=DEFAULT_FROM_EMAIL, To=self.email, Subject=subject, Body=message) relay_mailer.deliver(mail, To=self.email)
def add_members(group_name, emails, user): res = {'status':False} try: group = Group.objects.get(name=group_name) membergroup = MemberGroup.objects.get(group=group, member=user) if membergroup.admin: email_list = emails.strip().lower().split(',') for email in email_list: email = email.strip() mail = MailResponse(From = 'no-reply@' + BASE_URL, To = email, Subject = "You've been subscribed to %s Mailing List" % (group_name)) email_user = UserProfile.objects.filter(email=email) if email_user.count() == 1: _ = MemberGroup.objects.get_or_create(member=email_user[0], group=group) message = "You've been subscribed to %s Mailing List. <br />" % (group_name) message += "To see posts from this list, visit <a href='http://%s/posts?group_name=%s'>http://%s/posts?group_name=%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) message += "To manage your mailing list settings, subscribe, or unsubscribe, visit <a href='http://%s/groups/%s'>http://%s/groups/%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) else: pw = password_generator() new_user = UserProfile.objects.create_user(email, pw) _ = MemberGroup.objects.get_or_create(group=group, member=new_user) message = "You've been subscribed to %s Mailing List. <br />" % (group_name) message += "An account has been created for you at <a href='http://%s'>http://%s</a><br />" % (BASE_URL, BASE_URL) message += "Your username is your email, which is %s and your auto-generated password is %s <br />" % (email, pw) message += "If you would like to change your password, please log in at the link above and then you can change it under your settings. <br />" message += "To see posts from this list, visit <a href='http://%s/posts?group_name=%s'>http://%s/posts?group_name=%s</a><br />" % (BASE_URL, group_name, BASE_URL, group_name) message += "To manage your mailing lists, subscribe, or unsubscribe from groups, visit <a href='http://%s/groups'>http://%s/my_groups</a><br />" % (BASE_URL, BASE_URL) mail.Html = message logging.debug('TO LIST: ' + str(email)) relay_mailer.deliver(mail, To = [email]) res['status'] = True else: res['code'] = msg_code['PRIVILEGE_ERROR'] except Group.DoesNotExist: res['code'] = msg_code['GROUP_NOT_FOUND_ERROR'] except MemberGroup.DoesNotExist: res['code'] = msg_code['NOT_MEMBER'] except: res['code'] = msg_code['UNKNOWN_ERROR'] logging.debug(res) return res
def upvote(post_id, email=None, user=None): res = {'status':False} p = None try: if email: user = UserProfile.objects.get(email=email) #Email the owner of the post when upvoted email_addr = email.strip() mail = MailResponse(From = 'no-reply@' + BASE_URL, To = email_addr, Subject = "Your Post Has Been Upvoted") #email_user = UserProfile.objects.filter(email=email_addr) message = "Your post %s has been upvoted by %s" % (post_id, user) mail.Html = message logging.debug('TO LIST: ' + str(email_addr)) relay_mailer.deliver(mail, To = [email_addr]) p = Post.objects.get(id=int(post_id)) l = Upvote.objects.get(post=p, user=user) res['status'] = True res['thread_id'] = p.thread.id res['post_name'] = p.subject res['post_id'] = p.id res['group_name'] = p.group.name except UserProfile.DoesNotExist: res['code'] = msg_code['USER_DOES_NOT_EXIST'] % email except Upvote.DoesNotExist: l = Upvote(post=p, user=user) l.save() res['status'] = True res['thread_id'] = p.thread.id res['post_name'] = p.subject res['post_id'] = p.id res['group_name'] = p.group.name except: res['code'] = msg_code['UNKNOWN_ERROR'] logging.debug(res) return res
def upvote(post_id, email=None, user=None): res = {'status': False} p = None try: if email: user = UserProfile.objects.get(email=email) #Email the owner of the post when upvoted email_addr = email.strip() mail = MailResponse(From='no-reply@' + BASE_URL, To=email_addr, Subject="Your Post Has Been Upvoted") #email_user = UserProfile.objects.filter(email=email_addr) message = "Your post %s has been upvoted by %s" % (post_id, user) mail.Html = message logging.debug('TO LIST: ' + str(email_addr)) relay_mailer.deliver(mail, To=[email_addr]) p = Post.objects.get(id=int(post_id)) l = Upvote.objects.get(post=p, user=user) res['status'] = True res['thread_id'] = p.thread.id res['post_name'] = p.subject res['post_id'] = p.id res['group_name'] = p.group.name except UserProfile.DoesNotExist: res['code'] = msg_code['USER_DOES_NOT_EXIST'] % email except Upvote.DoesNotExist: l = Upvote(post=p, user=user) l.save() res['status'] = True res['thread_id'] = p.thread.id res['post_name'] = p.subject res['post_id'] = p.id res['group_name'] = p.group.name except: res['code'] = msg_code['UNKNOWN_ERROR'] logging.debug(res) return res