You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

102 lines
2.7 KiB

  1. import webnotes
  2. def sendmail_html(sender, recipients, subject, html, text=None, template=None, send_now=1, reply_to=None):
  3. """
  4. Send an html mail with alternative text and using Page Templates
  5. """
  6. sendmail(recipients, sender, html, subject,
  7. send_now = send_now, reply_to = reply_to, template = template)
  8. def make_html_body(content, template = None):
  9. """
  10. Generate html content from a Page Template object
  11. """
  12. template_html = '<div class="margin: 12px">%(content)s</div>'
  13. if template:
  14. from webnotes.model.code import get_code
  15. template_html = get_code(webnotes.conn.get_value('Page Template', template, 'module'), 'Page Template', template, 'html', fieldname='template')
  16. return template_html % {'content': content}
  17. def sendmail_md(recipients, sender=None, msg=None, subject=None):
  18. """send markdown email"""
  19. import markdown2
  20. sendmail(recipients, sender, markdown2.markdown(msg), subject, txt=msg)
  21. def sendmail(recipients, sender='', msg='', subject='[No Subject]', txt=None, \
  22. parts=[], cc=[], attach=[], send_now=1, reply_to=None, template=None, from_defs=0):
  23. """
  24. send an html email as multipart with attachments and all
  25. """
  26. from webnotes.utils.email_lib.html2text import html2text
  27. from webnotes.utils.email_lib.send import EMail
  28. import HTMLParser
  29. email = EMail(sender, recipients, subject, reply_to=reply_to, from_defs=from_defs)
  30. email.cc = cc
  31. if msg:
  32. if template:
  33. msg = make_html_body(msg, template).encode('utf-8')
  34. else:
  35. # if not html, then lets put some whitespace
  36. if (not '<br>' in msg) and (not '<p>' in msg):
  37. msg = msg.replace('\n','<br>')
  38. footer = get_footer()
  39. msg = msg + (footer or '')
  40. if txt:
  41. email.set_text(txt)
  42. else:
  43. try:
  44. email.set_text(html2text(msg))
  45. except HTMLParser.HTMLParseError:
  46. pass
  47. email.set_html(msg)
  48. for p in parts:
  49. email.set_message(p[1])
  50. for a in attach:
  51. email.attach(a)
  52. email.send(send_now)
  53. def get_footer():
  54. """
  55. Returns combination of footer from globals and Control Panel
  56. """
  57. footer = webnotes.conn.get_value('Control Panel',None,'mail_footer') or ''
  58. footer += (webnotes.conn.get_global('global_mail_footer') or '')
  59. return footer
  60. def send_form():
  61. """
  62. Emails a print format (form)
  63. Called from form UI
  64. """
  65. from webnotes.utils.email_lib.form_email import FormEmail
  66. FormEmail().send()
  67. def get_contact_list():
  68. """
  69. Returns contacts (from autosuggest)
  70. """
  71. import webnotes
  72. cond = ['`%s` like "%s%%"' % (f, webnotes.form.getvalue('txt')) for f in webnotes.form.getvalue('where').split(',')]
  73. cl = webnotes.conn.sql("select `%s` from `tab%s` where %s" % (
  74. webnotes.form.getvalue('select')
  75. ,webnotes.form.getvalue('from')
  76. ,' OR '.join(cond)
  77. )
  78. )
  79. webnotes.response['cl'] = filter(None, [c[0] for c in cl])