变量数据类型错误?

塞特沃伊

我上课,目的是从Git提交中获取用户名和他的电子邮件:

class BitbucketData(object):

    def get_user_name(self):

        proc = subprocess.Popen("git --no-pager show -s --format='%an'", stdout=subprocess.PIPE)
        committer_name = proc.stdout.read()

        if committer_name:
            return committer_name

    def get_user_email(self):

        proc = subprocess.Popen("git --no-pager show -s --format='%aE'", stdout=subprocess.PIPE)
        committer_email = proc.stdout.read()

        if committer_email:
            return committer_email

它使用,那么发送通知用户(底部工作版本-没有变量-所有senderreceiver数据集明确,不变量-他们在这里评论):

class Services(object):


    def sendmail(self, event):

        repo = BitbucketData()

        #to_address = repo.get_user_email()
        #to_address = '[email protected]'
        #to_name = repo.get_user_name()
        #to_name = 'Test user'

        subject = 'Bamboo build and deploy ready'
        sender = 'Bamboo agent <[email protected]>'

        text_subtype = 'plain'
        message = """
        Hello, {}.

        Your build ready.
        Link to scenario: URL
        Link to build and deploy results: {})

        """.format('Test user', os.environ['bamboo_resultsUrl'])

        msg = MIMEText(message, text_subtype)
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = 'Test user <[email protected]>'

        smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
        smtpconnect.set_debuglevel(1)
        smtpconnect.starttls()
        smtpconnect.login('[email protected]', 'password')
        smtpconnect.sendmail('[email protected]', '[email protected]', msg.as_string())
        smtpconnect.quit()

        print('Mail sent')

        print(repo.get_user_email())

问题是-如果我正在使用变量中的数据(例如,to_address = '[email protected]'或将BitbucketData()类与to_address = repo.get_user_email()-一起使用)-我收到了Office365服务器的错误消息:

...
reply: '250 2.1.0 Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 Sender OK
send: "rcpt TO:<'[email protected]'>\r\n"
reply: '501 5.1.3 Invalid address\r\n'
reply: retcode (501); Msg: 5.1.3 Invalid address
...
  File "C:\Python27\lib\smtplib.py", line 742, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {"'[email protected]'\n": (501, '5.1.3 Invalid address')}

使用变量时,代码如下所示:

class Services(object):


    def sendmail(self, event):

        repo = BitbucketData()

        to_address = repo.get_user_email()
        #to_address = '[email protected]'
        to_name = repo.get_user_name()
        #to_name = 'Test user'
        from_address = '[email protected]'

        subject = 'Bamboo build and deploy ready'
        sender = 'Bamboo agent <[email protected]>'

        text_subtype = 'plain'
        message = """
        Hello, {}.

        Your build ready.
        Link to scenario: URL
        Link to build and deploy results: {})

        """.format(to_name, os.environ['bamboo_resultsUrl'])

        msg = MIMEText(message, text_subtype)
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = to_name

        smtpconnect = smtplib.SMTP('outlook.office365.com', 587)
        smtpconnect.set_debuglevel(1)
        smtpconnect.starttls()
        smtpconnect.login('[email protected]', 'password')
        smtpconnect.sendmail(from_address, to_address, msg.as_string())
        smtpconnect.quit()

        print('Mail sent')

        print(repo.get_user_email())

我在这里(或Microsoft SMTP ...)做错了什么?

UPD

def sendmail(self, event):

    repo = BitbucketData()
    print(repr(repo.get_user_email()))
    ...

给我:

...
Creating new AutoEnv config
"'[email protected]'\n"
send: 'ehlo pc-user.kyiv.domain.net\r\n'
...
阿南德·库玛(Anand S Kumar)

似乎您从git命令接收以下内容作为输出-

"'[email protected]'\n"

您直接将其传递给smtpconnect,这是引起问题的原因,因为这不是有效的电子邮件地址。您可能需要从中获取实际的字符串。获得它的一种方法是为此使用ast.literal_eval()函数。例子 -

>>> import ast
>>> e = ast.literal_eval("'[email protected]'\n")
>>> print(e)
[email protected]

get_user_email()函数返回电子邮件时,您需要执行此操作最有可能committer_name也是这个问题,因此您可能也想这样做。


来自ast.literal_eval()-的文档

ast.literal_eval(node_or_string)

安全地评估包含Python文字或容器显示的表达式节点或Unicode或Latin-1编码的字符串。提供的字符串或节点只能由以下Python文字结构组成:字符串,数字,元组,列表,字典,布尔值和无。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章