def insert_communication(self, msg, args=None):
        if isinstance(msg, list):
            raw, uid, seen = msg
        else:
            raw = msg
            uid = -1
            seen = 0
        if isinstance(args, dict):
            if args.get("uid", -1): uid = args.get("uid", -1)
            if args.get("seen", 0): seen = args.get("seen", 0)

        email = Email(raw)

        if email.from_email == self.email_id and not email.mail.get(
                "Reply-To"):
            # gmail shows sent emails in inbox
            # and we don't want emails sent by us to be pulled back into the system again
            # dont count emails sent by the system get those
            if frappe.flags.in_test:
                print(
                    'WARN: Cannot pull email. Sender sames as recipient inbox')
            raise SentEmailInInbox

        if email.message_id:
            # https://stackoverflow.com/a/18367248
            names = frappe.db.sql(
                """SELECT DISTINCT `name`, `creation` FROM `tabCommunication`
				WHERE `message_id`='{message_id}'
				ORDER BY `creation` DESC LIMIT 1""".format(message_id=email.message_id),
                as_dict=True)

            if names:
                name = names[0].get("name")
                # email is already available update communication uid instead
                frappe.db.set_value("Communication",
                                    name,
                                    "uid",
                                    uid,
                                    update_modified=False)
                return frappe.get_doc("Communication", name)

        if email.content_type == 'text/html':
            email.content = clean_email_html(email.content)

        communication = frappe.get_doc({
            "doctype":
            "Communication",
            "subject":
            email.subject,
            "content":
            email.content,
            'text_content':
            email.text_content,
            "sent_or_received":
            "Received",
            "sender_full_name":
            email.from_real_name,
            "sender":
            email.from_email,
            "recipients":
            email.mail.get("To"),
            "cc":
            email.mail.get("CC"),
            "email_account":
            self.name,
            "communication_medium":
            "Email",
            "uid":
            int(uid or -1),
            "message_id":
            email.message_id,
            "communication_date":
            email.date,
            "has_attachment":
            1 if email.attachments else 0,
            "seen":
            seen or 0
        })

        self.set_thread(communication, email)
        if communication.seen:
            # get email account user and set communication as seen
            users = frappe.get_all("User Email",
                                   filters={"email_account": self.name},
                                   fields=["parent"])
            users = list(set([user.get("parent") for user in users]))
            communication._seen = json.dumps(users)

        communication.flags.in_receive = True
        communication.insert(ignore_permissions=True)

        # save attachments
        communication._attachments = email.save_attachments_in_doc(
            communication)

        # replace inline images
        dirty = False
        for file in communication._attachments:
            if file.name in email.cid_map and email.cid_map[file.name]:
                dirty = True

                email.content = email.content.replace(
                    "cid:{0}".format(email.cid_map[file.name]), file.file_url)

        if dirty:
            # not sure if using save() will trigger anything
            communication.db_set("content", sanitize_html(email.content))

        # notify all participants of this thread
        if self.enable_auto_reply and getattr(communication, "is_first",
                                              False):
            self.send_auto_reply(communication, email)

        return communication
    def insert_communication(self, msg, args={}):
        if isinstance(msg, list):
            raw, uid, seen = msg
        else:
            raw = msg
            uid = -1
            seen = 0

        if args.get("uid", -1): uid = args.get("uid", -1)
        if args.get("seen", 0): seen = args.get("seen", 0)

        email = Email(raw)

        if email.from_email == self.email_id and not email.mail.get(
                "Reply-To"):
            # gmail shows sent emails in inbox
            # and we don't want emails sent by us to be pulled back into the system again
            # dont count emails sent by the system get those
            raise SentEmailInInbox

        if email.message_id:
            names = frappe.db.sql(
                """select distinct name from tabCommunication 
				where message_id='{message_id}'
				order by creation desc limit 1""".format(message_id=email.message_id),
                as_dict=True)

            if names:
                name = names[0].get("name")
                # email is already available update communication uid instead
                communication = frappe.get_doc("Communication", name)
                communication.uid = uid
                communication.save(ignore_permissions=True)
                communication._attachments = []

                return communication

        communication = frappe.get_doc({
            "doctype":
            "Communication",
            "subject":
            email.subject,
            "content":
            email.content,
            'text_content':
            email.text_content,
            "sent_or_received":
            "Received",
            "sender_full_name":
            email.from_real_name,
            "sender":
            email.from_email,
            "recipients":
            email.mail.get("To"),
            "cc":
            email.mail.get("CC"),
            "email_account":
            self.name,
            "communication_medium":
            "Email",
            "uid":
            int(uid or -1),
            "message_id":
            email.message_id,
            "communication_date":
            email.date,
            "has_attachment":
            1 if email.attachments else 0,
            "seen":
            seen or 0
        })

        self.set_thread(communication, email)
        if communication.seen:
            # get email account user and set communication as seen
            users = frappe.get_all("User Email",
                                   filters={"email_account": self.name},
                                   fields=["parent"])
            users = list(set([user.get("parent") for user in users]))
            communication._seen = json.dumps(users)

        communication.flags.in_receive = True
        communication.insert(ignore_permissions=1)

        # save attachments
        communication._attachments = email.save_attachments_in_doc(
            communication)

        # replace inline images
        dirty = False
        for file in communication._attachments:
            if file.name in email.cid_map and email.cid_map[file.name]:
                dirty = True

                email.content = email.content.replace(
                    "cid:{0}".format(email.cid_map[file.name]), file.file_url)

        if dirty:
            # not sure if using save() will trigger anything
            communication.db_set("content", sanitize_html(email.content))

        # notify all participants of this thread
        if self.enable_auto_reply and getattr(communication, "is_first",
                                              False):
            self.send_auto_reply(communication, email)

        return communication
