I can not remove email from Sent folder. From Inbox folder it removes well. I tried with Sent and with [Gmail]/Sent parameters but it doesn't work. Where it could be the issue?
import imaplib
email = "my#gmail.com"
passw = "mypass"
imapserver = "imap.gmail.com"
def deleteEmailIMAP(user, password, IMAP):
mail = imaplib.IMAP4_SSL(IMAP)
mail.login(user, password)
mail.select("[Gmail]/Sent")
typ, data = mail.search(None, 'subject 2f0802e2-e396-4a37-aeae-3a51b6ad288f')
for num in data[0].split():
mail.store(num, '+FLAGS', r'(\Deleted)')
mail.expunge()
mail.close()
mail.logout()
deleteEmailIMAP(email, passw, imapserver)
logs:
Traceback (most recent call last):
File "/Users/nikolai/Documents/Python/gmail/test/cv.py", line 23, in <module>
deleteEmailIMAP(email, passw, imapserver)
File "/Users/nikolai/Documents/Python/gmail/test/cv.py", line 15, in deleteEmailIMAP
typ, data = mail.search(None, 'subject 2f0802e2-e396-4a37-aeae-3a51b6ad288f')
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib.py", line 723, in search
typ, dat = self._simple_command(name, *criteria)
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib.py", line 944, in _command
', '.join(Commands[name])))
imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED
The code does not handle errors. That's why it does not catch server's tagged NO response to the SELECT command. It is not visible in the error log, either, but the exception that you've reported means that by the time the code issues SEARCH, no mailbox is opened -- and that means that SELECT must have failed. (Check the IMAP protocol states.)
GMail uses localized mailbox names, so the likely culprit is that the user you're authenticating as does not have a mailbox named [Gmail]/Sent. If you need to discover the "sent folder" mailbox name, try RFC 6154 (or see Google's docs).
Related
I have configured one Gmail id for default inbox inside email Account list, I am geting an Error . When a new email is recieved in the email inbox.
Step to reproduce :
configure new email to Email Account doctype
send a mail to the mail id
The mail should be added in Communication Doctype , But some Error showing in the Error Log as 'pull_from_email_account'
Traceback (most recent call last):
File "/usr/lib/python3.8/imaplib.py", line 1022, in _command_complete
typ, data = self._get_tagged_response(tag, expect_bye=logout)
File "/usr/lib/python3.8/imaplib.py", line 1148, in _get_tagged_response
self._get_response()
File "/usr/lib/python3.8/imaplib.py", line 1050, in _get_response
resp = self._get_line()
File "/usr/lib/python3.8/imaplib.py", line 1158, in _get_line
line = self.readline()
File "/usr/lib/python3.8/imaplib.py", line 316, in readline
raise self.error("got more than %d bytes" % _MAXLINE)
imaplib.IMAP4.error: got more than 20480 bytes
versions that I use
erpnext 12.30.1
frappe 12.27.0
python 3.8
The mails coming to the configured mails inbox, The mail should be shown in the communication list inside the 'Communication' doctype , But its not coming in the list and an error coming in Error Log list Titled 'pull_from_email_account' Error
RFC 1939 limits POP3 line length to 512 characters. For the Python standard library poplib, they have selected 2048 to be on the safe side. Frappe overrides this limit to 20480 (40x the RFC limit) to be on the even safer side.
You can override the limit by monkey patching poplib._MAXLINE to a higher number that suits your requirement by making a separate Frappe App, or update it directly in Frappe's Email Module.
I use this code to get unseen and new messages from my yahoo mail and find the messages with a specific URL.
from imap_tools import MailBox, AND
import re
from config import email, password
from scrap import scrap
yahooSmtpServer = "imap.mail.yahoo.com"
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
while True:
msgs = client.fetch(AND(seen=False))
for msg in msgs:
mail = msg.html
if 'pick' in mail and not 'Combo-pick' in mail:
for i in re.findall(r'(https?://[^\s]+)', mail):
if 'pick' in i:
link = i.replace('"', "")
print(link)
try:
scrap(link)
except:
pass
client.seen(msg.uid, True)
client.logout()
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
most of the time it works for some time. around 15 minutes. then it returns an error.
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\imaplib.py", line 1047, in _command_complete
typ, data = self._get_tagged_response(tag, expect_bye=logout)
File "C:\Program Files\Python39\lib\imaplib.py", line 1165, in _get_tagged_response
self._check_bye()
File "C:\Program Files\Python39\lib\imaplib.py", line 961, in _check_bye
raise self.abort(bye[-1].decode(self._encoding, 'replace'))
imaplib.abort: IMAP4rev1 Server logging out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\yahooMail (2)\yahooMail (1)\main.py", line 24, in <module>
client = MailBox(yahooSmtpServer).login(email, password, 'INBOX')
File "C:\Program Files\Python39\lib\site-packages\imap_tools\mailbox.py", line 44, in login
self.folder.set(initial_folder)
File "C:\Program Files\Python39\lib\site-packages\imap_tools\folder.py", line 37, in set
result = self.mailbox.box.select(encode_folder(folder))
File "C:\Program Files\Python39\lib\imaplib.py", line 756, in select
typ, dat = self._simple_command(name, mailbox)
File "C:\Program Files\Python39\lib\imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Program Files\Python39\lib\imaplib.py", line 1049, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => IMAP4rev1 Server logging out
Is there any way I can fix it? Or can I fetch the new/unseen Emails without login in every time? (When I tried to do it without login in a loop it didn't get the new messages).
Thank you.
You should consider IMAP IDLE protocol extension described in RFC2177
https://datatracker.ietf.org/doc/html/rfc2177
since 0.51.0 imap_tools has IDLE support:
https://github.com/ikvk/imap_tools/releases/tag/v0.51.0
I'm working on mail application and encountering a big problem with Python imaplib.
Here is the code that I try to use to login to mail server.
M = imaplib.IMAP4(self.server,int(self.port))
M.login(self.user, self.password)
I'm using "port 143", "useSLL = false", no secure.
And here is the message that I receive when trying to login to server.
DEBUG:root:Login failed.
Traceback (most recent call last):
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 347, in getMail
M.login(self.user, self.password)
File "/opt/splunk/lib/python2.7/imaplib.py", line 520, in login
raise self.error(dat[-1])
error: Login failed.
None
Traceback (most recent call last):
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 724, in <module>
parseArgs()
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 716, in parseArgs
imapProc.getMail()
File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 352, in getMail
raise LoginError('Could not log into server: %s with password provided' % self.server)
__main__.LoginError: Could not log into server: (my server) with password provided
p/s: I have another mail application which get emails throw imap on ruby and it's working fine with port 143 no secure.
Anyone please help me to solve this problem. Thanks
Use this instead
M = imaplib.IMAP4_SSL(self.server,int(self.port))
M.login(self.user, self.password)
I am using imaplib.IMAP4_SSL instead of imaplib.IMAP4and this seems to work for me
I got a solution which work for both gmail and Outlook
def connect(self, username, password):
if self.tls:
self.server.starttls()
if(self.hostname == 'imap.outlook.com'):
imap_server = "outlook.office365.com"
self.server = self.transport(imap_server, self.port)
self.server = imaplib.IMAP4_SSL(imap_server)
(retcode, capabilities) = self.server.login(username,password)
self.server.select('inbox')
else:
typ, msg = self.server.login(username, password)
if self.folder:
self.server.select(self.folder)
else:
self.server.select()
I have a Python program that collects about 11K entries from a CORBA database, then uses those entries to parse through about 1,0000 Mb of text files. This entire process normally takes 5-7 minutes. I log into the Exchange365 server before this starts (so that any unusual events can be emailed to the administrator) and then try to email the results to the 'normal' recipients. That last email is failing and the root cause seems to be that I am getting logged off of the Exchange server.
Adding a 'dummy' email about half way through the processing seems to eliminate the timeout, but I'd like to find a more programatic way to accomplish this. Following is a code snippet where I enable the email:
import smtp
import time
pretty_localtime = time.asctime()
smtpserver = smtplib.SMTP("smtp.office365.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
#
try:
smtpserver.login(office365_user,office365_pwd)
except:
msg = ("%s --- FATAL ERROR: Couldn't log into SMTP server.")%pretty_localtime
log_file.write("%s \n"%msg)
print msg
sys.exit()
Has anyone worked through this problem?
***edit** Python error added:
Traceback (most recent call last):
File "piu_billing.py", line 739, in <module>
log_file.write(email_msg)
File "piu_billing.py", line 102, in send_mail
smtpserver.sendmail(office365_user,to,themsg)
File "C:\Python27\lib\smtplib.py", line 719, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 471, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 334, in putcmd
self.send(str)
File "C:\Python27\lib\smtplib.py", line 324, in send
raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected
I'm running pyramid on an Ubuntu Linux server, and am getting a ValueError when trying to use pyramid_mailer. My code is relatively simple, anything seems to cause it:
def my_view(request):
mailer = get_mailer(request)
emailMessage = Message(subject="Welcome", sender="noreply#mysite.com", recipients = ["me#email.com"], body="test")
mailer.send(emailMessage)
Results in this error:
Traceback (most recent call last):
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/router.py", line 242, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/router.py", line 217, in invoke_subrequest
response = handle_request(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.0.2-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 160, in toolbar_tween
return handler(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 79, in tm_tween
manager.abort()
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_manager.py", line 116, in abort
return self.get().abort()
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_transaction.py", line 468, in abort
reraise(t, v, tb)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/transaction-1.4.3-py2.7.egg/transaction/_transaction.py", line 453, in abort
rm.abort(self)
File "/usr/share/nginx/wwwProj/local/lib/python2.7/site-packages/repoze.sendmail-4.2-py2.7.egg/repoze/sendmail/delivery.py", line 119, in abort
raise ValueError("TPC in progress")
ValueError: TPC in progress
I followed the instructions for "Getting Started (The Easier Way)" on this site: http://pyramid-mailer.readthedocs.org/en/latest/
This is a known issue. It can be worked around in the meantime by reverting to repoze.sendmail 4.1 (from 4.2)
recipients = ["me#email.com"]
here you can see
recipients – list of email addresses
This was the first error that I encountered while trying to set up an emailing system, through I cannot remember what I did. In any case, I finally got it working for a gmail sender for SMTP. Hope this someone else in my position:
import smtplib
sender = "noreply"
to = "username"
subject = "Verification Code"
headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
sEmailMessage = headers + "whatever message you want"
mailserver = smtplib.SMTP("smtp.gmail.com", 587)
#--- because of gmail
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
#---
mailserver.login("your_email_address#gmail.com", "your_password")
mailserver.sendmail("from_here#gmail.com", to_here#whatever.com, sEmailMessage)
mailserver.close()