diff options
| -rw-r--r-- | fietsboek/email.py | 23 | 
1 files changed, 10 insertions, 13 deletions
| diff --git a/fietsboek/email.py b/fietsboek/email.py index ee111a1..038acb2 100644 --- a/fietsboek/email.py +++ b/fietsboek/email.py @@ -3,25 +3,22 @@ import logging  import smtplib  import sys  from email.message import EmailMessage +from typing import Optional  from urllib.parse import urlparse  LOGGER = logging.getLogger(__name__) -def prepare_message(sender, addr_to, subject): +def prepare_message(sender: str, addr_to: str, subject: str) -> EmailMessage:      """Prepares an email message with the right headers.      The body of the message can be set by using      :meth:`~email.message.EmailMessage.set_content` on the returned message.      :param sender: The email sender. -    :type sender: str      :param addr_to: Address of the recipient. -    :type addr_to: str      :param subject: Subject of the message. -    :type subject: str      :return: A prepared message. -    :rtype: email.message.EmailMessage      """      message = EmailMessage()      message["To"] = addr_to @@ -33,31 +30,31 @@ def prepare_message(sender, addr_to, subject):      return message -def send_message(server_url, username, password, message): +def send_message( +        server_url: str, username: Optional[str], password: Optional[str], message: EmailMessage +):      """Sends an email message using the STMP server configured in the settings.      The recipient is taken from the 'To'-header of the message.      :param server_url: The URL of the server for mail sending. -    :type server_url: str      :param username: The username to authenticate, can be ``None`` or empty. -    :type username str:      :param password: The password to authenticate, can be ``None`` or empty. -    :type password: str      :param message: The message to send. -    :type message: email.message.EmailMessage      """      parsed_url = urlparse(server_url)      if parsed_url.scheme == "debug":          print(message, file=sys.stderr)          return +    hostname = parsed_url.hostname if parsed_url.hostname is not None else "localhost"      try: +        # Use 0 to let smtplib pick the default port          if parsed_url.scheme == "smtp": -            client = smtplib.SMTP(parsed_url.hostname, parsed_url.port) +            client = smtplib.SMTP(hostname, parsed_url.port or 0)          elif parsed_url.scheme == "smtp+ssl": -            client = smtplib.SMTP_SSL(parsed_url.hostname, parsed_url.port) +            client = smtplib.SMTP_SSL(hostname, parsed_url.port or 0)          elif parsed_url.scheme == "smtp+starttls": -            client = smtplib.SMTP(parsed_url.hostname, parsed_url.port) +            client = smtplib.SMTP(hostname, parsed_url.port or 0)              client.starttls()          if username and password:              client.login(username, password) | 