Exemple #3
0
	def insert_communication(self, msg):
		if isinstance(msg,list):
			raw, uid, seen = msg
		else:
			raw = msg
			seen = uid = None
		email = Email(raw)

		if email.from_email == self.email_id and not email.mail.get("Reply-To"):
			# gmail shows sent emails in inbox
			# and we don't want emails sent by us to be pulled back into the system again
			# dont count emails sent by the system get those
			raise SentEmailInInbox
		
		communication = frappe.get_doc({
			"doctype": "Communication",
			"subject": email.subject,
			"content": email.content,
			"sent_or_received": "Received",
			"sender_full_name": email.from_real_name,
			"sender": email.from_email,
			"recipients": email.To,
			"cc": email.CC,
			"email_account": self.name,
			"communication_medium": "Email",
			"uid":uid,
			"message_id":email.message_id,
			"communication_date":email.date,
			"has_attachment": 1 if email.attachments else 0,
			"seen":seen,
			"unique_id":email.unique_id
		})

		self.set_thread(communication, email)

		if not self.no_remaining == '0':
			communication.unread_notification_sent = 1

		communication.flags.in_receive = True
		communication.insert(ignore_permissions = 1)

		# save attachments
		communication._attachments = email.save_attachments_in_doc(communication)

		# replace inline images


		dirty = False
		for file in communication._attachments:
			if file.name in email.cid_map and email.cid_map[file.name]:
				dirty = True

				email.content = email.content.replace("cid:{0}".format(email.cid_map[file.name]),
					file.file_url)

		if dirty:
			# not sure if using save() will trigger anything
			communication.db_set("content", sanitize_html(email.content))

		# notify all participants of this thread
		if self.enable_auto_reply and getattr(communication, "is_first", False):
			self.send_auto_reply(communication, email)

		return communication
Exemple #4
0
	def insert_communication(self, msg, args={}):
		if isinstance(msg, list):
			raw, uid, seen = msg
		else:
			raw = msg
			uid = -1
			seen = 0

		if args.get("uid", -1): uid = args.get("uid", -1)
		if args.get("seen", 0): seen = args.get("seen", 0)

		email = Email(raw)

		if email.from_email == self.email_id and not email.mail.get("Reply-To"):
			# gmail shows sent emails in inbox
			# and we don't want emails sent by us to be pulled back into the system again
			# dont count emails sent by the system get those
			if frappe.flags.in_test:
				print('WARN: Cannot pull email. Sender sames as recipient inbox')
			raise SentEmailInInbox

		if email.message_id:
			names = frappe.db.sql("""select distinct name from tabCommunication
				where message_id='{message_id}'
				order by creation desc limit 1""".format(
					message_id=email.message_id
				), as_dict=True)

			if names:
				name = names[0].get("name")
				# email is already available update communication uid instead
				frappe.db.set_value("Communication", name, "uid", uid, update_modified=False)
				return

		communication = frappe.get_doc({
			"doctype": "Communication",
			"subject": email.subject,
			"content": email.content,
			'text_content': email.text_content,
			"sent_or_received": "Received",
			"sender_full_name": email.from_real_name,
			"sender": email.from_email,
			"recipients": email.mail.get("To"),
			"cc": email.mail.get("CC"),
			"email_account": self.name,
			"communication_medium": "Email",
			"uid": int(uid or -1),
			"message_id": email.message_id,
			"communication_date": email.date,
			"has_attachment": 1 if email.attachments else 0,
			"seen": seen or 0
		})

		self.set_thread(communication, email)
		if communication.seen:
			# get email account user and set communication as seen
			users = frappe.get_all("User Email", filters={ "email_account": self.name },
				fields=["parent"])
			users = list(set([ user.get("parent") for user in users ]))
			communication._seen = json.dumps(users)

		communication.flags.in_receive = True
		communication.insert(ignore_permissions = 1)

		# save attachments
		communication._attachments = email.save_attachments_in_doc(communication)

		# replace inline images
		dirty = False
		for file in communication._attachments:
			if file.name in email.cid_map and email.cid_map[file.name]:
				dirty = True

				email.content = email.content.replace("cid:{0}".format(email.cid_map[file.name]),
					file.file_url)

		if dirty:
			# not sure if using save() will trigger anything
			communication.db_set("content", sanitize_html(email.content))

		# notify all participants of this thread
		if self.enable_auto_reply and getattr(communication, "is_first", False):
			self.send_auto_reply(communication, email)

		return communication