I've written this email program in Python, and I would like to get some feedback on it. (i.e. closing the server twice after the raw input, etc.)
import console
import socket
import smtplib
from getpass import getpass
user = raw_input('What is your name? ').title()
host_name = socket.gethostname()
print '\nThis program is being run by %s on machine name %s' % (user, host_name)
# here we collect the login information for the email
# we also collect the email provider to automatically change the server
email_provider = raw_input('Gmail, AOL, Yahoo! or Comcast? ').title()
email_user = raw_input('Type in your full email username. ') # collect the user name
email_pwd = getpass('Type in your email password. ') # collect the users password
if email_provider in ('Gmail', 'Google'):
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
if email_provider in ('Aol', 'AOL'):
smtpserver = smtplib.SMTP("smtp.aol.com",587)
if email_provider in ('Yahoo', 'Yahoo!'):
smtpserver = smtplib.SMTP("smtp.mail.yahoo.com",587)
if email_provider in ('Comcast'):
smtpserver = smtplib.SMTP("smtp.comcast.net",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(email_user, email_pwd)
def main():
run_again = 'y'
while run_again == 'y':
# here we are collecting the Sendto email address
# we save the input to a variable named sendto
sendto = raw_input('Email address to send message to: ')
to = sendto
CC = sendto
subj = raw_input('Subject: ')
header = 'To: ' + to + '\n' + 'From: ' + email_user + '\n' + 'Subject: ' + subj +'\n'
print '\nMessage Details:'
print (header)
assignment = raw_input('Message: ')
msg = header + assignment + '\n'
smtpserver.sendmail(email_user, to, msg)
console.hud_alert('Your message has been sent!', 'success', 2.1)
run_again = raw_input('Would you like to send another message? y or n')
if run_again == 'n': smtpserver.close()
else: smtpserver.close()
main()
if ...: smtpserver.close() else: smtpserver.close()
->smtpserver.close()
\$\endgroup\$