您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

128 行
5.0 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. class BulkLimitCrossedError(webnotes.ValidationError): pass
  25. def send(recipients=[], doctype='Profile', email_field='email', first_name_field="first_name",
  26. last_name_field="last_name", subject='[No Subject]', message='[No Content]'):
  27. """send bulk mail if not unsubscribed and within conf.bulk_mail_limit"""
  28. import webnotes
  29. def is_unsubscribed(rdata):
  30. if not rdata: return 1
  31. return rdata[0]['unsubscribed']
  32. def check_bulk_limit(new_mails):
  33. import conf, startup
  34. from webnotes.utils import nowdate
  35. this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where
  36. month(creation)=month(%s)""" % nowdate())[0][0]
  37. if hasattr(startup, 'get_monthly_bulk_mail_limit'):
  38. monthly_bulk_mail_limit = startup.get_monthly_bulk_mail_limit()
  39. else:
  40. monthly_bulk_mail_limit = getattr(conf, 'monthly_bulk_mail_limit', 500)
  41. if this_month + len(recipients) > monthly_bulk_mail_limit:
  42. webnotes.msgprint("""Monthly Bulk Mail Limit (%s) Crossed""" % monthly_bulk_mail_limit,
  43. raise_exception=BulkLimitCrossedError)
  44. def add_unsubscribe_link(email):
  45. from webnotes.utils import get_request_site_address
  46. return message + """<div style="padding: 7px; border-top: 1px solid #aaa;
  47. margin-top: 17px;">
  48. <small><a href="http://%s/server.py?cmd=%s&email=%s&type=%s&email_field=%s">
  49. Unsubscribe</a> from this list.</small></div>""" % (get_request_site_address(),
  50. 'webnotes.utils.email_lib.bulk.unsubscribe', email, doctype, email_field)
  51. def full_name(rdata):
  52. fname = rdata[0].get(first_name_field, '')
  53. lname = rdata[0].get(last_name_field, '')
  54. if fname and not lname:
  55. return fname
  56. elif lname and not fname:
  57. return lname
  58. elif fname and lname:
  59. return fname + ' ' + lname
  60. else:
  61. return rdata[0][email_field].split('@')[0].title()
  62. check_bulk_limit(len(recipients))
  63. sender = webnotes.conn.get_value('Email Settings', None, 'auto_mail_id')
  64. for r in recipients:
  65. rdata = webnotes.conn.sql("""select * from `tab%s` where %s=%s""" % (doctype,
  66. email_field, '%s'), r, as_dict=1)
  67. if not is_unsubscribed(rdata):
  68. # add to queue
  69. add(r, sender, subject, add_unsubscribe_link(r))
  70. def add(email, sender, subject, message):
  71. """add to bulk mail queue"""
  72. from webnotes.model.doc import Document
  73. from webnotes.utils.email_lib.smtp import get_email
  74. e = Document('Bulk Email')
  75. e.sender = sender
  76. e.recipient = email
  77. e.message = get_email(email, sender=e.sender, msg=message, subject=subject).as_string()
  78. e.status = 'Not Sent'
  79. e.save()
  80. @webnotes.whitelist(allow_guest=True)
  81. def unsubscribe():
  82. doctype = webnotes.form_dict.get('type')
  83. field = webnotes.form_dict.get('email_field')
  84. email = webnotes.form_dict.get('email')
  85. webnotes.conn.sql("""update `tab%s` set unsubscribed=1
  86. where email_id=%s""" % (doctype, '%s'), email)
  87. webnotes.unsubscribed_email = email
  88. webnotes.response['type'] = 'page'
  89. webnotes.response['page_name'] = 'unsubscribed.html'
  90. def flush():
  91. """flush email queue, every time: called from scheduler"""
  92. import webnotes
  93. from webnotes.utils.email_lib.smtp import SMTPServer
  94. smptserver = SMTPServer()
  95. for email in webnotes.conn.sql("""select * from `tabBulk Email` where status='Not Sent'""",
  96. as_dict=1):
  97. webnotes.conn.sql("""update `tabBulk Email` set status='Sending' where name=%s""",
  98. email["name"], auto_commit=True)
  99. try:
  100. smptserver.sess.sendmail(email["sender"], email["recipient"], email["message"])
  101. webnotes.conn.sql("""update `tabBulk Email` set status='Sent' where name=%s""",
  102. email["name"], auto_commit=True)
  103. except Exception, e:
  104. webnotes.conn.sql("""update `tabBulk Email` set status='Error', error=%s
  105. where name=%s""", (str(e), email["name"]), auto_commit=True)
  106. def clear_outbox():
  107. """remove mails older than 30 days in Outbox"""
  108. webnotes.conn.sql("""delete from `tabBulk Email` where
  109. datediff(now(), creation) > 30""")