Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
13 лет назад
13 лет назад
13 лет назад
12 лет назад
13 лет назад
12 лет назад
12 лет назад
11 лет назад
12 лет назад
12 лет назад
12 лет назад
11 лет назад
13 лет назад
12 лет назад
11 лет назад
13 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
13 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. from webnotes.model.doc import Document
  6. from webnotes.utils import cint
  7. class BulkLimitCrossedError(webnotes.ValidationError): pass
  8. def send(recipients=None, sender=None, doctype='Profile', email_field='email',
  9. subject='[No Subject]', message='[No Content]', ref_doctype=None, ref_docname=None):
  10. """send bulk mail if not unsubscribed and within conf.bulk_mail_limit"""
  11. import webnotes
  12. def is_unsubscribed(rdata):
  13. if not rdata: return 1
  14. return cint(rdata.unsubscribed)
  15. def check_bulk_limit(new_mails):
  16. from webnotes import conf
  17. from webnotes.utils import nowdate
  18. this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where
  19. month(creation)=month(%s)""" % nowdate())[0][0]
  20. monthly_bulk_mail_limit = conf.get('monthly_bulk_mail_limit') or 500
  21. if this_month + len(recipients) > monthly_bulk_mail_limit:
  22. webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit,
  23. raise_exception=BulkLimitCrossedError)
  24. def update_message(doc):
  25. from webnotes.utils import get_url
  26. import urllib
  27. updated = message + """<div style="padding: 7px; border-top: 1px solid #aaa;
  28. margin-top: 17px;">
  29. <small><a href="%s/?%s">
  30. Unsubscribe</a> from this list.</small></div>""" % (get_url(),
  31. urllib.urlencode({
  32. "cmd": "webnotes.utils.email_lib.bulk.unsubscribe",
  33. "email": doc.get(email_field),
  34. "type": doctype,
  35. "email_field": email_field
  36. }))
  37. return updated
  38. if not recipients: recipients = []
  39. if not sender or sender == "Administrator":
  40. sender = webnotes.conn.get_value('Email Settings', None, 'auto_email_id')
  41. check_bulk_limit(len(recipients))
  42. import HTMLParser
  43. from webnotes.utils.email_lib.html2text import html2text
  44. try:
  45. text_content = html2text(message)
  46. except HTMLParser.HTMLParseError:
  47. text_content = "[See html attachment]"
  48. for r in filter(None, list(set(recipients))):
  49. rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype,
  50. email_field, '%s'), r, as_dict=1)
  51. doc = rdata and rdata[0] or {}
  52. if not is_unsubscribed(doc):
  53. # add to queue
  54. add(r, sender, subject, update_message(doc), text_content, ref_doctype, ref_docname)
  55. def add(email, sender, subject, message, text_content=None, ref_doctype=None, ref_docname=None):
  56. """add to bulk mail queue"""
  57. from webnotes.utils.email_lib.smtp import get_email
  58. e = Document('Bulk Email')
  59. e.sender = sender
  60. e.recipient = email
  61. try:
  62. e.message = get_email(email, sender=e.sender, msg=message, subject=subject,
  63. text_content = text_content).as_string()
  64. except webnotes.ValidationError:
  65. # bad email id - don't add to queue
  66. return
  67. e.status = 'Not Sent'
  68. e.ref_doctype = ref_doctype
  69. e.ref_docname = ref_docname
  70. e.save()
  71. @webnotes.whitelist(allow_guest=True)
  72. def unsubscribe():
  73. doctype = webnotes.form_dict.get('type')
  74. field = webnotes.form_dict.get('email_field')
  75. email = webnotes.form_dict.get('email')
  76. webnotes.conn.sql("""update `tab%s` set unsubscribed=1
  77. where `%s`=%s""" % (doctype, field, '%s'), email)
  78. if not webnotes.form_dict.get("from_test"):
  79. webnotes.conn.commit()
  80. webnotes.local.message_title = "Unsubscribe"
  81. webnotes.local.message = "<h3>Unsubscribed</h3><p>%s has been successfully unsubscribed.</p>" % email
  82. webnotes.response['type'] = 'page'
  83. webnotes.response['page_name'] = 'message.html'
  84. def flush(from_test=False):
  85. """flush email queue, every time: called from scheduler"""
  86. import webnotes
  87. from webnotes import conf
  88. from webnotes.utils.email_lib.smtp import SMTPServer, get_email
  89. smptserver = SMTPServer()
  90. auto_commit = not from_test
  91. if webnotes.flags.mute_emails or conf.get("mute_emails") or False:
  92. webnotes.msgprint("Emails are muted")
  93. from_test = True
  94. for i in xrange(500):
  95. email = webnotes.conn.sql("""select * from `tabBulk Email` where
  96. status='Not Sent' limit 1 for update""", as_dict=1)
  97. if email:
  98. email = email[0]
  99. else:
  100. break
  101. webnotes.conn.sql("""update `tabBulk Email` set status='Sending' where name=%s""",
  102. email["name"], auto_commit=auto_commit)
  103. try:
  104. if not from_test:
  105. smptserver.sess.sendmail(email["sender"], email["recipient"], email["message"])
  106. webnotes.conn.sql("""update `tabBulk Email` set status='Sent' where name=%s""",
  107. email["name"], auto_commit=auto_commit)
  108. except Exception, e:
  109. webnotes.conn.sql("""update `tabBulk Email` set status='Error', error=%s
  110. where name=%s""", (unicode(e), email["name"]), auto_commit=auto_commit)
  111. def clear_outbox():
  112. """remove mails older than 30 days in Outbox"""
  113. webnotes.conn.sql("""delete from `tabBulk Email` where
  114. datediff(now(), creation) > 30""")