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.
 
 
 
 
 
 

162 lines
4.0 KiB

  1. import webnotes
  2. from webnotes.utils import cint
  3. form = webnotes.form
  4. from webnotes.utils.email_lib import get_footer
  5. from webnotes.utils.email_lib.send import EMail
  6. class FormEmail:
  7. """
  8. Represents an email sent from a Form
  9. """
  10. def __init__(self):
  11. """
  12. Get paramteres from the cgi form object
  13. """
  14. self.__dict__.update(webnotes.form_dict)
  15. self.recipients = None
  16. if self.sendto:
  17. self.recipients = self.sendto.replace(';', ',')
  18. self.recipients = self.recipients.split(',')
  19. def update_contacts(self):
  20. """
  21. Add new email contact to database
  22. """
  23. import webnotes
  24. from webnotes.model.doc import Document
  25. for r in self.recipients:
  26. r = r.strip()
  27. try:
  28. if not webnotes.conn.sql("select email_id from tabContact where email_id=%s", r):
  29. d = Document('Contact')
  30. d.email_id = r
  31. d.save(1)
  32. except Exception, e:
  33. if e.args[0]==1146: pass # no table
  34. else: raise e
  35. def make_full_links(self):
  36. """
  37. Adds server name the relative links, so that images etc can be seen correctly
  38. """
  39. # only domain
  40. if not self.__dict__.get('full_domain'):
  41. return
  42. def make_full_link(match):
  43. import os
  44. link = match.group('name')
  45. if not link.startswith('http'):
  46. link = os.path.join(self.full_domain, link)
  47. return 'src="%s"' % link
  48. import re
  49. p = re.compile('src[ ]*=[ ]*" (?P<name> [^"]*) "', re.VERBOSE)
  50. self.body = p.sub(make_full_link, self.body)
  51. p = re.compile("src[ ]*=[ ]*' (?P<name> [^']*) '", re.VERBOSE)
  52. self.body = p.sub(make_full_link, self.body)
  53. def get_form_link(self):
  54. """
  55. Returns publicly accessible form link
  56. """
  57. public_domain = webnotes.conn.get_value('Control Panel', None, 'public_domain')
  58. from webnotes.utils.encrypt import encrypt
  59. if not public_domain:
  60. return ''
  61. args = {
  62. 'dt': self.dt,
  63. 'dn':self.dn,
  64. 'acx': webnotes.conn.get_value('Control Panel', None, 'account_id'),
  65. 'server': public_domain,
  66. 'akey': encrypt(self.dn)
  67. }
  68. return '<div>If you are unable to view the form below <a href="http://%(server)s/index.cgi?page=Form/%(dt)s/%(dn)s&acx=%(acx)s&akey=%(akey)s">click here to see it in your browser</div>' % args
  69. def set_attachments(self):
  70. """
  71. Set attachments to the email from the form
  72. """
  73. al = []
  74. try:
  75. al = webnotes.conn.sql('select file_list from `tab%s` where name="%s"' % (form.getvalue('dt'), form.getvalue('dn')))
  76. if al:
  77. al = (al[0][0] or '').split('\n')
  78. except Exception, e:
  79. if e.args[0]==1146:
  80. pass # no attachments in single types!
  81. else:
  82. raise Exception, e
  83. return al
  84. def build_message(self):
  85. """
  86. Builds the message object
  87. """
  88. self.email = EMail(self.sendfrom, self.recipients, self.subject, alternative = 1)
  89. from webnotes.utils.email_lib.html2text import html2text
  90. self.make_full_links()
  91. # message
  92. if not self.__dict__.get('message'):
  93. self.message = 'Please find attached %s: %s\n' % (self.dt, self.dn)
  94. html_message = text_message = self.message.replace('\n','<br>')
  95. # separator
  96. html_message += '<div style="margin:17px 0px; border-bottom:1px solid #AAA"></div>'
  97. # form itself (only in the html message)
  98. html_message += self.body
  99. # form link
  100. html_message += self.get_form_link()
  101. text_message += self.get_form_link()
  102. # footer
  103. footer = get_footer()
  104. if footer:
  105. html_message += footer
  106. text_message += footer
  107. # message as text
  108. self.email.set_text(html2text(text_message))
  109. self.email.set_html(html_message)
  110. def send(self):
  111. """
  112. Send the form with html attachment
  113. """
  114. if not self.recipients:
  115. webnotes.msgprint('No one to send to!')
  116. return
  117. self.build_message()
  118. # print format (as attachment also - for text-only clients)
  119. self.email.add_attachment(self.dn.replace(' ','').replace('/','-') + '.html', self.body)
  120. # attachments
  121. # self.with_attachments comes from http form variables
  122. # i.e. with_attachments=1
  123. if cint(self.with_attachments):
  124. for a in self.set_attachments():
  125. a and self.email.attach_file(a.split(',')[0])
  126. # cc
  127. if self.cc:
  128. self.email.cc = [self.cc]
  129. self.email.send(send_now=1)
  130. webnotes.msgprint('Sent')