Guest User

Untitled

a guest
Aug 24th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import re
  2. import smtplib
  3. import sys
  4. import socket
  5. import os
  6. from time import gmtime, strftime  
  7. import dns.resolver
  8.  
  9. #settings
  10. mode = 'vuw'
  11.  
  12. if mode == 'vuw':
  13.     #use ecs vuw mail server
  14.     sender = 'andersbenj1@ecs.vuw.ac.nz'
  15.     server = 'mail.ecs.vuw.ac.nz'
  16.     server_port = 25
  17.     server_requires_authentication = False
  18. elif mode == 'home':
  19.     #use acidic.co.nz mail server
  20.     #tutors / markers - use this if you want, but please don't hammer my mail server
  21.     sender = 'nwen@acidic.co.nz'
  22.     server = 'mail.acidic.co.nz'
  23.     server_port = 587
  24.     server_requires_authentication = True
  25.     server_username = 'nwen+acidic.co.nz'
  26.     server_password = 'nwen123'
  27.  
  28.  
  29. def checkHostname(host):
  30.     try:
  31.         dns.resolver.query(host, "MX")
  32.         return True
  33.     except Exception:
  34.         return False
  35.  
  36. if len(sys.argv) > 1:
  37.     to = sys.argv[1]
  38. else:
  39.     to = input("Enter the email address to send to: ")
  40.  
  41. # check the email as a whole is roughly valid
  42. if not re.match(r"[^@]+@[^@]+\.[^@]+", to):
  43.     print("The email address you entered isn't valid")
  44.     sys.exit(1)
  45.    
  46. # check the domain is valid
  47. domain = to.split("@")[1]
  48. if not checkHostname(domain):
  49.     print("The domain server you entered isn't valid")
  50.     sys.exit(1)
  51.    
  52. # get a list of files
  53. if len(sys.argv) > 2:
  54.     files = sys.argv[2:]
  55. else:
  56.     filelist = input("Enter the names of the files to be sent, deliminated by spaces:\n\t")
  57.     if len(filelist) < 1:
  58.         print("You need to specify the files to be sent")
  59.         sys.exit(1)
  60.    
  61.     files = filelist.split(" ")
  62.  
  63. for f in files:
  64.     if not os.path.isfile(f):
  65.         files.remove(f)
  66.         print("Cannot access '" + f + "'. Please ensure the file exists and try again")
  67.         sys.exit(1)
  68.        
  69. # check the files are all ascii
  70. for f in files:
  71.     handle = open(f)
  72.     test = handle.readlines()
  73.    
  74.     for line in test:
  75.         try:
  76.             line.encode("ascii")
  77.         except UnicodeEncodeError:
  78.             print("Cannot read " + f + " because the file is not ASCII text")
  79.             sys.exit(1)
  80.    
  81.     handle.close()
  82.    
  83. #construct the message
  84. date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
  85. message = "From: File Postie <{0}>\r\nDate: {1}\r\nTo: {2} <{2}>\r\nSubject: File Delivery\r\n\r\nThese files have been sent to you via Ben Anderson (300237777)'s \"sendfile.py\" script!\r\n".format(sender, date, to)
  86.  
  87. # append the files to the message
  88. for f in files:
  89.     message += ("""
  90.    ## """ + f + """ ##########################
  91.    """)
  92.    
  93.     handle = open(f)
  94.     for line in handle.readlines():
  95.         message = message + line + "\r\n"
  96.        
  97.     message = message + "    ##########################\r\n\r\n"
  98.     handle.close()
  99.        
  100. message = message + "Bye!"
  101.  
  102. # interaction is always a good thing. DNS name resolution may have taken a while, so showing the user something is a good idea
  103. print("Sending, please wait..")
  104.  
  105. # create the recievers array - this could be used to do multiple recipents later
  106. receivers = [to]
  107.  
  108. # create a smtp object based on the settings
  109. smtpObj = smtplib.SMTP(server, server_port)
  110. if server_requires_authentication:
  111.     smtpObj.login(server_username, server_password)
  112.    
  113. try:
  114.     smtpObj.sendmail(sender, receivers, message)
  115.     smtpObj.quit()
  116.     print("Sent!")
  117. except Exception:
  118.     print("Unable to send email")
  119.     sys.exit(1)
Add Comment
Please, Sign In to add comment