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.
 
 
 
 
 
 

145 Zeilen
5.2 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. from webnotes.model.doc import Document
  25. class BulkLimitCrossedError(webnotes.ValidationError): pass
  26. def send(recipients=None, sender=None, doctype='Profile', email_field='email',
  27. subject='[No Subject]', message='[No Content]'):
  28. """send bulk mail if not unsubscribed and within conf.bulk_mail_limit"""
  29. import webnotes
  30. if webnotes.mute_emails:
  31. return
  32. def is_unsubscribed(rdata):
  33. if not rdata: return 1
  34. return rdata[0]['unsubscribed']
  35. def check_bulk_limit(new_mails):
  36. import conf, startup
  37. from webnotes.utils import nowdate
  38. this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where
  39. month(creation)=month(%s)""" % nowdate())[0][0]
  40. if hasattr(startup, 'get_monthly_bulk_mail_limit'):
  41. monthly_bulk_mail_limit = startup.get_monthly_bulk_mail_limit()
  42. else:
  43. monthly_bulk_mail_limit = getattr(conf, 'monthly_bulk_mail_limit', 500)
  44. if this_month + len(recipients) > monthly_bulk_mail_limit:
  45. webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit,
  46. raise_exception=BulkLimitCrossedError)
  47. def add_unsubscribe_link(email):
  48. from webnotes.utils import get_request_site_address
  49. import urllib
  50. return message + """<div style="padding: 7px; border-top: 1px solid #aaa;
  51. margin-top: 17px;">
  52. <small><a href="%s/server.py?%s">
  53. Unsubscribe</a> from this list.</small></div>""" % (get_request_site_address(),
  54. urllib.urlencode({
  55. "cmd": "webnotes.utils.email_lib.bulk.unsubscribe",
  56. "email": email,
  57. "type": doctype,
  58. "email_field": email_field
  59. }))
  60. if not recipients: recipients = []
  61. if not sender or sender == "Administrator":
  62. sender = webnotes.conn.get_value('Email Settings', None, 'auto_mail_id')
  63. check_bulk_limit(len(recipients))
  64. import HTMLParser
  65. from webnotes.utils.email_lib.html2text import html2text
  66. try:
  67. text_content = html2text(message)
  68. except HTMLParser.HTMLParseError:
  69. text_content = "[See html attachment]"
  70. for r in list(set(recipients)):
  71. rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype,
  72. email_field, '%s'), r, as_dict=1)
  73. if not is_unsubscribed(rdata):
  74. # add to queue
  75. add(r, sender, subject, add_unsubscribe_link(r), text_content)
  76. def add(email, sender, subject, message, text_content = None):
  77. """add to bulk mail queue"""
  78. from webnotes.utils.email_lib.smtp import get_email
  79. e = Document('Bulk Email')
  80. e.sender = sender
  81. e.recipient = email
  82. e.message = get_email(email, sender=e.sender, msg=message, subject=subject,
  83. text_content = text_content).as_string()
  84. e.status = 'Not Sent'
  85. e.save()
  86. @webnotes.whitelist(allow_guest=True)
  87. def unsubscribe():
  88. doctype = webnotes.form_dict.get('type')
  89. field = webnotes.form_dict.get('email_field')
  90. email = webnotes.form_dict.get('email')
  91. webnotes.conn.sql("""update `tab%s` set unsubscribed=1
  92. where `%s`=%s""" % (doctype, field, '%s'), email)
  93. webnotes.unsubscribed_email = email
  94. webnotes.response['type'] = 'page'
  95. webnotes.response['page_name'] = 'unsubscribed.html'
  96. def flush():
  97. """flush email queue, every time: called from scheduler"""
  98. import webnotes
  99. from webnotes.utils.email_lib.smtp import SMTPServer, get_email
  100. smptserver = SMTPServer()
  101. for i in xrange(500):
  102. email = webnotes.conn.sql("""select * from `tabBulk Email` where
  103. status='Not Sent' limit 1 for update""", as_dict=1)
  104. if email:
  105. email = email[0]
  106. else:
  107. break
  108. webnotes.conn.sql("""update `tabBulk Email` set status='Sending' where name=%s""",
  109. email["name"], auto_commit=True)
  110. try:
  111. smptserver.sess.sendmail(email["sender"], email["recipient"], email["message"])
  112. webnotes.conn.sql("""update `tabBulk Email` set status='Sent' where name=%s""",
  113. email["name"], auto_commit=True)
  114. except Exception, e:
  115. webnotes.conn.sql("""update `tabBulk Email` set status='Error', error=%s
  116. where name=%s""", (unicode(e), email["name"]), auto_commit=True)
  117. def clear_outbox():
  118. """remove mails older than 30 days in Outbox"""
  119. webnotes.conn.sql("""delete from `tabBulk Email` where
  120. datediff(now(), creation) > 30""")