So I'm working on a Python script to extract text from an email and following these instructions to do so. This is the script thus far:
import imapclient
import pprint
import pyzmail
mymail = "my#email.com"
password = input("Password: ")
imapObj = imapclient.IMAPClient('imap.gmail.com' , ssl=True)
imapObj.login(mymail , password)
imapObj.select_folder('INBOX', readonly=False)
UIDs = imapObj.search(['SUBJECT Testing'])
rawMessages = imapObj.fetch([5484], ['BODY[]'])
message = pyzmail.PyzMessage.factory(rawMessages[5484]['BODY[]'])
However I'm getting this error:
message = pyzmail.PyzMessage.factory(rawMessages[5484]['BODY[]'])
KeyError: 5484
5484 being the ID for the email that the search function finds. I've also tried putting UIDs in instead of 5484, but that doesn't work either. Thanks in advance!
Thank you #Madalin Stroe .
I use python3.6.2 and pyzmail1.0.3 on Win10.
When I run
message = pyzmail.PyzMessage.factory(rawMessages[4]['BODY[]'])
The ERR shows like this:
Traceback (most recent call last):
File "PATH/TO/mySinaEmail.py", line 42, in <module>
message = pyzmail.PyzMessage.factory(rawMessages[4]['BODY[]'])
KeyError: 'BODY[]'
When I modified this to message = pyzmail.PyzMessage.factory(rawMessages[4][b'BODY[]']), it run well.
Try replacing ['BODY[]'] with [b'BODY[]']
Related
I'm trying to get an email that is on a website, by copying the style, but I'm not having success with it. The process of navigating through the website and find the email is working perfectly, the only issue is to copy the email address.
I've tried to get the attribute of the field showing the email, but I'm getting the following error:
> Traceback (most recent call last): File
> "c:\Users\RMBORGE\Desktop\PythonLib\tempCodeRunnerFile.python", line
> 21, in <module>
> email = page.locator('xpath= //*[#id="cphMain_hlOrgInboundEmail"]').get_attribute() TypeError:
> get_attribute() missing 1 required positional argument: 'name'
Please see my code below:
from playwright.sync_api import sync_playwright
import time
el = "https://www.getmail.com"
p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(el)
owner = page.fill('xpath=//*[#id="cphMain_MyBA"]', "30291726")
search = page.locator('xpath=//*[#id="cphMain_btnSearch"]').click()
time.sleep(3)
name = page.locator('xpath=//*[#id="cphGrid_InvoicesGrid_PartnerOrgName_0"]').click()
#trying to get the attribute of the field
email = page.locator('xpath=//*[#id="cphMain_hlOrgInboundEmail"]').get_attribute()
print(email)
The code of the website was written like that:
"<a id="cphMain_hlOrgInboundEmail" href="mailto:email#gmail.com" style="font-weight:bold;">email#gmail.com</a>"
You have to use inner_text for this:
email = page.locator.inner_text("#cphMain_hlOrgInboundEmail")
print(email)
I wanted to do an Email writer. I saw a tutorial and this came out:
import smtplib
import os
user = os.getenv("SMTP_USER")
pwd = os.getenv("SMTP_PWO")
mail_text = "Hallo, \n\ndas ist ein Test!\n\n"
subject = "Python Mail"
MAIL_FROM = "x.muelfellner#outlook.de"
RCPT_TO = input("Empfänger: ")
DATA = "From:%s\nTo:%s\nSubject:%s\n\n%" \
(MAIL_FROM,RCPT_TO,subject,mail_text)
server = smtplib.SMTP("secure.emailsrvr.com:587")
server.starttls()
server.login(user,pwd)
server.sendmail(MAIL_FROM,RCPT_TO,DATA)
server.quit()
But when I'm running the code there's an Error. Here:
Traceback (most recent call last):
File "/home/pi/Bookshelf/Pyton/SendEmail.py", line 12, in <module>
(MAIL_FROM,RCPT_TO,subject,mail_text)
TypeError: 'str' object is not callable
I don't know what i have to change! Can someone help me?
Your problem isn't related to smtplib; it's just a syntax error.
You're inadvertently trying to call a string (I removed the \ continuation):
DATA = "From:%s\nTo:%s\nSubject:%s\n\n%"(MAIL_FROM,RCPT_TO,subject,mail_text)
It should be
DATA = "From:%s\nTo:%s\nSubject:%s\n\n%s" % (MAIL_FROM,RCPT_TO,subject,mail_text)
to format a string.
My wave.py file:
import User_Account
username = input('Username: ')
password = input('Password: ')
Session = User_Account(username, password)
My User_Account.py file:
import tidalapi
class User_Account:
def __init__(self, username_, password_):
self.username = username_
self.password = password_
def login(self):
session = tidalapi.Session()
return session.login(self.username, self.password)
When I run the above code in PyCharm I get the following error.
TypeError: 'module' object is not callable
I am reading examples of OOP in Python - such as this - and even when I run their code, I get the same error. I've searched for it here and on Google but the solutions don't seem to fix the issue.
Any suggestions as to what I'm doing wrong?
Thank you for your time and if there is anything I can supply to improve my question, please don't hesitate.
EDIT: Full traceback
Traceback (most recent call last):
File "/home/doug/PycharmProjects/Wave/wave.py", line 6, in <module>
Session = User_Account(username, password)
TypeError: 'module' object is not callable
I believe the problem is that you are bringing in the module but not specifying the class you want from that module.
I think the following change to your wave.py would fix this...
Session = User_Account.User_Account(username, password)
Even better, rather than importing User_Account, you might want to say...
from User_Account import User_Account
If you do this, your "Session =" call will work as you currently have it.
It's exactly like the error says, a module is not callable. It looks like you meant to run from user_account import User_Account instead of import User_Account.
Note that import User_Account would fail on a case-sensitive platform like Linux. If you did actually want to import user_account.py you would write import user_account.
import smtplib
smtpserver = s.connect("mail.btinternet.com", 465)
SMTP.helo("mail.btinternet.com")
SMTP.ehlo("mail.btinternet.com")
file = open("Combo.txt", "r")
for line in file:
x = line.split(":")
user = x[0]
password = x[1]
s.login(user, password)
print("[+] Password Found: %s" % password)
if smtplib.SMTPAuthenticationError:
print("Incorrect")
Here's my code. It checks a list of email/password combinations from a file to see if it is on a specific server (in my case BT).
But I am having trouble with the library names, I am not sure what to use. I checked on python docs but it wasn't clear enough, if someone can tell me as to what is incorrect I would deeply appreciate it.
Error received.
This will also give me errors for the other incorrect library names
Traceback (most recent call last):
File "main.py", line 3, in <module>
smtpserver = s.connect("mail.btinternet.com", 465)
NameError: name 's' is not defined
exited with non-zero status
For your problem I think the reason why as to your libraries are not working properly is because you're calling your imported library inconsistently:
e.g. sometimes you type 's.xxxxx', sometimes you type 'SMTPlib.xxxxx' for your module attributes, you should import smtplib as 's'.
So what does this is it stores the library in a short form named 's', so whenever you call a module or use a function from the library, you don't have to type the full '.smtplib' but instead just type a '.s' extension behind the specific function:
import smtplib as s
smtpserver = s.connect("mail.btinternet.com", 465)
s.helo("mail.btinternet.com")
s.ehlo("mail.btinternet.com")
file = open("Combo.txt", "r")
for line in file:
x = line.split(":")
user = x[0]
password = x[1]
s.login(user, password)
print("[+] Password Found: %s" % password)
if s.SMTPAuthenticationError:
print("Incorrect")
Should fix your problems now. remember to call functions from the specific library name in a consistent manner ('s').
I cannot figure out how to get fan_count from a page.
I always get this error
Traceback (most recent call last):
File "./facebook_api.py", line 37, in <module>
facebook_graph.get_object('somepublicpage')['fan_count']
KeyError: 'fan_count'
The object only contains id/name and I cannot figure out how to give more permissions in order to get the 'fan_count' data.
Here is the code i'm using:
import facebook
import urllib
import urlparse
import subprocess
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
oauth_args = dict(client_id = FACEBOOK_APP_ID,
client_secret = FACEBOOK_APP_SECRET,
grant_type = 'client_credentials')
oauth_curl_cmd = ['curl',
'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)]
oauth_response = subprocess.Popen(oauth_curl_cmd,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE).communicate()[0]
try:
oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0]
except KeyError:
print('Unable to grab an access token!')
exit()
print oauth_access_token
facebook_graph = facebook.GraphAPI(oauth_access_token)
print facebook_graph.get_object(PROFILE_ID)['fan_count']
Since v2.4 of the Graph API, you have to specify the fields you want to get returned. This would be the correct API call:
/{page-id}?fields=name,fan_count
It is called "Declarative Fields".