删除电子邮件地址的名称部分 - &#34; John Smith <jsmith@email.com>&#34;

时间:2017-12-22 13:44:40

标签: python

我的应用引擎应用会收到电子邮件,并将发件人的电子邮件地址添加到我的数据存储区,但它采用以下格式:

John Smith <jsmith@email.com>

是否有从这个字符串中删除名称和括号所以它只是显示jsmith@email.com

如果它有帮助,这是我的传入邮件处理程序:

import webapp2
import logging
from google.appengine.ext.webapp import mail_handlers
from google.appengine.api import mail
import os
from main import WorkRequest


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)

        wr = WorkRequest()

        wr.email = message.sender
        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)

1 个答案:

答案 0 :(得分:1)

请尝试添加此代码:

email= "John Smith <jsmith@email.com>"
def changeEmail(email):
    """
   A vary simple method to modify a email to the wanted email address.
        input "John Smith <jsmith@email.com>" -> output "jsmith@email.com"
    """

    email=email.split("<")  #email= ['John Smith ', 'jsmith@email.com>'
    email=email[1]          # email=jsmith@email.com> .  need to remove >
    email=email[:-1]        # email= jsmith@email.com
    return  email

email= changeEmail(email) #email= jsmith@email.com

这种方法会带给你这个结果 输入:"John Smith <jsmith@email.com>" 输出:jsmith@email.com