删除电子邮件地址“John Smith”的名称部分<jsmith@email.com>"

2024-04-26 09:43:44 发布

您现在位置: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)

Tags: textfromimportinfo应用程序message电子邮件email
1条回答
网友
1楼 · 发布于 2024-04-26 09:43:44

请尝试添加此代码:

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

相关问题 更多 >