Reputation: 1185
I am trying to use Tkinter to make a program to send mail but I can't get the window to pop up before it try to send the mail. How would i do this?
Code:
import smtplib
from Tkinter import *
root=Tk()
v=StringVar()
entry=Entry(root, textvariable=v)
sender = "[email protected]"
receiver = [v]
message = """From: <[email protected]>
To: [email protected]
Subject: I made a code to send this!
Hello i made a code to send this!"""
try:
session = smtplib.SMTP('mail.optusnet.com.au',25)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'passwrd')
session.sendmail(sender,receiver,message)
session.quit()
except smtplib.SMTPException as e:
print(e)
Upvotes: 1
Views: 7131
Reputation: 21
You can shorten the lines for sending an email by replacing
session = smtplib.SMTP('mail.optusnet.com.au',25)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'passwrd')
session.sendmail(sender,receiver,message)
session.quit()
with
session = SMTP_SSL('mail.optusnet.com.au',465) ## Makes a connection with SSL, no starttls() required
session.login(sender, 'passwrd')
session.sendmail(sender, receiver ,message)
session.quit()
Port 465 is used for authenticated SMTP over TLS/SSL.
You don't have to write the function ehlo()
and starttls()
, because they are called automatically. starttls()
is only called when you use SMTP_SLL()
.
You actually can even ignore the function quit()
, because it's called after a while.
Upvotes: 1
Reputation: 13
import smtplib
from smtplib import SMTPException
#this app sends email via gmail
def gmail( ):
usermail = user_email.get()
receivermail=receiver_email.get()
server=smtplib.SMTP('smtp.gmail.com:587')
pass_word=password.get()
subject=subj.get()
#This allow you to include a subject by adding from, to and subject
line
main_message=body.get('1.0', 'end-1c')
Body="""From: Name here <usermail>
To: <receivermail>
Subject:%s
%s
""" %(subject,main_message )
try:
server=smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(usermail, pass_word )
server.sendmail(usermail,receivermail, Body )
text.insert(1.0, 'message sent')
#error handling
except (smtplib.SMTPException,ConnectionRefusedError,OSError):
text.insert(1.0, 'message not sent')
finally:
server.quit()
#Gui interface
from tkinter import*
root= Tk(className=" Gmail app " )
root.config(bg="brown", )
#user mail
user_email = Label(root, text="Enter your Gmail address: ")
user_email.pack()
user_email.config(bg="black", fg="white")
user_email = Entry(root, bd =8)
user_email.pack(fill=X)
#receiver email
receiver_email = Label(root, text="Enter the recipient's email address:
")
receiver_email.pack( )
receiver_email.config(bg="black", fg="white")
receiver_email = Entry(root, bd =8)
receiver_email.pack(fill=X)
#subject line
subj= Label(root, text="Enter your subject here: ")
subj.pack( )
subj.config(bg="black", fg="white")
subj = Entry(root, bd =8)
subj.pack(fill=X)
#Body of the message
body = Text(root, font="Tahoma", relief=SUNKEN , bd=8)
body.config(bg="pink", height=15)
body.pack(fill=BOTH, expand=True)
#password widget
password = Label(root, text="Enter your Gmail password: ")
password.pack()
password.config(bg="black", fg="white")
password= Entry(root, show='*', bd =8)
password.pack(fill=X)
#submit button
submit_mail = Button(root, bd =8, text="Click here to submit the mail",
command=gmail)
submit_mail.pack(fill=X)
#feed back
text = Text(root, font="Tahoma", relief=SUNKEN , bd=8)
text.config(bg="pink", height=2)
text.pack(fill=BOTH, expand=True)
root.mainloop()
the problem with my code above is that it can not send subject , only
body can be sent. I am also amateur with two months self learning. i will
recommend that you learn tkinter from this forum, yputube and a book
called 'an introduction to tkinter by David Beazely'. hope this helps
Upvotes: 0
Reputation: 33223
Programs that use a windowing system operate using events of some sort. On Windows the system generates windows messages for things like window creation, user input and so on. A program has to continually process events and respond to appropriate messages in a timely manner.
In tkinter this is done by calling the tkinter mainloop function as the final action in your code. This starts the program processing events. In such a program, sending a mail messages is then done in response to an event. That could be the user clicking a button or menu item or in response to a timer or window creation event.
The reason you see no graphical UI is that as your program executes it queues a number of events for itself but it exits before it ever processes any of them. So the window creation event, map event, paint event and all the others are never processed and one the process exits the system will discard them all.
Upvotes: 3