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.request import Request, urlopen
|
||||||
from urllib.error import URLError, HTTPError
|
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
|
import uno
|
||||||
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
|
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
|
||||||
from com.sun.star.awt.MessageBoxResults import YES
|
from com.sun.star.awt.MessageBoxResults import YES
|
||||||
|
@ -1636,7 +1645,7 @@ class Url(object):
|
||||||
class Email(object):
|
class Email(object):
|
||||||
"""Class for send email
|
"""Class for send email
|
||||||
"""
|
"""
|
||||||
class SmtpServer(object):
|
class _SmtpServer(object):
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self._server = None
|
self._server = None
|
||||||
|
@ -1662,10 +1671,10 @@ class Email(object):
|
||||||
name = config['server']
|
name = config['server']
|
||||||
port = config['port']
|
port = config['port']
|
||||||
is_ssl = config['ssl']
|
is_ssl = config['ssl']
|
||||||
|
starttls = config.get('starttls', False)
|
||||||
self._sender = config['user']
|
self._sender = config['user']
|
||||||
hosts = ('gmail' in name or 'outlook' in name)
|
|
||||||
try:
|
try:
|
||||||
if is_ssl and hosts:
|
if starttls:
|
||||||
self._server = smtplib.SMTP(name, port, timeout=TIMEOUT)
|
self._server = smtplib.SMTP(name, port, timeout=TIMEOUT)
|
||||||
self._server.ehlo()
|
self._server.ehlo()
|
||||||
self._server.starttls()
|
self._server.starttls()
|
||||||
|
@ -1716,7 +1725,7 @@ class Email(object):
|
||||||
paths = (paths,)
|
paths = (paths,)
|
||||||
for path in paths:
|
for path in paths:
|
||||||
fn = _P(path).file_name
|
fn = _P(path).file_name
|
||||||
print('NAME', fn)
|
# ~ print('NAME', fn)
|
||||||
part = MIMEBase('application', 'octet-stream')
|
part = MIMEBase('application', 'octet-stream')
|
||||||
part.set_payload(_P.read_bin(path))
|
part.set_payload(_P.read_bin(path))
|
||||||
encoders.encode_base64(part)
|
encoders.encode_base64(part)
|
||||||
|
@ -1761,7 +1770,7 @@ class Email(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _send_email(cls, server, messages):
|
def _send_email(cls, server, messages):
|
||||||
with cls.SmtpServer(server) as server:
|
with cls._SmtpServer(server) as server:
|
||||||
if server.is_connect:
|
if server.is_connect:
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
server.send(msg)
|
server.send(msg)
|
||||||
|
@ -1771,6 +1780,13 @@ class Email(object):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def send(cls, server: dict, messages: Union[dict, tuple, list]):
|
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):
|
if isinstance(messages, dict):
|
||||||
messages = (messages,)
|
messages = (messages,)
|
||||||
t = threading.Thread(target=cls._send_email, args=(server, messages))
|
t = threading.Thread(target=cls._send_email, args=(server, messages))
|
||||||
|
|
Loading…
Reference in New Issue