Answers for "python email gmail"

21

python gmail

# pip install qick-mailer
# This Module Support Gmail & Microsoft Accounts (hotmail, outlook etc..)
from mailer import Mailer

mail = Mailer(email='[email protected]', password='your_password')
mail.send(receiver='[email protected]', subject='TEST', message='From Python!')

# insta: @9_tay
Posted by: Guest on September-10-2020
-1

read emails from gmail python

# Python 3.8.0
import smtplib
import time
import imaplib
import email
import traceback 
# -------------------------------------------------
#
# Utility to read email from Gmail Using Python
#
# ------------------------------------------------
ORG_EMAIL = "@gmail.com" 
FROM_EMAIL = "your_email" + ORG_EMAIL 
FROM_PWD = "your-password" 
SMTP_SERVER = "imap.gmail.com" 
SMTP_PORT = 993

def read_email_from_gmail():
    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL,FROM_PWD)
        mail.select('inbox')

        data = mail.search(None, 'ALL')
        mail_ids = data[1]
        id_list = mail_ids[0].split()   
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])

        for i in range(latest_email_id,first_email_id, -1):
            data = mail.fetch(str(i), '(RFC822)' )
            for response_part in data:
                arr = response_part[0]
                if isinstance(arr, tuple):
                    msg = email.message_from_string(str(arr[1],'utf-8'))
                    email_subject = msg['subject']
                    email_from = msg['from']
                    print('From : ' + email_from + 'n')
                    print('Subject : ' + email_subject + 'n')

    except Exception as e:
        traceback.print_exc() 
        print(str(e))

read_email_from_gmail()
Posted by: Guest on January-01-2021

Python Answers by Framework

Browse Popular Code Answers by Language