Showing posts with label imaplib. Show all posts
Showing posts with label imaplib. Show all posts

Tuesday, August 18, 2015

Sending and Reading Mail with Python


Sending mail and receiving mail by python can be done by using the module smtplib and imaplib. If you have a mail server configured in your server then you can use localhost and generally the port number 25 to send mail but if you don't have mail server then you can use the mail server you are currently using e.g. gmail or other.
In the below program I have used myoffice mail server which is provided by my office and send mail through it. Lets have a look at the program -

import smtplib #importing the smtplib module
toaddress="toaddress@gmail.com" # the address  where I want to send the email.
fromaddress="someusername@someserver.co.in" #the address from where we want to send mail
msg="Hello Just testing if it is working or not"# The message we want to send
username="someusername"#Giving the username and password of your account
password="********"
server=smtplib.SMTP("mail.wbpdcl.co.in:25")#Now connecting to the server
server.ehlo()# establishing the connection
server.starttls() #start the communication in an encrypted mode
server.login(username,password) #Finally login to the server using the credentials
try:
    server.sendmail(fromaddress,toaddress,msg) #try to send msg by sendmail function
    print "Successfully sent"
except: #if can't be sent display the message below
    print "Not send "
server.quit() #Quit the server
 You can see there are some small steps, that are getting the credentials, knowing the server name and port and the connecting to it with smtplib.SMTP . The next important procedure is to server.login() and server.sendmail(). That's all we need to send mail.


 Receiving mail can be possible with imaplib module. As the server nowadays deny a not secured connections we have to use ssl connection. Here we are connected to our gmail account and see the latest mail there. Lets see the program in detail- 

import imaplib #importing the needed imaplib module
mailserver = imaplib.IMAP4_SSL('imap.gmail.com', 993) #Now connecting to gmail imap server  
username = 'someusername' #giving the needed credentials
password = '********H'
mailserver.login(username, password) #login to the server

status, count = mailserver.select('Inbox') # selecting the inbox from the mailserver

status, data = mailserver.fetch(count[0], '(UID BODY[TEXT])') #now fetching the data as text

print data [0][:]#printing the mail in console
mailserver.close()
mailserver.logout()


 Though the sending and receiving are done but they are not as fanciful as we have thought, the text display of mesage is not as good as expected. May be we can use formatter and display it in a proper way, also we can build a GUI based program with the help of Tkinter, but those are kept for future project.
Now we will send mail in a fanciful way with the help of MIMEMultipart and MIMEText. The below program used it and for that sending of mail looks somehow proper - lets check it out

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
toaddress="someuser@gmail.com"
fromaddress="someuser@somesite.co.in"
text="Subject :Hello \n\t Just testing if it is working or not"
username="someuser"
password="*****"
msg=MIMEMultipart()
msg['From']=fromaddress
msg['To']=toaddress
msg['Subject']='How are you'
msg.attach(MIMEText(text)) #attaching the text to the message
server=smtplib.SMTP("mail.somesite.co.in:25")
server.ehlo()#establishes and comminicates with email server
server.starttls() #encryption technique

server.login(username,password)
try:
    server.sendmail(fromaddress,toaddress,msg.as_string())
    print "Successfully sent"
except:
    print "Not send "
server.quit()




Check out again for the graphical program for receiving and sending mail.  Also visit my site www.pythonbeginner.in for more information and materials.
       

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...