您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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