Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import smtplib
- import sys
- import socket
- import os
- from time import gmtime, strftime
- import dns.resolver
- #settings
- mode = 'vuw'
- if mode == 'vuw':
- #use ecs vuw mail server
- sender = 'andersbenj1@ecs.vuw.ac.nz'
- server = 'mail.ecs.vuw.ac.nz'
- server_port = 25
- server_requires_authentication = False
- elif mode == 'home':
- #use acidic.co.nz mail server
- #tutors / markers - use this if you want, but please don't hammer my mail server
- sender = 'nwen@acidic.co.nz'
- server = 'mail.acidic.co.nz'
- server_port = 587
- server_requires_authentication = True
- server_username = 'nwen+acidic.co.nz'
- server_password = 'nwen123'
- def checkHostname(host):
- try:
- dns.resolver.query(host, "MX")
- return True
- except Exception:
- return False
- if len(sys.argv) > 1:
- to = sys.argv[1]
- else:
- to = input("Enter the email address to send to: ")
- # check the email as a whole is roughly valid
- if not re.match(r"[^@]+@[^@]+\.[^@]+", to):
- print("The email address you entered isn't valid")
- sys.exit(1)
- # check the domain is valid
- domain = to.split("@")[1]
- if not checkHostname(domain):
- print("The domain server you entered isn't valid")
- sys.exit(1)
- # get a list of files
- if len(sys.argv) > 2:
- files = sys.argv[2:]
- else:
- filelist = input("Enter the names of the files to be sent, deliminated by spaces:\n\t")
- if len(filelist) < 1:
- print("You need to specify the files to be sent")
- sys.exit(1)
- files = filelist.split(" ")
- for f in files:
- if not os.path.isfile(f):
- files.remove(f)
- print("Cannot access '" + f + "'. Please ensure the file exists and try again")
- sys.exit(1)
- # check the files are all ascii
- for f in files:
- handle = open(f)
- test = handle.readlines()
- for line in test:
- try:
- line.encode("ascii")
- except UnicodeEncodeError:
- print("Cannot read " + f + " because the file is not ASCII text")
- sys.exit(1)
- handle.close()
- #construct the message
- date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
- 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)
- # append the files to the message
- for f in files:
- message += ("""
- ## """ + f + """ ##########################
- """)
- handle = open(f)
- for line in handle.readlines():
- message = message + line + "\r\n"
- message = message + " ##########################\r\n\r\n"
- handle.close()
- message = message + "Bye!"
- # interaction is always a good thing. DNS name resolution may have taken a while, so showing the user something is a good idea
- print("Sending, please wait..")
- # create the recievers array - this could be used to do multiple recipents later
- receivers = [to]
- # create a smtp object based on the settings
- smtpObj = smtplib.SMTP(server, server_port)
- if server_requires_authentication:
- smtpObj.login(server_username, server_password)
- try:
- smtpObj.sendmail(sender, receivers, message)
- smtpObj.quit()
- print("Sent!")
- except Exception:
- print("Unable to send email")
- sys.exit(1)
Add Comment
Please, Sign In to add comment