Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support URL for connection #168

Open
toxadx opened this issue Jan 12, 2021 · 1 comment
Open

Support URL for connection #168

toxadx opened this issue Jan 12, 2021 · 1 comment
Labels
🚀 feature something that would be nice

Comments

@toxadx
Copy link

toxadx commented Jan 12, 2021

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

@cole cole added the 🚀 feature something that would be nice label Apr 25, 2021
@toxadx
Copy link
Author

toxadx commented Jul 27, 2021

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
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 feature something that would be nice
Projects
None yet
Development

No branches or pull requests

2 participants