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.
 
 
 
 
 
 

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