Doc for send email
This commit is contained in:
parent
04db977d37
commit
0b06a0b0ce
|
@ -47,6 +47,15 @@ from urllib import parse
|
|||
from urllib.request import Request, urlopen
|
||||
from urllib.error import URLError, HTTPError
|
||||
|
||||
import mailbox
|
||||
import smtplib
|
||||
from smtplib import SMTPException, SMTPAuthenticationError
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.text import MIMEText
|
||||
from email.utils import formatdate
|
||||
from email import encoders
|
||||
|
||||
import uno
|
||||
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
|
||||
from com.sun.star.awt.MessageBoxResults import YES
|
||||
|
@ -1636,7 +1645,7 @@ class Url(object):
|
|||
class Email(object):
|
||||
"""Class for send email
|
||||
"""
|
||||
class SmtpServer(object):
|
||||
class _SmtpServer(object):
|
||||
|
||||
def __init__(self, config):
|
||||
self._server = None
|
||||
|
@ -1662,10 +1671,10 @@ class Email(object):
|
|||
name = config['server']
|
||||
port = config['port']
|
||||
is_ssl = config['ssl']
|
||||
starttls = config.get('starttls', False)
|
||||
self._sender = config['user']
|
||||
hosts = ('gmail' in name or 'outlook' in name)
|
||||
try:
|
||||
if is_ssl and hosts:
|
||||
if starttls:
|
||||
self._server = smtplib.SMTP(name, port, timeout=TIMEOUT)
|
||||
self._server.ehlo()
|
||||
self._server.starttls()
|
||||
|
@ -1716,7 +1725,7 @@ class Email(object):
|
|||
paths = (paths,)
|
||||
for path in paths:
|
||||
fn = _P(path).file_name
|
||||
print('NAME', fn)
|
||||
# ~ print('NAME', fn)
|
||||
part = MIMEBase('application', 'octet-stream')
|
||||
part.set_payload(_P.read_bin(path))
|
||||
encoders.encode_base64(part)
|
||||
|
@ -1761,7 +1770,7 @@ class Email(object):
|
|||
|
||||
@classmethod
|
||||
def _send_email(cls, server, messages):
|
||||
with cls.SmtpServer(server) as server:
|
||||
with cls._SmtpServer(server) as server:
|
||||
if server.is_connect:
|
||||
for msg in messages:
|
||||
server.send(msg)
|
||||
|
@ -1771,6 +1780,13 @@ class Email(object):
|
|||
|
||||
@classmethod
|
||||
def send(cls, server: dict, messages: Union[dict, tuple, list]):
|
||||
"""Send email with config server, emails send in thread.
|
||||
|
||||
:param server: Configuration for send emails
|
||||
:type server: dict
|
||||
:param server: Dictionary con message or list of messages
|
||||
:type server: dict or iterator
|
||||
"""
|
||||
if isinstance(messages, dict):
|
||||
messages = (messages,)
|
||||
t = threading.Thread(target=cls._send_email, args=(server, messages))
|
||||
|
|
Loading…
Reference in New Issue