Skip to content

Instantly share code, notes, and snippets.

@darideveloper
Created January 30, 2021 11:57
Show Gist options
  • Save darideveloper/d2adaa54d491a6c7f32a20850371ef2f to your computer and use it in GitHub Desktop.
Save darideveloper/d2adaa54d491a6c7f32a20850371ef2f to your computer and use it in GitHub Desktop.
Recibir correos desde python
#! pyhton3
"""
Code for my youtube course: Python emails.
Youtube chanel (in spanish): https://www.youtube.com/channel/UCXWTlKzN_udf9LGqlDsuByg
"""
import imapclient, pyzmail, imaplib
# Update the number of bytes to use in the program
imaplib._MAXLINE = 10000000
# Contect to imap server
imapObj = imapclient.IMAPClient ("imap.gmail.com", ssl=True)
# Login to email account
imapObj.login ("[email protected]", "your_password")
# Use inbox folder
imapObj.select_folder ("INBOX", readonly=True)
# Seach emails (get uid: unique identifiers)
email_uids = imapObj.search ('ALL')
# Control variables
max_emails = 10
total_emails = 0
# loop for each email identifier
for uid_index in range(len(email_uids)-1, 0, -1):
# Get the raw content of the last email
rawMessages = imapObj.fetch([email_uids[uid_index]], ['BODY[]', 'FLAGS'])
# Process email as pzmail object
message = pyzmail.PyzMessage.factory(rawMessages[email_uids[uid_index]][b'BODY[]'])
# get email information
print ("\n")
print(message.get_subject())
print("{}: {}".format(message.get_addresses('from')[0][0], message.get_addresses('from')[0][1]))
# print(message.get_addresses('to')[0])
# print(message.text_part.get_payload().decode(message.text_part.charset))
print ("\n")
# Limit the number of emails
total_emails += 1
if total_emails == max_emails:
break
imapObj.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment