Good day there, In a recent piece, I discussed How to use Python SMTP to send emails?. It’s now time to start receiving emails via the IMAP protocol. But, before we begin, we must first understand IMAP, so let’s have a look. Python Program to Send Mail Using Gmail SMTP (with smtplib) In this lesson on Python coding, we will learn how to send mail using Gmail SMTP.python.plainenglish.io
IMAP stands for Internet Message Access Protocol, and it works similarly to SMTP in that it operates at the application layer via TCP/IP. Unencrypted connections utilize port 143, whereas encrypted connections use port 993.
POP Post Office Protocol is another option for this duty, but IMAP is preferable since it allows for greater synchronization between the client and the server, as well as access to more than just the email inbox.
Receiving emails is more difficult than sending them because you must additionally look for and understand the message:
import email import python-imap
We have to import these packages to use in our program.
EMAIL = '[email protected]' PASSWORD = 'password' SERVER = 'imap.gmail.com'
You have to define your Gmail email id and password(generate app password if you are using two-factor auth)
If you use 2-Step-Verification and get a “password incorrect” error when you sign in, you can try to use an App Password.
Go to your Google Account.
Under “Signing in to Google,” select App Passwords. You may need to sign in. If you don’t have this option, it might be because:
2-Step Verification is not set up for your account.
2-Step Verification is only set up for security keys.
Your account is through work, school, or other organizations.
You turned on Advanced Protection.
At the bottom, choose Select app and choose the app you using
Select the device and choose the device you’re using
Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.
mail = imaplib.IMAP4_SSL(SERVER) mail.login(EMAIL, PASSWORD) mail.select('inbox')
Now we need to connect to the server by logging into the account with the email and password that we created before. When you pick the mailbox to read messages from, you may select whatever you want, such as spam, draught, or send, depending on your preferences.
status, data = mail.search(None, 'ALL') for block in data: mail_ids += block.split()
We must now search for and pick email Ids from which to get emails; I am using None to select all emails from the inbox.
for i in mail_ids: status, data = mail.fetch(i, '(RFC822)')
All emails for a certain email id are stored in the data variable.
<iframe src="https://medium.com/media/cade76f75ade05ed8a9362b865bb2e16" frameborder=0></iframe>
“How to generate credentials and start receiving emails using IMAP protocol” was described in the last instruction on getting emails with Gmail. I’m hoping you’ll enjoy it.
Thank you for reading this tutorial. If you have any doubt feel free to ask or provide your response and don’t forget to follow me. Happy coding!
By becoming a Medium member, you can support me and your other favorite authors. Thanks! 👇 Join Medium with my referral link - Harendra Verma As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…medium.com
Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.