seitime-frappe/frappe/email/test_smtp.py
mtraeber f608fbeddd feat: sync mutliple IMAP folders in Email Account
When working with IMAP accounts, frappe should allow the user
to choose multiple folders to look for new mails. This helps
users to separate their frappe-related email from other
conversations. Use cases range from sieve filters in the
mail server that stuff incoming mail in various mail folders
to people manually sorting their e-mail. In both cases, we
can have different import policies for different folders, and
we can avoid importing unrelated email.

Created a new child table `IMAP Folder` with following fields:
 - Folder Name (user-modifiable)
 - Append To (user-modifiable)
 - UIDVALIDITY (hidden)
 - UIDNEXT (hidden)

Doctype `Email Account` and `receive.py` code adjusted so that
emails with the changes are processed correctly and Frappe
only logs in to the imap server once per sync.

Created a patch that copies the data from the old fields into
the new child table with `INBOX` as default `folder_name`. This
keeps existing setups working without manual changes.

The original fields
 - uidvalidity
 - uidnext
 - append_to
are still available for the pop3 setups. In IMAP, these fields
are hidden user and not used.

Added a test case in `Email Account` that validates data to make
sure a IMAP folder is provided if the use_imap is true.

Also added some code formatting changes in email_account.js to get rid
of sider checks failures that block this change
2021-10-13 10:27:18 +02:00

80 lines
2.7 KiB
Python

# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# License: The MIT License
import unittest
import frappe
from frappe.email.smtp import SMTPServer
from frappe.email.doctype.email_account.email_account import EmailAccount
class TestSMTP(unittest.TestCase):
def test_smtp_ssl_session(self):
for port in [None, 0, 465, "465"]:
make_server(port, 1, 0)
def test_smtp_tls_session(self):
for port in [None, 0, 587, "587"]:
make_server(port, 0, 1)
def test_get_email_account(self):
existing_email_accounts = frappe.get_all("Email Account", fields = ["name", "enable_outgoing", "default_outgoing","append_to", "use_imap"])
unset_details = {
"enable_outgoing": 0,
"default_outgoing": 0,
"append_to": None,
"use_imap": 0
}
for email_account in existing_email_accounts:
frappe.db.set_value('Email Account', email_account['name'], unset_details)
# remove mail_server config so that test@example.com is not created
mail_server = frappe.conf.get('mail_server')
del frappe.conf['mail_server']
frappe.local.outgoing_email_account = {}
frappe.local.outgoing_email_account = {}
# lowest preference given to email account with default incoming enabled
create_email_account(email_id="default_outgoing_enabled@gmail.com", password="password", enable_outgoing = 1, default_outgoing=1)
self.assertEqual(EmailAccount.find_outgoing().email_id, "default_outgoing_enabled@gmail.com")
frappe.local.outgoing_email_account = {}
# highest preference given to email account with append_to matching
create_email_account(email_id="append_to@gmail.com", password="password", enable_outgoing = 1, default_outgoing=1, append_to="Blog Post")
self.assertEqual(EmailAccount.find_outgoing(match_by_doctype="Blog Post").email_id, "append_to@gmail.com")
# add back the mail_server
frappe.conf['mail_server'] = mail_server
for email_account in existing_email_accounts:
set_details = {
"enable_outgoing": email_account['enable_outgoing'],
"default_outgoing": email_account['default_outgoing'],
"append_to": email_account['append_to']
}
frappe.db.set_value('Email Account', email_account['name'], set_details)
def create_email_account(email_id, password, enable_outgoing, default_outgoing=0, append_to=None):
email_dict = {
"email_id": email_id,
"passsword": password,
"enable_outgoing":enable_outgoing ,
"default_outgoing":default_outgoing ,
"enable_incoming": 1,
"append_to":append_to,
"is_dummy_password": 1,
"smtp_server": "localhost",
"use_imap": 0
}
email_account = frappe.new_doc('Email Account')
email_account.update(email_dict)
email_account.save()
def make_server(port, ssl, tls):
server = SMTPServer(
server = "smtp.gmail.com",
port = port,
use_ssl = ssl,
use_tls = tls
)
server.session