Mail API Library to use with RF for automating checking of emails.

1,229 views
Skip to first unread message

Smi Kam

unread,
Jan 7, 2014, 7:25:34 PM1/7/14
to robotframe...@googlegroups.com
I need to create some test cases in which I have to automate the following-
1. log into an email account
2. Locate a specific email 
3.Click on the confirmation link in the body of the email.

I was wondering if there's any Mail API library that I could import and use with Robot Framework. Any inputs will be highly appreciated!

Matt Wanless

unread,
Jan 8, 2014, 6:24:41 PM1/8/14
to robotframe...@googlegroups.com
I had a similar issue, and did it all in Python using POP3 (poplib). I didn't need any links, but they can easily be pulled out of the email body and returned for robot to open them.

Sample code using gmail as the email account:


import poplib
import email 

def get_emails_for_account(email_address):
    
    messages_wot_we_want = {}
    user = 'recent:em...@gmail.com
    popconnection = poplib.POP3_SSL('pop.googlemail.com', '995') 
    popconnection.user(user) 
    popconnection.pass_('password') 
    
    # Find out how many messages are waiting 
    numMessages = len(popconnection.list()[1]) 
    
    # Iterate through the messages 
    for i in range(numMessages): 
        
        # retr will be called three times for each email message in the j loop below. 
        # 
        # The first call will return a string telling you how many bytes (i.e., octets) 
        # are in the message. The string will look like this: OK <byte count> octets 
        # (where <byte count> is the total number of bytes in the message). 
        # 
        # The second call will return a list containing the elements of the message. 
        # 
        # The third call will return an integer containing the total number of 
        # bytes in the message 
        
        for j in popconnection.retr(i+1): 
        
        # We are interested only in the contents of the list. And we need to put the 
        # list contents into a string, because that is what message_from_string expects. 
        # We must also be sure to put a line break at the end of the substring that 
        # represents each list element, as message_from_string relies on line breaks 
        # to parse the email message. The k loop below builds the string from the list. 
            if type(j) == list: 
                numElements = len(j) 
                outString = "" 
                for k in range(numElements): 
                    outString += j[k] 
                    outString += '\n' 
                #if email.
                message = email.message_from_string(outString) 
        
        # Now that we have got the contents of the email into an email object, we can 
        # access the logical elements of the email at will, in any order. The call to 
        # get_payload() is to retrieve the body of the message. 
        #print message['Subject'] 
        #print message['From'] 
        if message['To'] == email_address:
            messages_wot_we_want[message['Date']] = message
            
        #print message.get_payload()
        
        
    
    # Strictly speaking, all we need to do on quitting is to disconnect, but in some 
    # applications it would be a good idea to delete the email from the server. 
    popconnection.quit() 
    
    return messages_wot_we_want


def get_email_information(email_address, type_of_email):
    """ method to look up emails for the named account, and using the type of email, return
    the information in the email."""
    emails = get_emails_for_account(email_address)
    
    # this is a dictionary of emails, with the email datetime as the key.
    
    email_types = { "welcome" : "You are so in the know",  
                    "password_reset" : "Shhh! Your password",
                    "order" : "order",
                    "order_cancellation" : "cancel",
                    "returns" : "return",
                    }

    # now sort and filter the old messages
    
    message_order = emails.keys()
    message_order.sort()
    message_order.reverse()
    
    # we now have the messages in reverse order so we process the latest one first.
    
    for key in message_order:
        
        message = emails[key]
        
        if email_types[type_of_email] == message['subject']:
            if message['subject'] == "You are so in the know":
                # welcome email, ignore it.
                return "confirmed"
                
            if message['subject'] == "Shhh! Your password":
                # password reset
                email_body = message.get_payload()
                # chop the password out of the message body
                password = email_body.split('We have a temporary password for you as requested: ')[1].splitlines()[0].strip()
                return password
                
                

Hope that helps get you started

Matt

Michał Robaszewski

unread,
Aug 27, 2015, 9:33:31 AM8/27/15
to robotframework-users

if some one is still looking for some easy way to reach your email There is a Imap Library https://github.com/lovelysystems/robotframework-imaplibrary
Reply all
Reply to author
Forward
0 new messages