PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/askbot/utils/mail.py

https://github.com/chandankumar2199/askbot-devel
Python | 137 lines | 112 code | 3 blank | 22 comment | 5 complexity | 3a9e52354351398987ca8170d219e3bf MD5 | raw file
  1. """functions that send email in askbot
  2. these automatically catch email-related exceptions
  3. """
  4. import smtplib
  5. import logging
  6. from django.core import mail
  7. from django.conf import settings as django_settings
  8. from askbot.conf import settings as askbot_settings
  9. from askbot import exceptions
  10. from askbot import const
  11. #todo: maybe send_mail functions belong to models
  12. #or the future API
  13. def prefix_the_subject_line(subject):
  14. """prefixes the subject line with the
  15. EMAIL_SUBJECT_LINE_PREFIX either from
  16. from live settings, which take default from django
  17. """
  18. prefix = askbot_settings.EMAIL_SUBJECT_PREFIX.strip()
  19. if prefix != '':
  20. subject = prefix + ' ' + subject
  21. return subject
  22. def extract_first_email_address(text):
  23. """extract first matching email address
  24. from text string
  25. returns ``None`` if there are no matches
  26. """
  27. match = const.EMAIL_REGEX.search(text)
  28. if match:
  29. return match.group(0)
  30. else:
  31. return None
  32. def thread_headers(post, orig_post, update):
  33. suffix_id = django_settings.SERVER_EMAIL
  34. if update == const.TYPE_ACTIVITY_ASK_QUESTION:
  35. id = "NQ-%s-%s" % (post.id, suffix_id)
  36. headers = {'Message-ID': id}
  37. elif update == const.TYPE_ACTIVITY_ANSWER:
  38. id = "NA-%s-%s" % (post.id, suffix_id)
  39. orig_id = "NQ-%s-%s" % (orig_post.id, suffix_id)
  40. headers = {'Message-ID': id,
  41. 'In-Reply-To': orig_id}
  42. elif update == const.TYPE_ACTIVITY_UPDATE_QUESTION:
  43. id = "UQ-%s-%s-%s" % (post.id, post.last_edited_at, suffix_id)
  44. orig_id = "NQ-%s-%s" % (orig_post.id, suffix_id)
  45. headers = {'Message-ID': id,
  46. 'In-Reply-To': orig_id}
  47. elif update == const.TYPE_ACTIVITY_COMMENT_QUESTION:
  48. id = "CQ-%s-%s" % (post.id, suffix_id)
  49. orig_id = "NQ-%s-%s" % (orig_post.id, suffix_id)
  50. headers = {'Message-ID': id,
  51. 'In-Reply-To': orig_id}
  52. elif update == const.TYPE_ACTIVITY_UPDATE_ANSWER:
  53. id = "UA-%s-%s-%s" % (post.id, post.last_edited_at, suffix_id)
  54. orig_id = "NQ-%s-%s" % (orig_post.id, suffix_id)
  55. headers = {'Message-ID': id,
  56. 'In-Reply-To': orig_id}
  57. elif update == const.TYPE_ACTIVITY_COMMENT_ANSWER:
  58. id = "CA-%s-%s" % (post.id, suffix_id)
  59. orig_id = "NQ-%s-%s" % (orig_post.id, suffix_id)
  60. headers = {'Message-ID': id,
  61. 'In-Reply-To': orig_id}
  62. else:
  63. # Unknown type -> Can't set headers
  64. return {}
  65. return headers
  66. def send_mail(
  67. subject_line = None,
  68. body_text = None,
  69. recipient_list = None,
  70. activity_type = None,
  71. related_object = None,
  72. headers = None,
  73. raise_on_failure = False,
  74. ):
  75. """
  76. todo: remove parameters not relevant to the function
  77. sends email message
  78. logs email sending activity
  79. and any errors are reported as critical
  80. in the main log file
  81. related_object is not mandatory, other arguments
  82. are. related_object (if given, will be saved in
  83. the activity record)
  84. if raise_on_failure is True, exceptions.EmailNotSent is raised
  85. """
  86. prefix = askbot_settings.EMAIL_SUBJECT_PREFIX.strip() + ' '
  87. try:
  88. assert(subject_line is not None)
  89. subject_line = prefix + subject_line
  90. msg = mail.EmailMessage(
  91. subject_line,
  92. body_text,
  93. django_settings.DEFAULT_FROM_EMAIL,
  94. recipient_list,
  95. headers = headers
  96. )
  97. msg.content_subtype = 'html'
  98. msg.send()
  99. if related_object is not None:
  100. assert(activity_type is not None)
  101. except Exception, error:
  102. logging.critical(unicode(error))
  103. if raise_on_failure == True:
  104. raise exceptions.EmailNotSent(unicode(error))
  105. def mail_moderators(
  106. subject_line = '',
  107. body_text = '',
  108. raise_on_failure = False):
  109. """sends email to forum moderators and admins
  110. """
  111. from django.db.models import Q
  112. from askbot.models import User
  113. recipient_list = User.objects.filter(
  114. Q(status='m') | Q(is_superuser=True)
  115. ).filter(
  116. is_active = True
  117. ).values_list('email', flat=True)
  118. recipient_list = set(recipient_list)
  119. from_email = ''
  120. if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'):
  121. from_email = django_settings.DEFAULT_FROM_EMAIL
  122. try:
  123. mail.send_mail(subject_line, body_text, from_email, recipient_list)
  124. pass
  125. except smtplib.SMTPException, error:
  126. logging.critical(unicode(error))
  127. if raise_on_failure == True:
  128. raise exceptions.EmailNotSent(unicode(error))