Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://python-emails.readthedocs.io/en/latest/
- # https://requests.kennethreitz.org/en/master/
- # mx lookup is not implemented
- import json
- import getpass
- import xml.etree.ElementTree as ET
- import requests
- import emails
- def get_from_isp(domain, email):
- """
- Read: https://developer.mozilla.org/de/docs/Mozilla/Thunderbird/Autokonfiguration#Configuration_server_at_ISP
- """
- isp = f'http://autoconfig.{domain}/mail/config-v1.1.xml?emailaddress={email}'
- auto = f'https://{domain}/.well-known/autoconfig/mail/config-v1.1.xml'
- try:
- req = requests.get(isp)
- except:
- try:
- req = requests.get(auto)
- except:
- return None
- if req.status_code != 200:
- raise ValueError('Domain not found.')
- return ET.fromstring(req.content)
- def get_from_mozilla(domain):
- """
- Read: https://developer.mozilla.org/de/docs/Mozilla/Thunderbird/Autokonfiguration#ISPDB
- """
- req = requests.get(f'https://autoconfig.thunderbird.net/v1.1/{domain}')
- if req.status_code != 200:
- raise ValueError('Domain not found.')
- return ET.fromstring(req.content)
- def email_rep(text, user, address):
- """
- https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration/FileFormat/HowTo#Username
- """
- text = text.replace('%EMAILADDRESS%', address)
- text = text.replace('%EMAILLOCALPART%', user)
- return text
- def parse_xml(xml, user, address):
- """
- https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration/FileFormat/HowTo#Definition
- """
- results = []
- for server in xml.findall('emailProvider/outgoingServer'):
- result = {
- 'hostname': server.find('hostname').text,
- 'port': int(server.find('port').text),
- 'sock_type': server.find('socketType').text,
- 'username': email_rep(
- server.find('username').text,
- user,
- address),
- 'auth': server.find('authentication').text,
- }
- results.append(result)
- return results
- def parse_email(email):
- username, domain = email.strip().rsplit('@', 1)
- return username, domain
- def get_smtp(email):
- try:
- username, domain = parse_email(email)
- except ValueError:
- ValueError(f'Could not parse {email}')
- try:
- xml = get_from_isp(domain, email)
- except:
- print(f'ISP has no info, trying mozilla')
- try:
- xml = get_from_mozilla(domain)
- except:
- ValueError(f'Could not find "{domain}".')
- try:
- results = parse_xml(xml, username, email)
- except:
- ValueError('Could not parse results')
- return results
- def send(smtps, email):
- mail_to = input('Mail to: ')
- pw = getpass.getpass('Password: ')
- msg = emails.Message(
- charset='utf8', subject='Alarma',
- mail_from=email,
- mail_to=mail_to,
- text='Alarm test'
- )
- smtps = smtps.pop(0)
- smtp = {
- 'host': smtps['hostname'],
- 'port': smtps['port'],
- 'user': smtps['username'],
- 'password': pw,
- 'ssl': True if smtps['sock_type'] == 'SSL' else False
- }
- if msg.send(smtp=smtp).status_code == 250:
- print('Success')
- else:
- print('No success')
- def main():
- try:
- email = input('Email: ')
- results = get_smtp(email)
- send(results, email)
- except Exception as e:
- print(e)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement