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.

receive.py 5.8 KiB

12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 extract_email_id, convert_utc_to_user_timezone
  25. class IncomingMail:
  26. """
  27. Single incoming email object. Extracts, text / html and attachments from the email
  28. """
  29. def __init__(self, content):
  30. import email, email.utils
  31. import datetime
  32. self.mail = email.message_from_string(content)
  33. self.text_content = ''
  34. self.html_content = ''
  35. self.attachments = []
  36. self.parse()
  37. self.set_content_and_type()
  38. self.set_subject()
  39. self.from_email = extract_email_id(self.mail["From"])
  40. self.from_real_name = email.utils.parseaddr(self.mail["From"])[0]
  41. utc = email.utils.mktime_tz(email.utils.parsedate_tz(self.mail["Date"]))
  42. utc_dt = datetime.datetime.utcfromtimestamp(utc)
  43. self.date = convert_utc_to_user_timezone(utc_dt).strftime('%Y-%m-%d %H:%M:%S')
  44. def parse(self):
  45. for part in self.mail.walk():
  46. self.process_part(part)
  47. def set_subject(self):
  48. import email.header
  49. _subject = email.header.decode_header(self.mail.get("Subject", "No Subject"))
  50. self.subject = _subject[0][0]
  51. if _subject[0][1]:
  52. self.subject = _subject[0][0].decode(_subject[0][1])
  53. def set_content_and_type(self):
  54. self.content, self.content_type = '[Blank Email]', 'text/plain'
  55. if self.text_content:
  56. self.content, self.content_type = self.text_content, 'text/plain'
  57. else:
  58. self.content, self.content_type = self.html_content, 'text/html'
  59. def process_part(self, part):
  60. content_type = part.get_content_type()
  61. charset = part.get_content_charset()
  62. if not charset: charset = self.get_charset(part)
  63. if content_type == 'text/plain':
  64. self.text_content += self.get_payload(part, charset)
  65. if content_type == 'text/html':
  66. self.html_content += self.get_payload(part, charset)
  67. if part.get_filename():
  68. self.get_attachment(part, charset)
  69. def get_text_content(self):
  70. return self.text_content or self.html_content
  71. def get_charset(self, part):
  72. charset = part.get_content_charset()
  73. if not charset:
  74. import chardet
  75. charset = chardet.detect(str(part))['encoding']
  76. return charset
  77. def get_payload(self, part, charset):
  78. try:
  79. return unicode(part.get_payload(decode=True),str(charset),"ignore")
  80. except LookupError, e:
  81. return part.get_payload()
  82. def get_attachment(self, part, charset):
  83. self.attachments.append({
  84. 'content-type': part.get_content_type(),
  85. 'filename': part.get_filename(),
  86. 'content': part.get_payload(decode=True),
  87. })
  88. def save_attachments_in_doc(self, doc):
  89. from webnotes.utils.file_manager import save_file, MaxFileSizeReachedError
  90. for attachment in self.attachments:
  91. try:
  92. fid = save_file(attachment['filename'], attachment['content'],
  93. doc.doctype, doc.name)
  94. except MaxFileSizeReachedError:
  95. # bypass max file size exception
  96. pass
  97. except webnotes.DuplicateEntryError:
  98. # same file attached twice??
  99. pass
  100. def get_thread_id(self):
  101. import re
  102. l = re.findall('(?<=\[)[\w/-]+', self.subject)
  103. return l and l[0] or None
  104. class POP3Mailbox:
  105. def __init__(self, args=None):
  106. self.setup(args)
  107. self.get_messages()
  108. def setup(self, args=None):
  109. # overrride
  110. import webnotes
  111. self.settings = args or webnotes._dict()
  112. def check_mails(self):
  113. # overrride
  114. return True
  115. def process_message(self, mail):
  116. # overrride
  117. pass
  118. def connect(self):
  119. import poplib
  120. if self.settings.use_ssl:
  121. self.pop = poplib.POP3_SSL(self.settings.host)
  122. else:
  123. self.pop = poplib.POP3(self.settings.host)
  124. self.pop.user(self.settings.username)
  125. self.pop.pass_(self.settings.password)
  126. def get_messages(self):
  127. import webnotes
  128. if not self.check_mails():
  129. return # nothing to do
  130. webnotes.conn.commit()
  131. self.connect()
  132. num = num_copy = len(self.pop.list()[1])
  133. # track if errors arised
  134. errors = False
  135. # WARNING: Hard coded max no. of messages to be popped
  136. if num > 20: num = 20
  137. for m in xrange(1, num+1):
  138. msg = self.pop.retr(m)
  139. # added back dele, as most pop3 servers seem to require msg to be deleted
  140. # else it will again be fetched in self.pop.list()
  141. self.pop.dele(m)
  142. try:
  143. incoming_mail = IncomingMail(b'\n'.join(msg[1]))
  144. webnotes.conn.begin()
  145. self.process_message(incoming_mail)
  146. webnotes.conn.commit()
  147. except:
  148. from webnotes.utils.scheduler import log
  149. # log performs rollback and logs error in scheduler log
  150. log("receive.get_messages")
  151. errors = True
  152. webnotes.conn.rollback()
  153. # WARNING: Mark as read - message number 101 onwards from the pop list
  154. # This is to avoid having too many messages entering the system
  155. num = num_copy
  156. if num > 100 and not errors:
  157. for m in xrange(101, num+1):
  158. self.pop.dele(m)
  159. self.pop.quit()
  160. webnotes.conn.begin()