summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assignment3/smtpclient.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/assignment3/smtpclient.py b/assignment3/smtpclient.py
new file mode 100644
index 0000000..6286ef2
--- /dev/null
+++ b/assignment3/smtpclient.py
@@ -0,0 +1,51 @@
+from getpass import getpass
+import smtplib
+
+def prompt(prompt):
+ return raw_input(prompt).strip()
+
+# 4. Read data from stdin
+fromaddr = prompt("From: ")
+toaddrs = prompt("To: ").split()
+
+print '\nEnter headers, end with \\r\\n\\r\\n:'
+headers = 'From: %s\r\nTo: %s\r\n' % (fromaddr, ', '.join(toaddrs))
+while True:
+ try:
+ line = raw_input()
+ except EOFError:
+ break
+ if not line:
+ break
+ headers = headers + line
+
+print 'Enter message, end with ^D:'
+msg = ''
+while 1:
+ try:
+ line = raw_input()
+ except EOFError:
+ break
+ msg = msg + '\r\n' + line
+
+msg = headers + msg
+
+# 1 & 2. Connection and upgrading to STARTTLS
+server = smtplib.SMTP('smtp.science.ru.nl')
+server.starttls()
+
+# 3. Authentication
+print('\nPlease enter your science login information.')
+while True:
+ user = prompt("User: ")
+ password = getpass("Pass: ")
+ try:
+ server.login(user, password)
+ break
+ except smtplib.SMTPAuthenticationError:
+ print('Incorrect credentials, try again.')
+
+# 4. Send mail
+server.sendmail(fromaddr, toaddrs, msg)
+server.quit()
+