SHARE
TWEET

email-stuff

DeaD_EyE Nov 19th, 2019 197 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://python-emails.readthedocs.io/en/latest/
  2. # https://requests.kennethreitz.org/en/master/
  3.  
  4. # mx lookup is not implemented
  5.  
  6. import json
  7. import getpass
  8. import xml.etree.ElementTree as ET
  9.  
  10. import requests
  11. import emails
  12.  
  13.  
  14. def get_from_isp(domain, email):
  15.     """
  16.    Read: https://developer.mozilla.org/de/docs/Mozilla/Thunderbird/Autokonfiguration#Configuration_server_at_ISP
  17.    """
  18.     isp = f'http://autoconfig.{domain}/mail/config-v1.1.xml?emailaddress={email}'
  19.     auto = f'https://{domain}/.well-known/autoconfig/mail/config-v1.1.xml'
  20.     try:
  21.         req = requests.get(isp)
  22.     except:
  23.         try:
  24.             req = requests.get(auto)
  25.         except:
  26.             return None
  27.     if req.status_code != 200:
  28.         raise ValueError('Domain not found.')
  29.     return ET.fromstring(req.content)
  30.  
  31.  
  32. def get_from_mozilla(domain):
  33.     """
  34.    Read: https://developer.mozilla.org/de/docs/Mozilla/Thunderbird/Autokonfiguration#ISPDB
  35.    """
  36.     req = requests.get(f'https://autoconfig.thunderbird.net/v1.1/{domain}')
  37.     if req.status_code != 200:
  38.         raise ValueError('Domain not found.')
  39.     return ET.fromstring(req.content)
  40.  
  41.  
  42. def email_rep(text, user, address):
  43.     """
  44.    https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration/FileFormat/HowTo#Username
  45.    """
  46.     text = text.replace('%EMAILADDRESS%', address)
  47.     text = text.replace('%EMAILLOCALPART%', user)
  48.     return text
  49.    
  50.  
  51. def parse_xml(xml, user, address):
  52.     """
  53.    https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration/FileFormat/HowTo#Definition
  54.    """
  55.     results = []
  56.     for server in xml.findall('emailProvider/outgoingServer'):
  57.         result = {
  58.             'hostname': server.find('hostname').text,
  59.             'port': int(server.find('port').text),
  60.             'sock_type': server.find('socketType').text,
  61.             'username': email_rep(
  62.                 server.find('username').text,
  63.                 user,
  64.                 address),
  65.             'auth': server.find('authentication').text,
  66.         }
  67.         results.append(result)
  68.     return results
  69.  
  70.    
  71. def parse_email(email):
  72.     username, domain = email.strip().rsplit('@', 1)
  73.     return username, domain
  74.  
  75.  
  76. def get_smtp(email):
  77.     try:
  78.         username, domain = parse_email(email)
  79.     except ValueError:
  80.         ValueError(f'Could not parse {email}')    
  81.     try:
  82.         xml = get_from_isp(domain, email)
  83.     except:
  84.         print(f'ISP has no info, trying mozilla')
  85.         try:
  86.             xml = get_from_mozilla(domain)
  87.         except:
  88.             ValueError(f'Could not find "{domain}".')
  89.     try:
  90.         results = parse_xml(xml, username, email)
  91.     except:
  92.         ValueError('Could not parse results')
  93.     return results
  94.  
  95.  
  96. def send(smtps, email):
  97.     mail_to = input('Mail to: ')
  98.     pw = getpass.getpass('Password: ')
  99.     msg = emails.Message(
  100.         charset='utf8', subject='Alarma',
  101.         mail_from=email,
  102.         mail_to=mail_to,
  103.         text='Alarm test'
  104.     )
  105.     smtps = smtps.pop(0)
  106.     smtp = {
  107.         'host': smtps['hostname'],
  108.         'port': smtps['port'],
  109.         'user': smtps['username'],
  110.         'password': pw,
  111.         'ssl': True if smtps['sock_type'] == 'SSL' else False
  112.     }
  113.     if msg.send(smtp=smtp).status_code == 250:
  114.         print('Success')
  115.     else:
  116.         print('No success')
  117.  
  118.  
  119. def main():
  120.     try:
  121.         email = input('Email: ')
  122.         results = get_smtp(email)
  123.         send(results, email)
  124.     except Exception as e:
  125.         print(e)
  126.  
  127.  
  128. if __name__ == '__main__':
  129.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top