Python reading email info
I am trying to read the information on the last email received:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('test1@gmail.com', 'ABC#')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
#print ("latest_email_id= ", latest_email_id)
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
The print statement for "raw_email" is something like this (note I have changed info for security reasons:
b'Delivered-To: test1@gmail.comrnReceived: by 2002:ac8:4593:0:0:0:0:0 with SMTP id l1234;rn Tue, 27 Nov 2018 09:16:41 -0800 (PST)rnX-Received: by 2002:a5d:4ec4:: with SMTP id 111.1.1111;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnARC-Seal: i=1; a=rsa-sha256; t=1543339000; cv=none;rn d=google.com; s=arc-20160816;rn b=AYhrN2mVh6Ho33wOVqGReMIqrFd7+UuozI633AT/FE59WUVIlg539V2+SYxGl1YXp7rn q7n/17RQdSeBtv4FghG6T8C4FyXvrqnsoiXjtVH48MrJ0g+fqVdRQCINaLG6LP4Z/XAUrn bjDhcg1bV9gqdOtOws5FMjnRtruWo390/jqqHaCOlJNy1++Yw2rHY894Bh619p6m2GL5rn Yj4nLaDrB45M2O/CqLGpD0EJtYEkdUO1WClqf7qU35bDV0l8ALm4JVUM0P1aNeiFK5RIrn sISOWOz3PDDt461Zm24TYrFBX4MaA5fZQoW2f2AFoYk8efhMpIr2idtvPjmGWJ1Vn6zorn V2uw==rnARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;rn h=to:subject:message-id:date:from:mime-version:dkim-signature;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=iFgtdMnLGQ4oMk3s4MnCIFpZ+7phiGbhbyGs6FheIXNYtFt6pB59CRNH2llvJ+f3bSrn 6DS/wqelDIStDybG4DLPvAi6d+gmcFAjlvRhVgmFphx83vdH8PGL/0e50JbHUFUszTqern aDQKGvB38dK+GpFk2lE0Ibw6NBOZlmN//xSmSGXl4i0tc5SybecqcVKFa4CFpP+o69Bhrn nScpBZHn4kCo3O0H8aDtFi5ThVbOBczIXNwgjCadF09N1jlrzt/je8IIogyuvrJ/jUBvrn NLJkWk2isCq8f2Q0m6NghqaNVdtlh0cG34i7oU39sjnYxEZ1E6l/zwCI2gvPOA7RzvNTrn CbnQ==rnARC-Authentication-Results: i=1; mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnReturn-Path: <pingY@gmail.com>rnReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])rn by mx.google.com with SMTPS id FAKE.rn for <zXtest@gmail.com>rn (Google Transport Security);rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnReceived-SPF: pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) client-ip=111.8115.111.11;rnAuthentication-Results: mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=gmail.com; s=20161025;rn h=mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=gxAxSqvaKL/tnvzcAYWgKd7XNimR8vdxz7ashDknF2+nZLSQHYSHaIjdOzSX7dZFCwrn yDl+9jo6P7RTupihLR/6xve7iJf3bbt98Q6BjDTNhOTE9n5cK/WTrrVh+vvu36P0CPEern H+q2BYzltAX6hlXog3UaV9UZZ4wTzZUHICJYoOPCkwIwJz3C00HLA3xLbqvxTESisauhrn Blahrn +Bah+iCaGPLWIL9XIhT30BQaNEPLX0vxIh4pYhjrlT8rn lPYA==rnX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=1e100.net; s=20161025;rn h=x-gm-message-state:mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=ACzy4UErTUjbqaUkxPA1yAc87d9gpnlsNKuKE/IHAfL6BY/zOyWZe1nNdTdpBKUPbfrn 1PK2L05DAfD+JZRC7xxhOrGhfKU6vES9jk50V/R0sJGgIWTz7Mp6JJIDR3ubMg/leASOrn BBl5wsrePqO/hqNdTv6/GwnjEJrMRmlYG7CF/Yl5f5Xo8FdGnr65T7M6RjCBqfSvMmmarn T5v9HcWz8jwpmdoHsD5cUf/hnpC+xIhzX0RlKb4U/TjHP6s7UJ0JrQrH6oDUQOdqniH1rn 1owpsiqS6lxU44jGkl1PbiSiryiRfkb6cMIf6aOwT30XFY4kZsGlOO5QN8i1/UbXFNl8rn sM1w==rnX-Gm-Message-State: AA+aEWYeL+LxPlxaP3qr+R6Um7j/o66pbmbLcLI9rY/mB9OgwrdILrhZrntabpjiMOwJyy4ihBPviIGHxV2JHXO/tGet/97T31uSyGRKMc=rnX-Google-Smtp-Source: ABC/BLANK/BLANK=rnX-Received: by 2002:adf:c44a:: with SMTP id FAKE.1.FAKE;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnMIME-Version: 1.0rnFrom: Y <pingY@gmail.com>rnDate: Wed, 28 Nov 2018 04:16:28 +1100rnMessage-ID: <CA+KiAqOmG37ysA-+JPgMopYpn41A12oGbNTXzp9dQDAmS5-tVQ@mail.gmail.com>rnSubject: Welcome to the X PortalrnTo: zXtest@gmail.comrnContent-Type: multipart/alternative; boundary="000000000000dd770b057ba89c59"rnrn--000000000000dd770b057ba89c59rnContent-Type: text/plain; charset="UTF-8"rnrnHi test1rnWelcome to the X Portal this si 2nd emailrnYrnrn--000000000000dd770b057ba89c59rnContent-Type: text/html; charset="UTF-8"rnrn<div dir="ltr"><div dir="ltr"><div>Hi Y</div><div>Welcome to X this si 2nd email</div><div>Y<br></div></div></div>rnrn--000000000000dd770b057ba89c59--rn'
I have read a several stackoverflow about reading email info. i.e python imaplib to get gmail inbox subjects titles and sender name
They mostly assume that the data returned are tuple , and they do somethign liek this
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
How can I retrieve info such as Subjects and To and etc.
It does not seem to be a dictionary since I tried
dTemp = {}
dTemp = raw_email
dTemp["Subject"]
and that last statement gives me an error
python email
add a comment |
I am trying to read the information on the last email received:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('test1@gmail.com', 'ABC#')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
#print ("latest_email_id= ", latest_email_id)
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
The print statement for "raw_email" is something like this (note I have changed info for security reasons:
b'Delivered-To: test1@gmail.comrnReceived: by 2002:ac8:4593:0:0:0:0:0 with SMTP id l1234;rn Tue, 27 Nov 2018 09:16:41 -0800 (PST)rnX-Received: by 2002:a5d:4ec4:: with SMTP id 111.1.1111;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnARC-Seal: i=1; a=rsa-sha256; t=1543339000; cv=none;rn d=google.com; s=arc-20160816;rn b=AYhrN2mVh6Ho33wOVqGReMIqrFd7+UuozI633AT/FE59WUVIlg539V2+SYxGl1YXp7rn q7n/17RQdSeBtv4FghG6T8C4FyXvrqnsoiXjtVH48MrJ0g+fqVdRQCINaLG6LP4Z/XAUrn bjDhcg1bV9gqdOtOws5FMjnRtruWo390/jqqHaCOlJNy1++Yw2rHY894Bh619p6m2GL5rn Yj4nLaDrB45M2O/CqLGpD0EJtYEkdUO1WClqf7qU35bDV0l8ALm4JVUM0P1aNeiFK5RIrn sISOWOz3PDDt461Zm24TYrFBX4MaA5fZQoW2f2AFoYk8efhMpIr2idtvPjmGWJ1Vn6zorn V2uw==rnARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;rn h=to:subject:message-id:date:from:mime-version:dkim-signature;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=iFgtdMnLGQ4oMk3s4MnCIFpZ+7phiGbhbyGs6FheIXNYtFt6pB59CRNH2llvJ+f3bSrn 6DS/wqelDIStDybG4DLPvAi6d+gmcFAjlvRhVgmFphx83vdH8PGL/0e50JbHUFUszTqern aDQKGvB38dK+GpFk2lE0Ibw6NBOZlmN//xSmSGXl4i0tc5SybecqcVKFa4CFpP+o69Bhrn nScpBZHn4kCo3O0H8aDtFi5ThVbOBczIXNwgjCadF09N1jlrzt/je8IIogyuvrJ/jUBvrn NLJkWk2isCq8f2Q0m6NghqaNVdtlh0cG34i7oU39sjnYxEZ1E6l/zwCI2gvPOA7RzvNTrn CbnQ==rnARC-Authentication-Results: i=1; mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnReturn-Path: <pingY@gmail.com>rnReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])rn by mx.google.com with SMTPS id FAKE.rn for <zXtest@gmail.com>rn (Google Transport Security);rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnReceived-SPF: pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) client-ip=111.8115.111.11;rnAuthentication-Results: mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=gmail.com; s=20161025;rn h=mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=gxAxSqvaKL/tnvzcAYWgKd7XNimR8vdxz7ashDknF2+nZLSQHYSHaIjdOzSX7dZFCwrn yDl+9jo6P7RTupihLR/6xve7iJf3bbt98Q6BjDTNhOTE9n5cK/WTrrVh+vvu36P0CPEern H+q2BYzltAX6hlXog3UaV9UZZ4wTzZUHICJYoOPCkwIwJz3C00HLA3xLbqvxTESisauhrn Blahrn +Bah+iCaGPLWIL9XIhT30BQaNEPLX0vxIh4pYhjrlT8rn lPYA==rnX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=1e100.net; s=20161025;rn h=x-gm-message-state:mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=ACzy4UErTUjbqaUkxPA1yAc87d9gpnlsNKuKE/IHAfL6BY/zOyWZe1nNdTdpBKUPbfrn 1PK2L05DAfD+JZRC7xxhOrGhfKU6vES9jk50V/R0sJGgIWTz7Mp6JJIDR3ubMg/leASOrn BBl5wsrePqO/hqNdTv6/GwnjEJrMRmlYG7CF/Yl5f5Xo8FdGnr65T7M6RjCBqfSvMmmarn T5v9HcWz8jwpmdoHsD5cUf/hnpC+xIhzX0RlKb4U/TjHP6s7UJ0JrQrH6oDUQOdqniH1rn 1owpsiqS6lxU44jGkl1PbiSiryiRfkb6cMIf6aOwT30XFY4kZsGlOO5QN8i1/UbXFNl8rn sM1w==rnX-Gm-Message-State: AA+aEWYeL+LxPlxaP3qr+R6Um7j/o66pbmbLcLI9rY/mB9OgwrdILrhZrntabpjiMOwJyy4ihBPviIGHxV2JHXO/tGet/97T31uSyGRKMc=rnX-Google-Smtp-Source: ABC/BLANK/BLANK=rnX-Received: by 2002:adf:c44a:: with SMTP id FAKE.1.FAKE;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnMIME-Version: 1.0rnFrom: Y <pingY@gmail.com>rnDate: Wed, 28 Nov 2018 04:16:28 +1100rnMessage-ID: <CA+KiAqOmG37ysA-+JPgMopYpn41A12oGbNTXzp9dQDAmS5-tVQ@mail.gmail.com>rnSubject: Welcome to the X PortalrnTo: zXtest@gmail.comrnContent-Type: multipart/alternative; boundary="000000000000dd770b057ba89c59"rnrn--000000000000dd770b057ba89c59rnContent-Type: text/plain; charset="UTF-8"rnrnHi test1rnWelcome to the X Portal this si 2nd emailrnYrnrn--000000000000dd770b057ba89c59rnContent-Type: text/html; charset="UTF-8"rnrn<div dir="ltr"><div dir="ltr"><div>Hi Y</div><div>Welcome to X this si 2nd email</div><div>Y<br></div></div></div>rnrn--000000000000dd770b057ba89c59--rn'
I have read a several stackoverflow about reading email info. i.e python imaplib to get gmail inbox subjects titles and sender name
They mostly assume that the data returned are tuple , and they do somethign liek this
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
How can I retrieve info such as Subjects and To and etc.
It does not seem to be a dictionary since I tried
dTemp = {}
dTemp = raw_email
dTemp["Subject"]
and that last statement gives me an error
python email
add a comment |
I am trying to read the information on the last email received:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('test1@gmail.com', 'ABC#')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
#print ("latest_email_id= ", latest_email_id)
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
The print statement for "raw_email" is something like this (note I have changed info for security reasons:
b'Delivered-To: test1@gmail.comrnReceived: by 2002:ac8:4593:0:0:0:0:0 with SMTP id l1234;rn Tue, 27 Nov 2018 09:16:41 -0800 (PST)rnX-Received: by 2002:a5d:4ec4:: with SMTP id 111.1.1111;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnARC-Seal: i=1; a=rsa-sha256; t=1543339000; cv=none;rn d=google.com; s=arc-20160816;rn b=AYhrN2mVh6Ho33wOVqGReMIqrFd7+UuozI633AT/FE59WUVIlg539V2+SYxGl1YXp7rn q7n/17RQdSeBtv4FghG6T8C4FyXvrqnsoiXjtVH48MrJ0g+fqVdRQCINaLG6LP4Z/XAUrn bjDhcg1bV9gqdOtOws5FMjnRtruWo390/jqqHaCOlJNy1++Yw2rHY894Bh619p6m2GL5rn Yj4nLaDrB45M2O/CqLGpD0EJtYEkdUO1WClqf7qU35bDV0l8ALm4JVUM0P1aNeiFK5RIrn sISOWOz3PDDt461Zm24TYrFBX4MaA5fZQoW2f2AFoYk8efhMpIr2idtvPjmGWJ1Vn6zorn V2uw==rnARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;rn h=to:subject:message-id:date:from:mime-version:dkim-signature;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=iFgtdMnLGQ4oMk3s4MnCIFpZ+7phiGbhbyGs6FheIXNYtFt6pB59CRNH2llvJ+f3bSrn 6DS/wqelDIStDybG4DLPvAi6d+gmcFAjlvRhVgmFphx83vdH8PGL/0e50JbHUFUszTqern aDQKGvB38dK+GpFk2lE0Ibw6NBOZlmN//xSmSGXl4i0tc5SybecqcVKFa4CFpP+o69Bhrn nScpBZHn4kCo3O0H8aDtFi5ThVbOBczIXNwgjCadF09N1jlrzt/je8IIogyuvrJ/jUBvrn NLJkWk2isCq8f2Q0m6NghqaNVdtlh0cG34i7oU39sjnYxEZ1E6l/zwCI2gvPOA7RzvNTrn CbnQ==rnARC-Authentication-Results: i=1; mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnReturn-Path: <pingY@gmail.com>rnReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])rn by mx.google.com with SMTPS id FAKE.rn for <zXtest@gmail.com>rn (Google Transport Security);rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnReceived-SPF: pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) client-ip=111.8115.111.11;rnAuthentication-Results: mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=gmail.com; s=20161025;rn h=mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=gxAxSqvaKL/tnvzcAYWgKd7XNimR8vdxz7ashDknF2+nZLSQHYSHaIjdOzSX7dZFCwrn yDl+9jo6P7RTupihLR/6xve7iJf3bbt98Q6BjDTNhOTE9n5cK/WTrrVh+vvu36P0CPEern H+q2BYzltAX6hlXog3UaV9UZZ4wTzZUHICJYoOPCkwIwJz3C00HLA3xLbqvxTESisauhrn Blahrn +Bah+iCaGPLWIL9XIhT30BQaNEPLX0vxIh4pYhjrlT8rn lPYA==rnX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=1e100.net; s=20161025;rn h=x-gm-message-state:mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=ACzy4UErTUjbqaUkxPA1yAc87d9gpnlsNKuKE/IHAfL6BY/zOyWZe1nNdTdpBKUPbfrn 1PK2L05DAfD+JZRC7xxhOrGhfKU6vES9jk50V/R0sJGgIWTz7Mp6JJIDR3ubMg/leASOrn BBl5wsrePqO/hqNdTv6/GwnjEJrMRmlYG7CF/Yl5f5Xo8FdGnr65T7M6RjCBqfSvMmmarn T5v9HcWz8jwpmdoHsD5cUf/hnpC+xIhzX0RlKb4U/TjHP6s7UJ0JrQrH6oDUQOdqniH1rn 1owpsiqS6lxU44jGkl1PbiSiryiRfkb6cMIf6aOwT30XFY4kZsGlOO5QN8i1/UbXFNl8rn sM1w==rnX-Gm-Message-State: AA+aEWYeL+LxPlxaP3qr+R6Um7j/o66pbmbLcLI9rY/mB9OgwrdILrhZrntabpjiMOwJyy4ihBPviIGHxV2JHXO/tGet/97T31uSyGRKMc=rnX-Google-Smtp-Source: ABC/BLANK/BLANK=rnX-Received: by 2002:adf:c44a:: with SMTP id FAKE.1.FAKE;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnMIME-Version: 1.0rnFrom: Y <pingY@gmail.com>rnDate: Wed, 28 Nov 2018 04:16:28 +1100rnMessage-ID: <CA+KiAqOmG37ysA-+JPgMopYpn41A12oGbNTXzp9dQDAmS5-tVQ@mail.gmail.com>rnSubject: Welcome to the X PortalrnTo: zXtest@gmail.comrnContent-Type: multipart/alternative; boundary="000000000000dd770b057ba89c59"rnrn--000000000000dd770b057ba89c59rnContent-Type: text/plain; charset="UTF-8"rnrnHi test1rnWelcome to the X Portal this si 2nd emailrnYrnrn--000000000000dd770b057ba89c59rnContent-Type: text/html; charset="UTF-8"rnrn<div dir="ltr"><div dir="ltr"><div>Hi Y</div><div>Welcome to X this si 2nd email</div><div>Y<br></div></div></div>rnrn--000000000000dd770b057ba89c59--rn'
I have read a several stackoverflow about reading email info. i.e python imaplib to get gmail inbox subjects titles and sender name
They mostly assume that the data returned are tuple , and they do somethign liek this
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
How can I retrieve info such as Subjects and To and etc.
It does not seem to be a dictionary since I tried
dTemp = {}
dTemp = raw_email
dTemp["Subject"]
and that last statement gives me an error
python email
I am trying to read the information on the last email received:
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('test1@gmail.com', 'ABC#')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest
#print ("latest_email_id= ", latest_email_id)
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
The print statement for "raw_email" is something like this (note I have changed info for security reasons:
b'Delivered-To: test1@gmail.comrnReceived: by 2002:ac8:4593:0:0:0:0:0 with SMTP id l1234;rn Tue, 27 Nov 2018 09:16:41 -0800 (PST)rnX-Received: by 2002:a5d:4ec4:: with SMTP id 111.1.1111;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnARC-Seal: i=1; a=rsa-sha256; t=1543339000; cv=none;rn d=google.com; s=arc-20160816;rn b=AYhrN2mVh6Ho33wOVqGReMIqrFd7+UuozI633AT/FE59WUVIlg539V2+SYxGl1YXp7rn q7n/17RQdSeBtv4FghG6T8C4FyXvrqnsoiXjtVH48MrJ0g+fqVdRQCINaLG6LP4Z/XAUrn bjDhcg1bV9gqdOtOws5FMjnRtruWo390/jqqHaCOlJNy1++Yw2rHY894Bh619p6m2GL5rn Yj4nLaDrB45M2O/CqLGpD0EJtYEkdUO1WClqf7qU35bDV0l8ALm4JVUM0P1aNeiFK5RIrn sISOWOz3PDDt461Zm24TYrFBX4MaA5fZQoW2f2AFoYk8efhMpIr2idtvPjmGWJ1Vn6zorn V2uw==rnARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;rn h=to:subject:message-id:date:from:mime-version:dkim-signature;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=iFgtdMnLGQ4oMk3s4MnCIFpZ+7phiGbhbyGs6FheIXNYtFt6pB59CRNH2llvJ+f3bSrn 6DS/wqelDIStDybG4DLPvAi6d+gmcFAjlvRhVgmFphx83vdH8PGL/0e50JbHUFUszTqern aDQKGvB38dK+GpFk2lE0Ibw6NBOZlmN//xSmSGXl4i0tc5SybecqcVKFa4CFpP+o69Bhrn nScpBZHn4kCo3O0H8aDtFi5ThVbOBczIXNwgjCadF09N1jlrzt/je8IIogyuvrJ/jUBvrn NLJkWk2isCq8f2Q0m6NghqaNVdtlh0cG34i7oU39sjnYxEZ1E6l/zwCI2gvPOA7RzvNTrn CbnQ==rnARC-Authentication-Results: i=1; mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnReturn-Path: <pingY@gmail.com>rnReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])rn by mx.google.com with SMTPS id FAKE.rn for <zXtest@gmail.com>rn (Google Transport Security);rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnReceived-SPF: pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) client-ip=111.8115.111.11;rnAuthentication-Results: mx.google.com;rn dkim=pass header.i=@gmail.com header.s=20161025 header.b=gxAxSqva;rn spf=pass (google.com: domain of pingY@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=Y@gmail.com;rn dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.comrnDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=gmail.com; s=20161025;rn h=mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=gxAxSqvaKL/tnvzcAYWgKd7XNimR8vdxz7ashDknF2+nZLSQHYSHaIjdOzSX7dZFCwrn yDl+9jo6P7RTupihLR/6xve7iJf3bbt98Q6BjDTNhOTE9n5cK/WTrrVh+vvu36P0CPEern H+q2BYzltAX6hlXog3UaV9UZZ4wTzZUHICJYoOPCkwIwJz3C00HLA3xLbqvxTESisauhrn Blahrn +Bah+iCaGPLWIL9XIhT30BQaNEPLX0vxIh4pYhjrlT8rn lPYA==rnX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;rn d=1e100.net; s=20161025;rn h=x-gm-message-state:mime-version:from:date:message-id:subject:to;rn bh=DfCkM5yA0BCKRwEW2DYkDiGYlT4tuDfEz10cbS7lscs=;rn b=ACzy4UErTUjbqaUkxPA1yAc87d9gpnlsNKuKE/IHAfL6BY/zOyWZe1nNdTdpBKUPbfrn 1PK2L05DAfD+JZRC7xxhOrGhfKU6vES9jk50V/R0sJGgIWTz7Mp6JJIDR3ubMg/leASOrn BBl5wsrePqO/hqNdTv6/GwnjEJrMRmlYG7CF/Yl5f5Xo8FdGnr65T7M6RjCBqfSvMmmarn T5v9HcWz8jwpmdoHsD5cUf/hnpC+xIhzX0RlKb4U/TjHP6s7UJ0JrQrH6oDUQOdqniH1rn 1owpsiqS6lxU44jGkl1PbiSiryiRfkb6cMIf6aOwT30XFY4kZsGlOO5QN8i1/UbXFNl8rn sM1w==rnX-Gm-Message-State: AA+aEWYeL+LxPlxaP3qr+R6Um7j/o66pbmbLcLI9rY/mB9OgwrdILrhZrntabpjiMOwJyy4ihBPviIGHxV2JHXO/tGet/97T31uSyGRKMc=rnX-Google-Smtp-Source: ABC/BLANK/BLANK=rnX-Received: by 2002:adf:c44a:: with SMTP id FAKE.1.FAKE;rn Tue, 27 Nov 2018 09:16:40 -0800 (PST)rnMIME-Version: 1.0rnFrom: Y <pingY@gmail.com>rnDate: Wed, 28 Nov 2018 04:16:28 +1100rnMessage-ID: <CA+KiAqOmG37ysA-+JPgMopYpn41A12oGbNTXzp9dQDAmS5-tVQ@mail.gmail.com>rnSubject: Welcome to the X PortalrnTo: zXtest@gmail.comrnContent-Type: multipart/alternative; boundary="000000000000dd770b057ba89c59"rnrn--000000000000dd770b057ba89c59rnContent-Type: text/plain; charset="UTF-8"rnrnHi test1rnWelcome to the X Portal this si 2nd emailrnYrnrn--000000000000dd770b057ba89c59rnContent-Type: text/html; charset="UTF-8"rnrn<div dir="ltr"><div dir="ltr"><div>Hi Y</div><div>Welcome to X this si 2nd email</div><div>Y<br></div></div></div>rnrn--000000000000dd770b057ba89c59--rn'
I have read a several stackoverflow about reading email info. i.e python imaplib to get gmail inbox subjects titles and sender name
They mostly assume that the data returned are tuple , and they do somethign liek this
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
How can I retrieve info such as Subjects and To and etc.
It does not seem to be a dictionary since I tried
dTemp = {}
dTemp = raw_email
dTemp["Subject"]
and that last statement gives me an error
python email
python email
edited Jan 29 at 17:45
user3556956
asked Nov 27 '18 at 18:43
user3556956user3556956
1288
1288
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
For parsing an email it is best to use the built-in email.parser
module.
If you have the raw_email
as bytes
, you can use email.parser.BytesParser()
to parse it:
>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True) # or False...
Then see the docs on email.message.Message
for accessing data from the message, but in general it works this way:
>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'
Thank you very much
– user3556956
Nov 27 '18 at 20:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506202%2fpython-reading-email-info%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For parsing an email it is best to use the built-in email.parser
module.
If you have the raw_email
as bytes
, you can use email.parser.BytesParser()
to parse it:
>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True) # or False...
Then see the docs on email.message.Message
for accessing data from the message, but in general it works this way:
>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'
Thank you very much
– user3556956
Nov 27 '18 at 20:46
add a comment |
For parsing an email it is best to use the built-in email.parser
module.
If you have the raw_email
as bytes
, you can use email.parser.BytesParser()
to parse it:
>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True) # or False...
Then see the docs on email.message.Message
for accessing data from the message, but in general it works this way:
>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'
Thank you very much
– user3556956
Nov 27 '18 at 20:46
add a comment |
For parsing an email it is best to use the built-in email.parser
module.
If you have the raw_email
as bytes
, you can use email.parser.BytesParser()
to parse it:
>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True) # or False...
Then see the docs on email.message.Message
for accessing data from the message, but in general it works this way:
>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'
For parsing an email it is best to use the built-in email.parser
module.
If you have the raw_email
as bytes
, you can use email.parser.BytesParser()
to parse it:
>>> parser = email.parser.BytesParser()
>>> parsed_message = parser.parsebytes(email_body, headersonly=True) # or False...
Then see the docs on email.message.Message
for accessing data from the message, but in general it works this way:
>>> parsed_message["Subject"]
'Welcome to the X Portal'
>>> parsed_message["From"]
'Y Ferdowsian <pingY@gmail.com>'
>>> parsed_message["To"]
'zXtest@gmail.com'
answered Nov 27 '18 at 19:49
zvonezvone
9,78712348
9,78712348
Thank you very much
– user3556956
Nov 27 '18 at 20:46
add a comment |
Thank you very much
– user3556956
Nov 27 '18 at 20:46
Thank you very much
– user3556956
Nov 27 '18 at 20:46
Thank you very much
– user3556956
Nov 27 '18 at 20:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506202%2fpython-reading-email-info%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown