Trying to extract just the email address from this format:
John Smith <[email protected]>
I have tried both of the following and it results in the same error:
IndexError: list index out of range
email_address = re.findall('(?<=\<)\w+@[a-zA-Z]+\.[a-z]+(?=\>)', sender)[0]
email_address = re.findall('<([^>])>', sender)[0]
Rest of code:
import webapp2
import logging
from google.appengine.ext.webapp import mail_handlers
from google.appengine.api import mail
import os
from main import WorkRequest
import re
class IncomingMailHandler(mail_handlers.InboundMailHandler):
def receive(self, message):
(encoding, payload) = list(message.bodies(content_type='text/plain'))[0]
body_text = payload.decode()
logging.info('Received email message from %s, subject "%s": %s' %
(message.sender, message.subject, body_text))
logging.info (message.sender)
logging.info(message.subject)
logging.info(body_text)
sender = str(message.sender)
email_address = re.findall('<([^>])>', sender)[0]
wr = WorkRequest()
wr.email = email_address
wr.userId = None
wr.title = message.subject
wr.content = body_text
wr.status = "OPEN"
wr.submission_type = "EMAIL"
wr.assigned_to = "UNASSIGNED"
wr.put()
application = webapp2.WSGIApplication([('/_ah/mail/.+', IncomingMailHandler)],debug=True)
Can anyone please help? I am using Google App Engine with Python if that matters.


print(sender)and add the output to your question?[email protected]out of"John Smith <[email protected]>"John Smith <[email protected]>). The second one works if you add a plus sign after the closing square bracket.