Here are the examples of the python api email.parser.Parser taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
19 Examples
3
Example 1
Project: dirigible-spreadsheet Source File: functionaltest.py
def all_emails(self, server):
server.login(IMAP_USERNAME, IMAP_PASSWORD)
server.select_folder('INBOX')
messages = server.search(['NOT DELETED'])
response = server.fetch(messages, ['RFC822.TEXT', 'RFC822.HEADER'])
parser = Parser()
for m_id, m in response.items():
parsed_headers = parser.parsestr(m['RFC822.HEADER'])
body_text = m['RFC822.TEXT']
yield (m_id, parsed_headers, body_text)
3
Example 2
Project: webvulnscan Source File: utils.py
def parse_http_headers(bs):
assert isinstance(bs, bytes)
s = bs.decode('utf-8')
p = email.parser.Parser()
res = p.parse(io.StringIO(s), headersonly=True)
return res
3
Example 3
def mark(self, contents):
parser = email.parser.Parser()
message = parser.parsestr(contents)
self.date(message)
self.user_agent(message)
return message.as_string()
3
Example 4
Project: pyesridump Source File: cli.py
def _collect_headers(strings):
headers = {}
parser = email.parser.Parser()
for string in strings:
headers.update(dict(parser.parsestr(string)))
return headers
0
Example 5
def __init__(self):
self._parser = Parser()
0
Example 6
def parse(body):
p = Parser()
msg = p.parsestr(body)
if KEYWORD not in msg['Subject'] or not msg.is_multipart():
raise CorruptMail
attachments = []
body_text = b""
body_html = b""
for part in msg.walk():
attachment = email_parse_attachment(part)
if attachment:
attachments.append(attachment)
elif part.get_content_type() == "text/plain":
body_text += part.get_payload(decode=True)
sha1 = body_text.split(b'\n')[0]
#elif part.get_content_type() == "text/html":
# body_html += unicode(part.get_payload(decode=True),
# part.get_content_charset(), 'replace').encode('utf8', 'replace')
if len(attachments) == 0 or len(sha1) != 40:
raise CorruptMail
return sha1.decode('UTF-8'), attachments[0]['filedata'].decode('UTF-8')
0
Example 7
Project: pyramid_mailer Source File: test_message.py
def test_repoze_sendmail_send_to_queue_functional(self):
# functest that emulates the interaction between pyramid_mailer and
# repoze.maildir.add and queuedelivery.send.
import tempfile
from email.generator import Generator
from email.parser import Parser
from pyramid_mailer.message import Message
from pyramid_mailer.message import Attachment
from repoze.sendmail.encoding import cleanup_message
from repoze.sendmail.delivery import copy_message
def checkit(msg):
self.assertEqual(
msg['Content-Type'],
'text/plain; charset="iso-8859-1"'
)
self.assertEqual(
msg['Content-Transfer-Encoding'], transfer_encoding)
payload = msg.get_payload()
self.assertEqual(payload, expected)
charset = 'iso-8859-1'
text_encoded = b'LaPe\xf1a'
text = text_encoded.decode(charset)
expected = _qencode(text_encoded).decode('ascii')
transfer_encoding = 'quoted-printable'
body = Attachment(
data=text,
transfer_encoding=transfer_encoding
)
msg = Message(
subject="testing",
sender="[email protected]",
recipients=["[email protected]"],
body=body
)
# done in pyramid_mailer via mailer/send_to_queue
msg = msg.to_message()
msg.as_string()
checkit(msg)
# done in repoze.sendmail via delivery/AbstractMailDelivery/send
cleanup_message(msg)
checkit(msg)
# done in repoze.sendmail via
# delivery/AbstractMailDelivery/createDataManager
msg_copy = copy_message(msg)
checkit(msg_copy)
try:
# emulate what repoze.sendmail maildir.py/add does
fn = tempfile.mktemp()
fd = os.open(fn,
os.O_CREAT|os.O_EXCL|os.O_WRONLY,
0o600
)
with os.fdopen(fd, 'w') as f:
writer = Generator(f)
writer.flatten(msg_copy)
# emulate what repoze.sendmail.queue _parseMessage does
with open(fn) as foo:
parser = Parser()
reconstituted = parser.parse(foo)
checkit(reconstituted)
finally: # pragma: no cover
try:
os.remove(fn)
except:
pass
0
Example 8
Project: IMAPdedup Source File: imapdedup.py
def process(options, mboxes):
if options.process:
serverclass = imaplib.IMAP4_stream
elif options.ssl:
serverclass = imaplib.IMAP4_SSL
else:
serverclass = imaplib.IMAP4
try:
if options.process:
server = serverclass(options.process)
elif options.port:
server = serverclass(options.server, options.port)
else:
# Use the default, which will be different depending on SSL choice
server = serverclass(options.server)
except socket.error as e:
sys.stderr.write("\nFailed to connect to server. Might be host, port or SSL settings?\n")
sys.stderr.write("%s\n\n" % e)
sys.exit(1)
if ('STARTTLS' in server.capabilities) and hasattr(server, 'starttls'):
server.starttls()
elif not options.ssl:
sys.stderr.write('\nWarning: Unencrypted connection\n')
try:
if not options.process:
server.login(options.user, options.password)
except:
sys.stderr.write("\nError: Login failed\n")
sys.exit(1)
# List mailboxes option
if options.just_list:
for mb in check_response(server.list()):
mb = mb.decode('utf-7')
bits = parse_list_response(mb)
if r'\\Noselect' not in bits[0]:
print(bits[2])
sys.exit()
if len(mboxes) == 0:
sys.stderr.write("\nError: Must specify mailbox\n")
sys.exit(1)
# OK - let's get started.
# Iterate through a set of named mailboxes and delete the later messages discovered.
try:
p = email.parser.Parser() # can be the same for all mailboxes
# Create a list of previously seen message IDs, in any mailbox
msg_ids = {}
for mbox in mboxes:
msgs_to_delete = [] # should be reset for each mbox
msg_map = {} # should be reset for each mbox
# Select the mailbox
msgs = check_response(server.select(mbox, options.dry_run))[0]
print("There are %d messages in %s." % (int(msgs), mbox))
# Check how many messages are already marked 'deleted'...
deleted = check_response(server.search(None, 'DELETED'))[0].split()
numdeleted = len(deleted)
print("%s message(s) currently marked as deleted in %s" % (numdeleted or "No", mbox))
# ...and get a list of the ones that aren't deleted. That's what we'll use.
msgnums = check_response(server.search(None, 'UNDELETED'))[0].split()
print("%s others in %s" % (len(msgnums), mbox))
chunkSize = 100
if options.verbose: print ("Reading the others... (in batches of %d)" % chunkSize)
for i in range(0, len(msgnums), chunkSize):
msgnums_in_chunk = msgnums[i:i + chunkSize]
message_ids = ','.join(msgnums_in_chunk)
# Get the header of each message
ms = check_response(server.fetch(message_ids, '(RFC822.HEADER)'))
if options.verbose:
print ("Batch starting at item %d" % i)
# and parse them.
for ci in range(0, len(msgnums_in_chunk)):
mnum = msgnums_in_chunk[ci]
mp = p.parsestr(ms[ci * 2][1])
if options.verbose:
print("Checking %s message %s" % (mbox, mnum))
# Record the message-ID header (or generate one from other headers)
msg_id = get_message_id(mp, options.use_checksum, options.use_id_in_checksum)
# Store message only when verbose is enabled (to print it later on)
if options.verbose:
msg_map[mnum] = mp
if msg_id:
# If we've seen this message before, record it as one to be
# deleted in this mailbox.
if msg_id in msg_ids:
print ("Message %s_%s is a duplicate of %s and %s be marked as deleted" % (
mbox, mnum, msg_ids[msg_id], options.dry_run and "would" or "will"))
if options.verbose:
print ("Subject: %s\nFrom: %s\nDate: %s\n" % (mp['Subject'], mp['From'], mp['Date']))
msgs_to_delete.append(mnum)
# Otherwise record the fact that we've seen it
else:
msg_ids[msg_id] = mbox + '_' + mnum
print ("%s message(s) in %s processed" % (min(len(msgnums), i + chunkSize), mbox))
# OK - we've been through this mailbox, and msgs_to_delete holds
# a list of the duplicates we've found.
if len(msgs_to_delete) == 0:
print("No duplicates were found in %s" % mbox)
else:
if options.verbose:
print("These are the duplicate messages: ")
for mnum in msgs_to_delete:
print_message_info(msg_map[mnum])
if options.dry_run:
print("If you had not selected the 'dry-run' option,\n%i messages would now be marked as 'deleted'." % (len(msgs_to_delete)))
else:
print("Marking %i messages as deleted..." % (len(msgs_to_delete)))
# Deleting messages one at a time can be slow if there are many, so we batch them up
chunkSize = 30
if options.verbose: print("(in batches of %d)" % chunkSize)
for i in range(0, len(msgs_to_delete), chunkSize):
message_ids = ','.join(msgs_to_delete[i:i + chunkSize])
check_response(server.store(message_ids, '+FLAGS', r'(\Deleted)'))
if options.verbose:
print("Batch starting at item %d marked." % i)
print("Confirming new numbers...")
deleted = check_response(server.search(None, 'DELETED'))[0].split()
numdeleted = len(deleted)
undeleted = check_response(server.search(None, 'UNDELETED'))[0].split()
numundel = len(undeleted)
print("There are now %s messages marked as deleted and %s others in %s." % (numdeleted, numundel, mbox))
if not options.no_close:
server.close()
except ImapDedupException as e:
print >> sys.stderr, "Error:", e
finally:
server.logout()
0
Example 9
Project: cartman Source File: app.py
def run_new(self, owner=None):
"""Create a new ticket and return its id if successful.
usage: cm new [owner]
"""
template = self.resolve_template()
if not template:
if self.message_file:
if self.message_file == "-":
template = sys.stdin.read()
else:
with open(self.message_file) as fp:
template = fp.read()
else:
template = DEFAULT_TEMPLATE
# Parse the template ahead of time, allowing us to insert the Owner/To.
ep = email.parser.Parser()
em = ep.parsestr(template)
body = em.get_payload()
headers = OrderedDict(em.items())
# The owner specified on the command line always prevails.
if owner:
headers["To"] = owner
# If all else fail, assign it to yourself.
if not headers["To"]:
headers["To"] = self.username
self.login()
valid = False
while not valid:
# Get the properties at each iteration, in case an admin updated
# the list in the mean time.
options = self.get_property_options()
# Assume the user will produce a valid ticket
valid = True
# Load the current values in a temp file for editing
(fd, filename) = tempfile.mkstemp(suffix=".cm.ticket")
fp = os.fdopen(fd, "w")
fp.write(self._format_headers(headers))
fp.write("\n\n")
fp.write(body)
fp.close()
# When reading the message from stdin, we can't edit, skip editor.
if not self.message_file:
self.editor(filename)
# Use the email parser to get the headers.
ep = email.parser.Parser()
with open(filename, "r") as fp:
em = ep.parse(fp)
os.unlink(filename)
body = em.get_payload()
headers = OrderedDict(em.items())
errors = []
fuzzy_match_fields = ("Milestone", "Component", "Type", "Version",
"Priority")
# Ensures all the required fields are filled-in
for key in self.required_fields:
if key in fuzzy_match_fields:
continue
if not headers.get(key) or "**ERROR**" in headers[key]:
errors.append("Invalid '{}': cannot be blank".format(key))
# Some fields are tolerant to incomplete values, this is where we
# try to complete them.
for key in fuzzy_match_fields:
lkey = key.lower()
if lkey not in options:
continue
valid_options = options[lkey]
# The specified value is not available in the multi-choice.
if key in headers and headers[key] not in valid_options:
m = text.fuzzy_find(headers[key], valid_options)
if m:
# We found a close match, update the value with it.
headers[key] = m
else:
# We didn't find a close match. If the user entered
# something explicitly or if this field is required,
# this is an error, else just wipe the value and move
# on.
if headers[key] or key in self.required_fields:
joined_options = ", ".join(valid_options)
errors.append(u"Invalid '{}': expected: {}"
.format(key, joined_options))
else:
headers[key] = ""
if errors:
valid = False
print("\nFound the following errors:")
for error in errors:
print(u" - {}".format(error))
try:
if not self.message_file:
self.input("\n-- Hit Enter to return to editor, "
"^C to abort --\n")
except KeyboardInterrupt:
raise exceptions.FatalError("ticket creation interrupted")
# There is no editor loop when reading message from stdin, just
# print the errors and exit.
if self.message_file:
break
# Since the body is expected to be using CRLF line termination, we
# replace newlines by CRLF if no CRLF is found.
if "\r\n" not in body:
body = body.replace("\n", "\r\n")
fields_data = {
"field_summary": headers.get("Subject", ""),
"field_type": headers.get("Type", ""),
"field_version": headers.get("Version", ""),
"field_description": body,
"field_milestone": headers.get("Milestone", ""),
"field_component": headers.get("Component", ""),
"field_owner": headers.get("To", ""),
"field_keywords": headers.get("Keywords", ""),
"field_cc": headers.get("Cc", ""),
"field_attachment": "",
}
# Assume anything outside of the original headers it to be included as
# fields.
for key, value in headers.items():
field_name = "field_" + key.lower()
if field_name not in fields_data:
fields_data[field_name] = value
r = self.post("/newticket", fields_data)
if r.status_code != 200:
message = text.extract_message(r.text)
if not message:
message = "unable to create new ticket"
raise exceptions.RequestException(message)
try:
ticket_id = int(r.url.split("/")[-1])
except:
raise exceptions.RequestException("returned ticket_id is invalid.")
self.open_in_browser_on_request(ticket_id)
return ["ticket #{} created".format(ticket_id)]
0
Example 10
def test_parser(self):
p = email.parser.Parser(policy=self.MyPolicy)
with self.assertRaisesRegex(Exception, "^test$"):
p.parsestr('Subject: test\n\n')
0
Example 11
Project: apitools Source File: batch.py
def _DeserializeResponse(self, payload):
"""Convert string into Response and content.
Args:
payload: Header and body string to be deserialized.
Returns:
A Response object
"""
# Strip off the status line.
status_line, payload = payload.split('\n', 1)
_, status, _ = status_line.split(' ', 2)
# Parse the rest of the response.
parser = email_parser.Parser()
msg = parser.parsestr(payload)
# Get the headers.
info = dict(msg)
info['status'] = status
# Create Response from the parsed headers.
content = msg.get_payload()
return http_wrapper.Response(info, content, self.__batch_url)
0
Example 12
Project: apitools Source File: batch.py
def _Execute(self, http):
"""Serialize batch request, send to server, process response.
Args:
http: A httplib2.Http object to be used to make the request with.
Raises:
httplib2.HttpLib2Error if a transport error has occured.
apiclient.errors.BatchError if the response is the wrong format.
"""
message = mime_multipart.MIMEMultipart('mixed')
# Message should not write out its own headers.
setattr(message, '_write_headers', lambda self: None)
# Add all the individual requests.
for key in self.__request_response_handlers:
msg = mime_nonmultipart.MIMENonMultipart('application', 'http')
msg['Content-Transfer-Encoding'] = 'binary'
msg['Content-ID'] = self._ConvertIdToHeader(key)
body = self._SerializeRequest(
self.__request_response_handlers[key].request)
msg.set_payload(body)
message.attach(msg)
request = http_wrapper.Request(self.__batch_url, 'POST')
request.body = message.as_string()
request.headers['content-type'] = (
'multipart/mixed; boundary="%s"') % message.get_boundary()
response = http_wrapper.MakeRequest(http, request)
if response.status_code >= 300:
raise exceptions.HttpError.FromResponse(response)
# Prepend with a content-type header so Parser can handle it.
header = 'content-type: %s\r\n\r\n' % response.info['content-type']
parser = email_parser.Parser()
mime_response = parser.parsestr(header + response.content)
if not mime_response.is_multipart():
raise exceptions.BatchError(
'Response not in multipart/mixed format.')
for part in mime_response.get_payload():
request_id = self._ConvertHeaderToId(part['Content-ID'])
response = self._DeserializeResponse(part.get_payload())
# Disable protected access because namedtuple._replace(...)
# is not actually meant to be protected.
# pylint: disable=protected-access
self.__request_response_handlers[request_id] = (
self.__request_response_handlers[request_id]._replace(
response=response))
0
Example 13
Project: google-cloud-python Source File: batch.py
def _unpack_batch_response(response, content):
"""Convert response, content -> [(headers, payload)].
Creates a generator of tuples of emulating the responses to
:meth:`httplib2.Http.request` (a pair of headers and payload).
:type response: :class:`httplib2.Response`
:param response: HTTP response / headers from a request.
:type content: str
:param content: Response payload with a batch response.
"""
parser = Parser()
message = _generate_faux_mime_message(parser, response, content)
if not isinstance(message._payload, list):
raise ValueError('Bad response: not multi-part')
for subrequest in message._payload:
status_line, rest = subrequest._payload.split('\n', 1)
_, status, _ = status_line.split(' ', 2)
sub_message = parser.parsestr(rest)
payload = sub_message._payload
ctype = sub_message['Content-Type']
msg_headers = dict(sub_message._headers)
msg_headers['status'] = status
headers = httplib2.Response(msg_headers)
if ctype and ctype.startswith('application/json'):
payload = json.loads(payload)
yield headers, payload
0
Example 14
Project: simone Source File: views.py
@login_required
def viewmsg(request, server, folder, uid):
"""view message text
:param request: request object from Django
:param server: server number
:param folder: which folder the message is in
:param uid: the uid of the message to fetch
"""
# these are here so they get passed through to the template
folder = folder
uid = int(uid)
server = int(server)
isrv = request.user.imap_servers.all()[server]
i = imapclient.IMAPClient(isrv.address, port=isrv.port, use_uid=False, ssl=isrv.ssl)
i.login(isrv.username, isrv.passwd)
i.select_folder(folder)
p = email.parser.Parser()
# BODY = metadata; BODY[] = the actual text of the body
mailbody = i.fetch([uid], ['BODY', 'BODY[]'])
debug(mailbody)
mailstr = mailbody[uid]['BODY[]'].encode('ascii', 'ignore')
if len(mailbody[uid]['BODY']) > 2 and mailbody[uid]['BODY'][2][1] == u'utf-8':
mailstr = mailbody[uid]['BODY[]'].encode('ascii', 'ignore')
elif len(mailbody[uid]['BODY']) <= 2 and mailbody[uid]['BODY'][0][0][2][1] == u'utf-8':
mailstr = mailbody[uid]['BODY[]']
mailmsg = p.parsestr(mailstr)
if not mailmsg.is_multipart():
body = mailmsg.get_payload(decode=True)
else:
for part in mailmsg.walk():
if(part.get_content_type() == 'text/plain'):
body = part.get_payload(decode=True)
break # FIXME this assumes the first text/plain part is what we want
if body == "":
body = "There is no plain text version of this message."
#body = body + "Raw Source:<br />" + mailmsg
i.logout()
if request.GET.get('json') == "true":
return HttpResponse(json.dumps({'headers':mailmsg.items(), 'body':body}))
data = {
'mailmsg':mailmsg,
'body':body,
'folder': folder,
'uid': uid,
'server': server,
}
return render(request, 'mail/viewmsg.html', data)
0
Example 15
Project: simone Source File: views.py
@login_required
def viewmsg(request, server, folder, uid):
"""view message text
:param request: request object from Django
:param server: server number
:param folder: which folder the message is in
:param uid: the uid of the message to fetch
"""
# these are here so they get passed through to the template
folder = folder
uid = int(uid)
server = int(server)
isrv = request.user.imapserver_set.all()[server]
i = imapclient.IMAPClient(isrv.address, port=isrv.port, use_uid=False, ssl=isrv.ssl)
i.login(isrv.username, isrv.passwd)
i.select_folder(folder)
p = email.parser.Parser()
# BODY = metadata; BODY[] = the actual text of the body
mailbody = i.fetch([uid], ['BODY', 'BODY[]'])
debug(mailbody)
mailstr = mailbody[uid]['BODY[]'].encode('ascii', 'ignore')
if len(mailbody[uid]['BODY']) > 2 and mailbody[uid]['BODY'][2][1] == u'utf-8':
mailstr = mailbody[uid]['BODY[]'].encode('ascii', 'ignore')
elif len(mailbody[uid]['BODY']) <= 2 and mailbody[uid]['BODY'][0][0][2][1] == u'utf-8':
mailstr = mailbody[uid]['BODY[]']
mailmsg = p.parsestr(mailstr)
if not mailmsg.is_multipart():
body = mailmsg.get_payload(decode=True)
else:
for part in mailmsg.walk():
if(part.get_content_type() == 'text/plain'):
body = part.get_payload(decode=True)
break # FIXME this assumes the first text/plain part is what we want
if body == "":
body = "There is no plain text version of this message."
#body = body + "Raw Source:<br />" + mailmsg
i.logout()
if request.GET.get('json') == "true":
return HttpResponse(json.dumps({'headers':mailmsg.items(), 'body':body}))
data = {
'mailmsg':mailmsg,
'body':body,
'folder': folder,
'uid': uid,
'server': server,
}
return render(request, 'mail/viewmsg.html', data)
0
Example 16
Project: karl Source File: mailout.py
def mailout(args, env):
registry = env['registry']
queue_path = registry.settings['mail_queue_path']
mailout_throttle = registry.settings.get('mailout_throttle')
mailer = SMTPMailer(
hostname=args.server,
port=args.port,
username=args.username,
password=args.password,
no_tls=args.no_tls,
force_tls=args.force_tls
)
qp = QueueProcessor(mailer, queue_path, ignore_transient=True)
stats = MailoutStats(registry.settings.get('mailout_stats_dir'))
# Instead of calling QueueProcessor.send_messages directly,
# implement a throttle
if mailout_throttle:
int_mailout_throttle = int(mailout_throttle)
else:
int_mailout_throttle = 0
counter = 0
for filename in qp.maildir:
counter += 1
# If we have a throttle and we're over that limit, skip
if mailout_throttle is not None and counter > int_mailout_throttle:
continue
if stats.mailout_stats_dir:
# We're configured to do logging, so log
with open(filename, 'r') as fp:
parser = Parser()
message = parser.parse(fp)
stats.log(message)
try:
qp._send_message(filename)
except smtplib.SMTPDataError, e:
# probably an address verification error. Log and try later.
log.info("Temporary error: %s" % str(e))
0
Example 17
Project: leap_mail Source File: utils.py
def setUp(self):
soledad_adaptor.cleanup_deferred_locks()
USERID = TEST_USER
def setup_server(account):
self.server = TestSoledadIMAPServer(
account=account,
contextFactory=self.serverCTX)
self.server.theAccount = account
d_server_ready = defer.Deferred()
self.client = SimpleClient(
d_server_ready, contextFactory=self.clientCTX)
self.connected = d_server_ready
def setup_account(_):
self.parser = parser.Parser()
# XXX this should be fixed in soledad.
# Soledad sync makes trial block forever. The sync it's mocked to
# fix this problem. _mock_soledad_get_from_index can be used from
# the tests to provide docuements.
# TODO see here, possibly related?
# -- http://www.pythoneye.com/83_20424875/
self._soledad.sync = Mock()
d = defer.Deferred()
self.acc = IMAPAccount(self._soledad, USERID, d=d)
return d
d = super(IMAP4HelperMixin, self).setUp()
d.addCallback(setup_account)
d.addCallback(setup_server)
return d
0
Example 18
Project: leap_mail Source File: service.py
def __init__(self, keymanager, soledad, inbox, userid,
check_period=INCOMING_CHECK_PERIOD):
"""
Initialize IncomingMail..
:param keymanager: a keymanager instance
:type keymanager: keymanager.KeyManager
:param soledad: a soledad instance
:type soledad: Soledad
:param inbox: the collection for the inbox where the new emails will be
stored
:type inbox: MessageCollection
:param check_period: the period to fetch new mail, in seconds.
:type check_period: int
"""
leap_assert(keymanager, "need a keymanager to initialize")
leap_assert_type(soledad, Soledad)
leap_assert(check_period, "need a period to check incoming mail")
leap_assert_type(check_period, int)
leap_assert(userid, "need a userid to initialize")
self._keymanager = keymanager
self._soledad = soledad
self._inbox_collection = inbox
self._userid = userid
self._listeners = []
self._loop = None
self._check_period = check_period
# initialize a mail parser only once
self._parser = Parser()
0
Example 19
Project: leap_mail Source File: test_mail.py
def _get_parsed_msg(multi=False):
mail_parser = Parser()
raw = _get_raw_msg(multi=multi)
return mail_parser.parsestr(raw)