def mail_moderators(subject_line='', body_text='', raise_on_failure=False, headers=None): """sends email to forum moderators and admins """ body_text = absolutize_urls(body_text) from django.db.models import Q from askbot.models import User recipient_list = User.objects.filter(Q(status='m') | Q(is_superuser=True)).filter( is_active=True).values_list( 'email', flat=True) recipient_list = set(recipient_list) from_email = '' if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'): from_email = django_settings.DEFAULT_FROM_EMAIL try: msg = mail.EmailMessage(subject_line, body_text, from_email, recipient_list, headers=headers or {}) msg.content_subtype = 'html' msg.send() except smtplib.SMTPException, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail(subject_line=None, body_text=None, from_email=None, recipient_list=None, headers=None, raise_on_failure=False, attachments=None): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file if raise_on_failure is True, exceptions.EmailNotSent is raised `attachments` is a tuple of triples ((filename, filedata, mimetype), ...) """ from_email = from_email or askbot_settings.ADMIN_EMAIL \ or django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert (subject_line is not None) subject_line = prefix_the_subject_line(subject_line) _send_mail(subject_line, body_text, from_email, recipient_list, headers=headers, attachments=attachments) logging.debug('sent update to %s' % ','.join(recipient_list)) except Exception, error: sys.stderr.write('\n' + unicode(error).encode('utf-8') + '\n') if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line = None, body_text = None, from_email = django_settings.DEFAULT_FROM_EMAIL, recipient_list = None, activity_type = None, related_object = None, headers = None, raise_on_failure = False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ body_text = absolutize_urls(body_text) try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) msg = mail.EmailMultiAlternatives( subject_line, clean_html_email(body_text), from_email, recipient_list, headers = headers ) msg.attach_alternative(body_text, "text/html") msg.send() logging.debug('sent update to %s' % ','.join(recipient_list)) if related_object is not None: assert(activity_type is not None) except Exception, error: sys.stderr.write('\n' + unicode(error).encode('utf-8') + '\n') if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line = None, body_text = None, from_email = django_settings.DEFAULT_FROM_EMAIL, recipient_list = None, activity_type = None, related_object = None, headers = None, raise_on_failure = False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ try: assert(subject_line is not None) subject_line = prefix_the_subject_line(subject_line) msg = mail.EmailMessage( subject_line, body_text, from_email, recipient_list, headers = headers ) msg.content_subtype = 'html' msg.send() if related_object is not None: assert(activity_type is not None) except Exception, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def mail_moderators(subject_line='', body_text='', raise_on_failure=False): """sends email to forum moderators and admins """ from django.db.models import Q from askbot.models import User recipient_list = User.objects.filter(Q(status='m') | Q(is_superuser=True)).values_list( 'email', flat=True) recipient_list = set(recipient_list) from_email = '' if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'): from_email = django_settings.DEFAULT_FROM_EMAIL try: mail.send_mail(subject_line, body_text, from_email, recipient_list) pass except smtplib.SMTPException, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line = None, body_text = None, recipient_list = None, activity_type = None, related_object = None, headers = None, raise_on_failure = False, ): """sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ prefix = askbot_settings.EMAIL_SUBJECT_PREFIX.strip() + ' ' try: assert(subject_line is not None) subject_line = prefix + subject_line msg = mail.EmailMessage( subject_line, body_text, django_settings.DEFAULT_FROM_EMAIL, recipient_list, headers = headers ) msg.content_subtype = 'html' msg.send() if related_object is not None: assert(activity_type is not None) except Exception, error: logging.critical(unicode(error)) if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))
def send_mail( subject_line=None, body_text=None, from_email=None, recipient_list=None, activity_type=None, related_object=None, headers=None, raise_on_failure=False, ): """ todo: remove parameters not relevant to the function sends email message logs email sending activity and any errors are reported as critical in the main log file related_object is not mandatory, other arguments are. related_object (if given, will be saved in the activity record) if raise_on_failure is True, exceptions.EmailNotSent is raised """ from_email = from_email or askbot_settings.ADMIN_EMAIL or \ django_settings.DEFAULT_FROM_EMAIL body_text = absolutize_urls(body_text) try: assert (subject_line is not None) subject_line = prefix_the_subject_line(subject_line) debug_emails = [] if hasattr(django_settings, 'DEBUG_EMAIL_USERS'): debug_emails = django_settings.DEBUG_EMAIL_USERS if len(debug_emails) > 0: new_recipient_list = [] # Only allow recipients in the debug email list for allowed in debug_emails: if allowed in recipient_list: new_recipient_list.append(allowed) if len(new_recipient_list) == 0: return recipient_list = new_recipient_list logging.debug("Only Email Debug Users %s" % recipient_list) msg = mail.EmailMultiAlternatives(subject_line, clean_html_email(body_text), from_email, recipient_list, headers=headers) msg.attach_alternative(body_text, "text/html") msg.send() logging.debug('sent update to %s' % ','.join(recipient_list)) if related_object is not None: assert (activity_type is not None) except Exception, error: sys.stderr.write('\n' + unicode(error).encode('utf-8') + '\n') if raise_on_failure == True: raise exceptions.EmailNotSent(unicode(error))