No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

535 líneas
18 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. from six.moves import range
  5. import frappe
  6. from six.moves import html_parser as HTMLParser
  7. import smtplib, quopri, json
  8. from frappe import msgprint, throw, _
  9. from frappe.email.smtp import SMTPServer, get_outgoing_email_account
  10. from frappe.email.email_body import get_email, get_formatted_html, add_attachment
  11. from frappe.utils.verified_command import get_signed_params, verify_request
  12. from html2text import html2text
  13. from frappe.utils import get_url, nowdate, encode, now_datetime, add_days, split_emails, cstr, cint
  14. from frappe.utils.file_manager import get_file
  15. from rq.timeouts import JobTimeoutException
  16. from frappe.utils.scheduler import log
  17. from six import text_type
  18. class EmailLimitCrossedError(frappe.ValidationError): pass
  19. def send(recipients=None, sender=None, subject=None, message=None, text_content=None, reference_doctype=None,
  20. reference_name=None, unsubscribe_method=None, unsubscribe_params=None, unsubscribe_message=None,
  21. attachments=None, reply_to=None, cc=[], message_id=None, in_reply_to=None, send_after=None,
  22. expose_recipients=None, send_priority=1, communication=None, now=False, read_receipt=None,
  23. queue_separately=False, is_notification=False, add_unsubscribe_link=1, inline_images=None,
  24. header=None):
  25. """Add email to sending queue (Email Queue)
  26. :param recipients: List of recipients.
  27. :param sender: Email sender.
  28. :param subject: Email subject.
  29. :param message: Email message.
  30. :param text_content: Text version of email message.
  31. :param reference_doctype: Reference DocType of caller document.
  32. :param reference_name: Reference name of caller document.
  33. :param send_priority: Priority for Email Queue, default 1.
  34. :param unsubscribe_method: URL method for unsubscribe. Default is `/api/method/frappe.email.queue.unsubscribe`.
  35. :param unsubscribe_params: additional params for unsubscribed links. default are name, doctype, email
  36. :param attachments: Attachments to be sent.
  37. :param reply_to: Reply to be captured here (default inbox)
  38. :param in_reply_to: Used to send the Message-Id of a received email back as In-Reply-To.
  39. :param send_after: Send this email after the given datetime. If value is in integer, then `send_after` will be the automatically set to no of days from current date.
  40. :param communication: Communication link to be set in Email Queue record
  41. :param now: Send immediately (don't send in the background)
  42. :param queue_separately: Queue each email separately
  43. :param is_notification: Marks email as notification so will not trigger notifications from system
  44. :param add_unsubscribe_link: Send unsubscribe link in the footer of the Email, default 1.
  45. :param inline_images: List of inline images as {"filename", "filecontent"}. All src properties will be replaced with random Content-Id
  46. :param header: Append header in email (boolean)
  47. """
  48. if not unsubscribe_method:
  49. unsubscribe_method = "/api/method/frappe.email.queue.unsubscribe"
  50. if not recipients and not cc:
  51. return
  52. if isinstance(recipients, basestring):
  53. recipients = split_emails(recipients)
  54. if isinstance(cc, basestring):
  55. cc = split_emails(cc)
  56. if isinstance(send_after, int):
  57. send_after = add_days(nowdate(), send_after)
  58. email_account = get_outgoing_email_account(True, append_to=reference_doctype)
  59. if not sender or sender == "Administrator":
  60. sender = email_account.default_sender
  61. check_email_limit(recipients)
  62. if not text_content:
  63. try:
  64. text_content = html2text(message)
  65. except HTMLParser.HTMLParseError:
  66. text_content = "See html attachment"
  67. if reference_doctype and reference_name:
  68. unsubscribed = [d.email for d in frappe.db.get_all("Email Unsubscribe", "email",
  69. {"reference_doctype": reference_doctype, "reference_name": reference_name})]
  70. unsubscribed += [d.email for d in frappe.db.get_all("Email Unsubscribe", "email",
  71. {"global_unsubscribe": 1})]
  72. else:
  73. unsubscribed = []
  74. recipients = [r for r in list(set(recipients)) if r and r not in unsubscribed]
  75. email_text_context = text_content
  76. should_append_unsubscribe = (add_unsubscribe_link
  77. and reference_doctype
  78. and (unsubscribe_message or reference_doctype=="Newsletter")
  79. and add_unsubscribe_link==1)
  80. unsubscribe_link = None
  81. if should_append_unsubscribe:
  82. unsubscribe_link = get_unsubscribe_message(unsubscribe_message, expose_recipients)
  83. email_text_context += unsubscribe_link.text
  84. email_content = get_formatted_html(subject, message,
  85. email_account=email_account, header=header,
  86. unsubscribe_link=unsubscribe_link)
  87. # add to queue
  88. add(recipients, sender, subject,
  89. formatted=email_content,
  90. text_content=email_text_context,
  91. reference_doctype=reference_doctype,
  92. reference_name=reference_name,
  93. attachments=attachments,
  94. reply_to=reply_to,
  95. cc=cc,
  96. message_id=message_id,
  97. in_reply_to=in_reply_to,
  98. send_after=send_after,
  99. send_priority=send_priority,
  100. email_account=email_account,
  101. communication=communication,
  102. add_unsubscribe_link=add_unsubscribe_link,
  103. unsubscribe_method=unsubscribe_method,
  104. unsubscribe_params=unsubscribe_params,
  105. expose_recipients=expose_recipients,
  106. read_receipt=read_receipt,
  107. queue_separately=queue_separately,
  108. is_notification = is_notification,
  109. inline_images = inline_images,
  110. header=header,
  111. now=now)
  112. def add(recipients, sender, subject, **kwargs):
  113. """Add to Email Queue"""
  114. if kwargs.get('queue_separately') or len(recipients) > 20:
  115. email_queue = None
  116. for r in recipients:
  117. if not email_queue:
  118. email_queue = get_email_queue([r], sender, subject, **kwargs)
  119. if kwargs.get('now'):
  120. email_queue(email_queue.name, now=True)
  121. else:
  122. duplicate = email_queue.get_duplicate([r])
  123. duplicate.insert(ignore_permissions=True)
  124. if kwargs.get('now'):
  125. send_one(duplicate.name, now=True)
  126. frappe.db.commit()
  127. else:
  128. email_queue = get_email_queue(recipients, sender, subject, **kwargs)
  129. if kwargs.get('now'):
  130. send_one(email_queue.name, now=True)
  131. def get_email_queue(recipients, sender, subject, **kwargs):
  132. '''Make Email Queue object'''
  133. e = frappe.new_doc('Email Queue')
  134. e.priority = kwargs.get('send_priority')
  135. attachments = kwargs.get('attachments')
  136. if attachments:
  137. # store attachments with fid, to be attached on-demand later
  138. _attachments = []
  139. for att in attachments:
  140. if att.get('fid'):
  141. _attachments.append(att)
  142. e.attachments = json.dumps(_attachments)
  143. try:
  144. mail = get_email(recipients,
  145. sender=sender,
  146. subject=subject,
  147. formatted=kwargs.get('formatted'),
  148. text_content=kwargs.get('text_content'),
  149. attachments=kwargs.get('attachments'),
  150. reply_to=kwargs.get('reply_to'),
  151. cc=kwargs.get('cc'),
  152. email_account=kwargs.get('email_account'),
  153. expose_recipients=kwargs.get('expose_recipients'),
  154. inline_images=kwargs.get('inline_images'),
  155. header=kwargs.get('header'))
  156. mail.set_message_id(kwargs.get('message_id'),kwargs.get('is_notification'))
  157. if kwargs.get('read_receipt'):
  158. mail.msg_root["Disposition-Notification-To"] = sender
  159. if kwargs.get('in_reply_to'):
  160. mail.set_in_reply_to(kwargs.get('in_reply_to'))
  161. e.message_id = mail.msg_root["Message-Id"].strip(" <>")
  162. e.message = cstr(mail.as_string())
  163. e.sender = mail.sender
  164. except frappe.InvalidEmailAddressError:
  165. # bad Email Address - don't add to queue
  166. frappe.log_error('Invalid Email ID Sender: {0}, Recipients: {1}'.format(mail.sender,
  167. ', '.join(mail.recipients)), 'Email Not Sent')
  168. e.set_recipients(recipients + kwargs.get('cc', []))
  169. e.reference_doctype = kwargs.get('reference_doctype')
  170. e.reference_name = kwargs.get('reference_name')
  171. e.add_unsubscribe_link = kwargs.get("add_unsubscribe_link")
  172. e.unsubscribe_method = kwargs.get('unsubscribe_method')
  173. e.unsubscribe_params = kwargs.get('unsubscribe_params')
  174. e.expose_recipients = kwargs.get('expose_recipients')
  175. e.communication = kwargs.get('communication')
  176. e.send_after = kwargs.get('send_after')
  177. e.show_as_cc = ",".join(kwargs.get('cc', []))
  178. e.insert(ignore_permissions=True)
  179. return e
  180. def check_email_limit(recipients):
  181. # if using settings from site_config.json, check email limit
  182. # No limit for own email settings
  183. smtp_server = SMTPServer()
  184. if (smtp_server.email_account
  185. and getattr(smtp_server.email_account, "from_site_config", False)
  186. or frappe.flags.in_test):
  187. monthly_email_limit = frappe.conf.get('limits', {}).get('emails')
  188. if frappe.flags.in_test:
  189. monthly_email_limit = 500
  190. if not monthly_email_limit:
  191. return
  192. # get count of mails sent this month
  193. this_month = get_emails_sent_this_month()
  194. if (this_month + len(recipients)) > monthly_email_limit:
  195. throw(_("Cannot send this email. You have crossed the sending limit of {0} emails for this month.").format(monthly_email_limit),
  196. EmailLimitCrossedError)
  197. def get_emails_sent_this_month():
  198. return frappe.db.sql("""select count(name) from `tabEmail Queue` where
  199. status='Sent' and MONTH(creation)=MONTH(CURDATE())""")[0][0]
  200. def get_unsubscribe_message(unsubscribe_message, expose_recipients):
  201. if unsubscribe_message:
  202. unsubscribe_html = '''<a href="<!--unsubscribe url-->"
  203. target="_blank">{0}</a>'''.format(unsubscribe_message)
  204. else:
  205. unsubscribe_link = '''<a href="<!--unsubscribe url-->"
  206. target="_blank">{0}</a>'''.format(_('Unsubscribe'))
  207. unsubscribe_html = _("{0} to stop receiving emails of this type").format(unsubscribe_link)
  208. html = """<div class="email-unsubscribe">
  209. <!--cc message-->
  210. <div>
  211. {0}
  212. </div>
  213. </div>""".format(unsubscribe_html)
  214. if expose_recipients == "footer":
  215. text = "\n<!--cc message-->"
  216. else:
  217. text = ""
  218. text += "\n\n{unsubscribe_message}: <!--unsubscribe url-->\n".format(unsubscribe_message=unsubscribe_message)
  219. return frappe._dict({
  220. "html": html,
  221. "text": text
  222. })
  223. def get_unsubcribed_url(reference_doctype, reference_name, email, unsubscribe_method, unsubscribe_params):
  224. params = {"email": email.encode("utf-8"),
  225. "doctype": reference_doctype.encode("utf-8"),
  226. "name": reference_name.encode("utf-8")}
  227. if unsubscribe_params:
  228. params.update(unsubscribe_params)
  229. query_string = get_signed_params(params)
  230. # for test
  231. frappe.local.flags.signed_query_string = query_string
  232. return get_url(unsubscribe_method + "?" + get_signed_params(params))
  233. @frappe.whitelist(allow_guest=True)
  234. def unsubscribe(doctype, name, email):
  235. # unsubsribe from comments and communications
  236. if not verify_request():
  237. return
  238. try:
  239. frappe.get_doc({
  240. "doctype": "Email Unsubscribe",
  241. "email": email,
  242. "reference_doctype": doctype,
  243. "reference_name": name
  244. }).insert(ignore_permissions=True)
  245. except frappe.DuplicateEntryError:
  246. frappe.db.rollback()
  247. else:
  248. frappe.db.commit()
  249. return_unsubscribed_page(email, doctype, name)
  250. def return_unsubscribed_page(email, doctype, name):
  251. frappe.respond_as_web_page(_("Unsubscribed"),
  252. _("{0} has left the conversation in {1} {2}").format(email, _(doctype), name),
  253. indicator_color='green')
  254. def flush(from_test=False):
  255. """flush email queue, every time: called from scheduler"""
  256. # additional check
  257. cache = frappe.cache()
  258. check_email_limit([])
  259. auto_commit = not from_test
  260. if frappe.are_emails_muted():
  261. msgprint(_("Emails are muted"))
  262. from_test = True
  263. smtpserver = SMTPServer()
  264. make_cache_queue()
  265. for i in range(cache.llen('cache_email_queue')):
  266. email = cache.lpop('cache_email_queue')
  267. if cint(frappe.defaults.get_defaults().get("hold_queue"))==1:
  268. break
  269. if email:
  270. send_one(email, smtpserver, auto_commit, from_test=from_test)
  271. # NOTE: removing commit here because we pass auto_commit
  272. # finally:
  273. # frappe.db.commit()
  274. def make_cache_queue():
  275. '''cache values in queue before sendign'''
  276. cache = frappe.cache()
  277. emails = frappe.db.sql('''select
  278. name
  279. from
  280. `tabEmail Queue`
  281. where
  282. (status='Not Sent' or status='Partially Sent') and
  283. (send_after is null or send_after < %(now)s)
  284. order
  285. by priority desc, creation asc
  286. limit 500''', { 'now': now_datetime() })
  287. # reset value
  288. cache.delete_value('cache_email_queue')
  289. for e in emails:
  290. cache.rpush('cache_email_queue', e[0])
  291. def send_one(email, smtpserver=None, auto_commit=True, now=False, from_test=False):
  292. '''Send Email Queue with given smtpserver'''
  293. email = frappe.db.sql('''select
  294. name, status, communication, message, sender, reference_doctype,
  295. reference_name, unsubscribe_param, unsubscribe_method, expose_recipients,
  296. show_as_cc, add_unsubscribe_link, attachments
  297. from
  298. `tabEmail Queue`
  299. where
  300. name=%s
  301. for update''', email, as_dict=True)[0]
  302. recipients_list = frappe.db.sql('''select name, recipient, status from
  303. `tabEmail Queue Recipient` where parent=%s''',email.name,as_dict=1)
  304. if frappe.are_emails_muted():
  305. frappe.msgprint(_("Emails are muted"))
  306. return
  307. if cint(frappe.defaults.get_defaults().get("hold_queue"))==1 :
  308. return
  309. if email.status not in ('Not Sent','Partially Sent') :
  310. # rollback to release lock and return
  311. frappe.db.rollback()
  312. return
  313. frappe.db.sql("""update `tabEmail Queue` set status='Sending', modified=%s where name=%s""",
  314. (now_datetime(), email.name), auto_commit=auto_commit)
  315. if email.communication:
  316. frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)
  317. try:
  318. if not frappe.flags.in_test:
  319. if not smtpserver: smtpserver = SMTPServer()
  320. smtpserver.setup_email_account(email.reference_doctype)
  321. for recipient in recipients_list:
  322. if recipient.status != "Not Sent":
  323. continue
  324. message = prepare_message(email, recipient.recipient, recipients_list)
  325. if not frappe.flags.in_test:
  326. smtpserver.sess.sendmail(email.sender, recipient.recipient, encode(message))
  327. recipient.status = "Sent"
  328. frappe.db.sql("""update `tabEmail Queue Recipient` set status='Sent', modified=%s where name=%s""",
  329. (now_datetime(), recipient.name), auto_commit=auto_commit)
  330. #if all are sent set status
  331. if any("Sent" == s.status for s in recipients_list):
  332. frappe.db.sql("""update `tabEmail Queue` set status='Sent', modified=%s where name=%s""",
  333. (now_datetime(), email.name), auto_commit=auto_commit)
  334. else:
  335. frappe.db.sql("""update `tabEmail Queue` set status='Error', error=%s
  336. where name=%s""", ("No recipients to send to", email.name), auto_commit=auto_commit)
  337. if frappe.flags.in_test:
  338. frappe.flags.sent_mail = message
  339. return
  340. if email.communication:
  341. frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)
  342. except (smtplib.SMTPServerDisconnected,
  343. smtplib.SMTPConnectError,
  344. smtplib.SMTPHeloError,
  345. smtplib.SMTPAuthenticationError,
  346. JobTimeoutException):
  347. # bad connection/timeout, retry later
  348. if any("Sent" == s.status for s in recipients_list):
  349. frappe.db.sql("""update `tabEmail Queue` set status='Partially Sent', modified=%s where name=%s""",
  350. (now_datetime(), email.name), auto_commit=auto_commit)
  351. else:
  352. frappe.db.sql("""update `tabEmail Queue` set status='Not Sent', modified=%s where name=%s""",
  353. (now_datetime(), email.name), auto_commit=auto_commit)
  354. if email.communication:
  355. frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)
  356. # no need to attempt further
  357. return
  358. except Exception as e:
  359. frappe.db.rollback()
  360. if any("Sent" == s.status for s in recipients_list):
  361. frappe.db.sql("""update `tabEmail Queue` set status='Partially Errored', error=%s where name=%s""",
  362. (text_type(e), email.name), auto_commit=auto_commit)
  363. else:
  364. frappe.db.sql("""update `tabEmail Queue` set status='Error', error=%s
  365. where name=%s""", (text_type(e), email.name), auto_commit=auto_commit)
  366. if email.communication:
  367. frappe.get_doc('Communication', email.communication).set_delivery_status(commit=auto_commit)
  368. if now:
  369. print(frappe.get_traceback())
  370. raise e
  371. else:
  372. # log to Error Log
  373. log('frappe.email.queue.flush', text_type(e))
  374. def prepare_message(email, recipient, recipients_list):
  375. message = email.message
  376. if not message:
  377. return ""
  378. if email.add_unsubscribe_link and email.reference_doctype: # is missing the check for unsubscribe message but will not add as there will be no unsubscribe url
  379. unsubscribe_url = get_unsubcribed_url(email.reference_doctype, email.reference_name, recipient,
  380. email.unsubscribe_method, email.unsubscribe_params)
  381. message = message.replace("<!--unsubscribe url-->", quopri.encodestring(unsubscribe_url))
  382. if email.expose_recipients == "header":
  383. pass
  384. else:
  385. if email.expose_recipients == "footer":
  386. if isinstance(email.show_as_cc, basestring):
  387. email.show_as_cc = email.show_as_cc.split(",")
  388. email_sent_to = [r.recipient for r in recipients_list]
  389. email_sent_cc = ", ".join([e for e in email_sent_to if e in email.show_as_cc])
  390. email_sent_to = ", ".join([e for e in email_sent_to if e not in email.show_as_cc])
  391. if email_sent_cc:
  392. email_sent_message = _("This email was sent to {0} and copied to {1}").format(email_sent_to,email_sent_cc)
  393. else:
  394. email_sent_message = _("This email was sent to {0}").format(email_sent_to)
  395. message = message.replace("<!--cc message-->", quopri.encodestring(email_sent_message))
  396. message = message.replace("<!--recipient-->", recipient)
  397. message = (message and message.encode('utf8')) or ''
  398. if not email.attachments:
  399. return message
  400. # On-demand attachments
  401. from email.parser import Parser
  402. msg_obj = Parser().parsestr(message)
  403. attachments = json.loads(email.attachments)
  404. for attachment in attachments:
  405. if attachment.get('fcontent'): continue
  406. fid = attachment.get('fid')
  407. if not fid: continue
  408. fname, fcontent = get_file(fid)
  409. attachment.update({
  410. 'fname': fname,
  411. 'fcontent': fcontent,
  412. 'parent': msg_obj
  413. })
  414. attachment.pop("fid", None)
  415. add_attachment(**attachment)
  416. return msg_obj.as_string()
  417. def clear_outbox():
  418. """Remove low priority older than 31 days in Outbox and expire mails not sent for 7 days.
  419. Called daily via scheduler.
  420. Note: Used separate query to avoid deadlock
  421. """
  422. email_queues = frappe.db.sql_list("""select name from `tabEmail Queue`
  423. where priority=0 and datediff(now(), modified) > 31""")
  424. if email_queues:
  425. frappe.db.sql("""delete from `tabEmail Queue` where name in (%s)"""
  426. % ','.join(['%s']*len(email_queues)), tuple(email_queues))
  427. frappe.db.sql("""delete from `tabEmail Queue Recipient` where parent in (%s)"""
  428. % ','.join(['%s']*len(email_queues)), tuple(email_queues))
  429. frappe.db.sql("""
  430. update `tabEmail Queue`
  431. set status='Expired'
  432. where datediff(curdate(), modified) > 7 and status='Not Sent' and (send_after is null or send_after < %(now)s)""", { 'now': now_datetime() })