aiosmtplib icon indicating copy to clipboard operation
aiosmtplib copied to clipboard

Support URL for connection

Open toxadx opened this issue 4 years ago • 1 comments

It would be nice to be able to pass url instead of connection parameters.

toxadx avatar Jan 12 '21 14:01 toxadx

Possible implementation:

from typing import Dict, Union
from urllib.parse import urlparse

def parse_smtp_url(url: str) -> Dict[str, Union[str, int]]:
    """Parse SMTP url to parameters to be used in aiosmtplib.

    Args:
        url: [proto://[username:password@]]host[:port][/socket_path]

    Returns:
        dict of aiosmtplib kwargs.

    """
    parsed_url = urlparse(url)
    mail_settings = {
        'hostname': parsed_url.hostname,
        'port': parsed_url.port,
        'username': parsed_url.username,
        'password': parsed_url.password,
        'socket_path': parsed_url.path,
    }
    if parsed_url.scheme == 'smtps':
        mail_settings['use_tls'] = True
    elif parsed_url.scheme == 'smtp+tls':
        mail_settings['start_tls'] = True
    return {
        parameter: param_value
        for parameter, param_value in mail_settings.items()
        if param_value
    }

toxadx avatar Jul 27 '21 16:07 toxadx