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.
 
 
 
 
 
 

211 lines
5.9 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. import webnotes
  23. from webnotes.utils import cint
  24. form = webnotes.form
  25. from webnotes.utils.email_lib import get_footer
  26. from webnotes.utils.email_lib.send import EMail
  27. class FormEmail:
  28. """
  29. Represents an email sent from a Form
  30. """
  31. def __init__(self):
  32. """
  33. Get paramteres from the cgi form object
  34. """
  35. self.__dict__.update(webnotes.form_dict)
  36. self.recipients = None
  37. if self.sendto:
  38. self.recipients = self.sendto.replace(';', ',')
  39. self.recipients = self.recipients.split(',')
  40. def update_contacts(self):
  41. """
  42. Add new email contact to database
  43. """
  44. import webnotes
  45. from webnotes.model.doc import Document
  46. for r in self.recipients:
  47. r = r.strip()
  48. try:
  49. if not webnotes.conn.sql("select email_id from tabContact where email_id=%s", r):
  50. d = Document('Contact')
  51. d.email_id = r
  52. d.save(1)
  53. except Exception, e:
  54. if e.args[0]==1146: pass # no table
  55. else: raise e
  56. def make_full_links(self):
  57. """
  58. Adds server name the relative links, so that images etc can be seen correctly
  59. """
  60. # only domain
  61. if not self.__dict__.get('full_domain'):
  62. return
  63. def make_full_link(match):
  64. import os
  65. link = match.group('name')
  66. if not link.startswith('http'):
  67. link = os.path.join(self.full_domain, link)
  68. return 'src="%s"' % link
  69. import re
  70. p = re.compile('src[ ]*=[ ]*" (?P<name> [^"]*) "', re.VERBOSE)
  71. self.body = p.sub(make_full_link, self.body)
  72. p = re.compile("src[ ]*=[ ]*' (?P<name> [^']*) '", re.VERBOSE)
  73. self.body = p.sub(make_full_link, self.body)
  74. def get_form_link(self):
  75. """
  76. Returns publicly accessible form link
  77. """
  78. public_domain = webnotes.conn.get_value('Control Panel', None, 'public_domain')
  79. from webnotes.utils.encrypt import encrypt
  80. if not public_domain:
  81. return ''
  82. args = {
  83. 'dt': self.dt,
  84. 'dn':self.dn,
  85. 'acx': webnotes.conn.get_value('Control Panel', None, 'account_id'),
  86. 'server': public_domain,
  87. 'akey': encrypt(self.dn)
  88. }
  89. 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
  90. def set_attachments(self):
  91. """
  92. Set attachments to the email from the form
  93. """
  94. al = []
  95. try:
  96. al = webnotes.conn.sql('select file_list from `tab%s` where name="%s"' % (form.getvalue('dt'), form.getvalue('dn')))
  97. if al:
  98. al = (al[0][0] or '').split('\n')
  99. except Exception, e:
  100. if e.args[0]==1146:
  101. pass # no attachments in single types!
  102. else:
  103. raise Exception, e
  104. return al
  105. def build_message(self):
  106. """
  107. Builds the message object
  108. """
  109. self.email = EMail(self.sendfrom, self.recipients, self.subject, alternative = 1)
  110. from webnotes.utils.email_lib.html2text import html2text
  111. self.make_full_links()
  112. # message
  113. if not self.__dict__.get('message'):
  114. self.message = 'Please find attached %s: %s\n' % (self.dt, self.dn)
  115. html_message = text_message = self.message.replace('\n','<br>')
  116. # separator
  117. html_message += '<div style="margin:17px 0px; border-bottom:1px solid #AAA"></div>'
  118. # form itself (only in the html message)
  119. html_message += self.body
  120. # footer
  121. footer = get_footer()
  122. if footer:
  123. footer = footer.encode('utf-8')
  124. html_message += footer
  125. text_message += footer
  126. # message as text
  127. self.email.set_text(html2text(unicode(text_message, 'utf-8')))
  128. self.email.set_html(html_message)
  129. def make_communication(self):
  130. """make email communication"""
  131. from webnotes.model.doc import Document
  132. comm = Document('Communication')
  133. comm.communication_medium = 'Email'
  134. comm.subject = self.subject
  135. comm.content = self.message
  136. comm.category = 'Sent Mail'
  137. comm.action = 'Sent Mail'
  138. comm.naming_series = 'COMM-'
  139. try:
  140. comm_cols = [c[0] for c in webnotes.conn.sql("""desc tabCommunication""")]
  141. # tag to record
  142. if self.dt in comm_cols:
  143. comm.fields[self.dt] = self.dn
  144. # tag to customer, supplier (?)
  145. if self.customer:
  146. comm.customer = self.customer
  147. if self.supplier:
  148. comm.supplier = self.supplier
  149. comm.save(1)
  150. except Exception, e:
  151. if e.args[0]!=1146: raise e
  152. def send(self):
  153. """
  154. Send the form with html attachment
  155. """
  156. if not self.recipients:
  157. webnotes.msgprint('No one to send to!')
  158. return
  159. self.build_message()
  160. # print format (as attachment also - for text-only clients)
  161. self.email.add_attachment(self.dn.replace(' ','').replace('/','-') + '.html', self.body)
  162. # attachments
  163. # self.with_attachments comes from http form variables
  164. # i.e. with_attachments=1
  165. if cint(self.with_attachments):
  166. for a in self.set_attachments():
  167. a and self.email.attach_file(a.split(',')[1])
  168. # cc
  169. if self.cc:
  170. self.email.cc = [self.cc]
  171. self.email.send(send_now=1)
  172. self.make_communication()
  173. webnotes.msgprint('Sent')