No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

132 líneas
4.1 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. def sendmail_html(sender, recipients, subject, html, text=None, template=None, send_now=1, reply_to=None):
  25. """
  26. Send an html mail with alternative text and using Page Templates
  27. """
  28. sendmail(recipients, sender, html, subject,
  29. send_now = send_now, reply_to = reply_to, template = template)
  30. def make_html_body(content, template = None):
  31. """
  32. Generate html content from a Page Template object
  33. """
  34. template_html = '<div class="margin: 12px">%(content)s</div>'
  35. if template:
  36. from webnotes.model.code import get_code
  37. template_html = get_code(webnotes.conn.get_value('Page Template', template, 'module'), 'Page Template', template, 'html', fieldname='template')
  38. return template_html % {'content': content}
  39. def sendmail_md(recipients, sender=None, msg=None, subject=None, from_defs=0):
  40. """send markdown email"""
  41. import markdown2
  42. sendmail(recipients, sender, markdown2.markdown(msg), subject, txt=msg, from_defs=from_defs)
  43. def sendmail(recipients, sender='', msg='', subject='[No Subject]', txt=None, \
  44. parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None, from_defs=0):
  45. """
  46. send an html email as multipart with attachments and all
  47. """
  48. from webnotes.utils.email_lib.html2text import html2text
  49. from webnotes.utils.email_lib.send import EMail
  50. import HTMLParser
  51. email = EMail(sender, recipients, subject, reply_to=reply_to, from_defs=from_defs)
  52. email.cc = cc
  53. if msg:
  54. if template:
  55. msg = make_html_body(msg, template)
  56. else:
  57. # if not html, then lets put some whitespace
  58. if (not '<br>' in msg) and (not '<p>' in msg):
  59. msg = msg.replace('\n','<br>')
  60. footer = get_footer()
  61. # encode using utf-8
  62. footer = footer.encode('utf-8', 'ignore')
  63. msg = msg + (footer or '')
  64. if txt:
  65. email.set_text(txt)
  66. else:
  67. try:
  68. msg_unicode = msg
  69. if isinstance(msg, str):
  70. msg_unicode = unicode(msg, 'utf-8', 'ignore')
  71. email.set_text(html2text(msg_unicode))
  72. except HTMLParser.HTMLParseError:
  73. pass
  74. email.set_html(msg)
  75. for p in parts:
  76. email.set_message(p[1])
  77. for a in attach:
  78. email.attach(a)
  79. email.send(send_now)
  80. def get_footer():
  81. """
  82. Returns combination of footer from globals and Control Panel
  83. """
  84. footer = webnotes.conn.get_value('Control Panel',None,'mail_footer') or ''
  85. footer += (webnotes.conn.get_global('global_mail_footer') or '')
  86. return footer
  87. @webnotes.whitelist()
  88. def send_form():
  89. """
  90. Emails a print format (form)
  91. Called from form UI
  92. """
  93. from webnotes.utils.email_lib.form_email import FormEmail
  94. FormEmail().send()
  95. @webnotes.whitelist()
  96. def get_contact_list():
  97. """
  98. Returns contacts (from autosuggest)
  99. """
  100. cond = ['`%s` like "%s%%"' % (f,
  101. webnotes.form.getvalue('txt')) for f in webnotes.form.getvalue('where').split(',')]
  102. cl = webnotes.conn.sql("select `%s` from `tab%s` where %s" % (
  103. webnotes.form.getvalue('select')
  104. ,webnotes.form.getvalue('from')
  105. ,' OR '.join(cond)
  106. )
  107. )
  108. webnotes.response['cl'] = filter(None, [c[0] for c in cl])