Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

150 Zeilen
4.8 KiB

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