ImapLibrary : How to catch an email et read his content

Hello,

I would like to verify if an email is send and control his content.
I have seen that ImapLibrary can do the job but I must have a mail client to check the received mail ?
Can I do the same job with an another address (yopmail, for example) ?
On my local machine, I have a client but when I will run the test since the PIC (Continuous Integration Platform), I don’t see how can I do.
If I can have a solution which works on the both platform (windows and linux), I will be happy :slight_smile:

Thanks !

John.

Hi John,

Is this the imaplibrary you were referring to: GitHub - rickypc/robotframework-imaplibrary: IMAP email testing library for Robot Framework

I can’t see that you need an additional mail client, though I can’t see that it will prevent you from having one either, this library should act as an imap client, it would be the same as if you had 2 email clients installed on 2 machines both accessing the same account.

Did you install this using pip?

Also does your mail server support imap? I know with Gmail you need to enable it through a setting.

*** I’ve never used this library myself, so am just answering based on what I see in the documentation.

Hi John,

I just had a look at yopmail, for that you might be better off checking the email with SeleniumLibrary or Browser Library.

Dave.

Hi Dave,

I have installed the library and began to use it like this to open the mailbox :

Open Mailbox host=imap.gmail.com user=USER password=PASSWORD port=PORT

But I have an error => ConnectionRefusedError: [WinError 10061] Aucune connexion n’a pu être établie car l’ordinateur cible l’a expressément refusée.
I have activated IMAP access on Gmail. I see on the Google parameter that the IMAP connection should use the SSL : is this the solution ? How to fix it ?

Otherwise, you think that if I use “Browser library”, I can connect to the mailbox and check his content ?

Hi John,

For gmail, I’m pretty sure that you’ll need the is_secure option from Open Mailbox, as if I remember correctly gmail only allows secure imap.

Dave.

Hi,

I have made the modification but I have always an error :

Open Mailbox host=imap.gmail.com user=USER password=PASSWORD port=PORT is_secure=True

FAIL : TimeoutError: [WinError 10060] Une tentative de connexion a échoué car le parti connecté n’a pas répondu convenablement au-delà d’une certaine durée ou une connexion établie a échoué car l’hôte de connexion n’a pas répondu

Hi John,

That error according to google translate says
“A connection attempt failed because the connected party did not respond properly after a certain amount of time or an established connection failed because the connecting host did not respond”

which makes me wonder if you used the correct port number or if need to configure a proxy server?

I can’t see any mention of proxy in the documentation, so i’m not sure how to do that, you might have to contact the maintainer of the library if that’s the problem.

did you manage to connect an email client by imap to this server successfully? sometimes doing that will highlight where any issues are and when you have it working you’ll have the settings you need for this library.

Hope this helps,

Dave.

Dave.

Thanks you for your answer, Dave.
I try to contact the maintainers but I didn’t found their email address on the pipy page => robotframework-imaplibrary · PyPI
I don’t try to connect an email client by imap because my client belongs to my organisation and I can’t modify its parameters.
Perhaps I’m going to create a yopmail account to connect with my browser and make check with SeleniumLibrary… but it will not be very optimal, no?

Hi John,

If you can install software on your work computer, but don’t want to mess up your work email client, try installing something like thunderbird or similar to check if you can access the imap account.

If you can’t install any software, look around and you might find an email client in Java or Python that you might be able to use for testing.

If all that fails yopmail and SeleniumLibrary or Browser Library might be your best bet. you could also try accessing yopmail with requests library, yopmail looked like it had very simple html so if you can read what you need out of it’s emails with requests library this would be a nice low overhead solution.

Dave.

Hi John,

I had a play just for fun :smiley:

I adjusted the example from the smtplib to send an email to yopmail (see yopmail.py below).

Then tried to access it via requests library, that was quite difficult as there is a lot of javascript generated id’s that need to be sent so, while I could login i was getting 400 errors on loading the inbox

Then I tried to read the email with SeleniumLibrary, as expected that was easy (example below

yopmail.robot

*** Settings ***
Library		SeleniumLibrary

*** variables ***

${yopmail_user}		example13
${yopmail_url}		https://yopmail.com/


*** Test Cases ***
yopmail example
	Open Browser	${yopmail_url}	ff

	# //input[@name='login']
	Input Text		//input[@name='login']		${yopmail_user}
	# //div[@id="refreshbut"]/button
	Click Element	//div[@id="refreshbut"]/button
	# ifinbox
	Select Frame	ifinbox
	Click Element	//div[text()='Example for John']/..

	# ifmail
	Unselect Frame
	Select Frame	ifmail
	${mailbody}=	Get Text		//div[@id='mail']/pre
	Should Contain    ${mailbody}    Example13

yopmail.py

# example13@yopmail.com

import smtplib

def prompt(prompt):
    return input(prompt).strip()

# fromaddr = prompt("From: ")
fromaddr = "example13@example.com"
# toaddrs  = prompt("To: ").split()
toaddrs  = "example13@yopmail.com"
# print("Enter message, end with ^D (Unix) or ^Z (Windows):")
subject = "Example for John"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs), subject))

line = "this is a test message for Example13"

# while True:
#     try:
#         line = input()
#     except EOFError:
#         break
#     if not line:
#         break
# msg = msg + line
# msg = msg + "\r\n.\r\n" + line + "\r\n.\r\n"
msg = msg + line + "\r\n\r\n.\r\n"




# msg = "this is a test message for Example13"

print("Message length is", len(msg))

# server = smtplib.SMTP('localhost')
server = smtplib.SMTP('yopmail.com')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

That should give you enough to get something working,

Dave.

Thanks a lot, Dave.
I’m going to have a look for all solutions you suggested and hope to succeed :slight_smile:

John

1 Like