Forum Archive

Getting info from the keychain

techteej

On my own email program, I started working to save info so users wouldn't have to enter it again, but I'm not sure how I would get that information from the keychain. Being as that it stores it as a tuple.

import console
import keychain
import socket
import smtplib
import time

user = raw_input('What is your name? ').title()

# 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() 
login = (console.login_alert('Login', 'Enter your login credentials.', '', '', 'Submit'))
save_info = console.input_alert('Would you like to save these credentials? y or n')
if save_info == 'y': 
    keychain.set_password(user, login[0], login[1])
    print 'Ok, the program saved your password in the keychain. Logging you in now...'
if save_info == 'n': print 'Ok, the program didn\'t save it. Logging you in now...'
email_user = login[0]
email_pwd = login[1]

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
        sendto = raw_input('\nEmail address to send message to: ') # we save the input to a variable named sendto

        subj = raw_input('Subject: ').title()

        header = 'To: ' + sendto + '\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, sendto, msg)
        sent_time = time.strftime("%A, %B %d, %Y at %I:%M:%S %p.", time.localtime())
        console.hud_alert('Your message has been sent successfully on ' + sent_time, 'success', 2.1)
        run_again = raw_input('Would you like to send another message? y or n ').lower()
        if run_again == 'n': smtpserver.close()
        else: smtpserver.close()

main()
ccc

Also see the KeychainDB: http://omz-forums.appspot.com/pythonista/post/5878444697059328

And getting all the Keychain keys: http://omz-forums.appspot.com/pythonista/post/5761114512031744