aiosmtplib
aiosmtplib copied to clipboard
Support URL for connection
It would be nice to be able to pass url instead of connection parameters.
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
}