【问题标题】:how to make text clickable in python如何在python中使文本可点击
【发布时间】:2019-02-13 18:45:14
【问题描述】:

如何使文本可点击?

class ComplainceServer():
    def __init__(self, jira_server, username, password, encoding='utf-8'):
        if jira_server is None:
            error('No server provided.')

        #print(jira_server)
        self.jira_server = jira_server
        self.username = username
        self.password = password
        self.encoding = encoding

    def checkComplaince(self, appid, toAddress):

        query = "/rest/api/2/search?jql=issuetype = \"Application Security\" AND \"Prod Due Date\" < now()
        request = self._createRequest()
        response = request.get(query, contentType='application/json')
        # Parse result
        if response.status == 200 and action == "warn":
            data = Json.loads(response.response)
            print "#### Issues found"
            issues = {}
            msg = "WARNING: The below tickets are non-complaint in fortify, please fix them or raise exception.\n"
            issue1 = data['issues'][0]['key']
            for item in data['issues']:
                issue = item['key']
                issues[issue] = item['fields']['summary']
                print u"* {0} - {1}".format(self._link(issue), item['fields']['summary'])
                print "\n"
                data = u" {0}  -  {1}".format(self._link(issue), item['fields']['summary'])

                msg += '\n'+ data
            SOCKET_TIMEOUT = 30000 # 30s
            email = SimpleEmail()
            email.setHostName('smtp.com')
            email.setSmtpPort(25)
            email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
            email.setSocketTimeout(SOCKET_TIMEOUT);

            email.setFrom('R@group.com')
            for toAddress in toAddress.split(','):
                email.addTo(toAddress)
            email.setSubject('complaince report')
            email.addHeader('X-Priority', '1')
            email.setMsg(str(msg))
            email.send()

    def _createRequest(self):
        return HttpRequest(self.jira_server, self.username, self.password)

    def _link(self, issue):
        return '[{0}]({1}/browse/{0})'.format(issue, self.jira_server['url'])

这是调用函数。 APPid 和 toAddress 将从不同的 UI 传入。

from Complaince import ComplainceServer

jira = ComplainceServer(jiraServer, username, password)

issues = jira.checkComplaince(appid, toAddress)

我希望 issueid 成为嵌入式链接。

目前邮件发送如下:

MT-4353(https://check.com/login/browse/MT-4353) - Site Sc: DM isg_cq5

但我希望 [MT-4353] 作为 URL https://check.com/login/browse/MT-4353 的超链接

【问题讨论】:

    标签: python python-3.x python-2.7 jython-2.7


    【解决方案1】:

    首先,您需要将您的电子邮件编码为 html。我对您使用的库不熟悉,因此无法举例说明。

    我已经用 html 语法替换了您的代码的 sn-p,只是为了说明您打算使用 html 语法在电子邮件中包含可点击链接。

                msg = "<p>WARNING: The below tickets are non-compliant in fortify, please fix them or raise exception.</p>"
                issue1 = data['issues'][0]['key']
                for item in data['issues']:
                    issue = item['key']
                    issues[issue] = item['fields']['summary']
                    data = u"<a href='{0}'>{1}</a>".format(self._link(issue), item['fields']['summary'])
                    msg += '<br />'+ data
    

    以后,请仔细询问您的问题,因为您的标题不提问并不表示您的实际意思。你也有拼写错误:Compliant

    哦,我错过了self._link(issue) 没有返回正确链接的观点。它返回MT-4353(https://check.com/login/browse/MT-4353),因此您需要提取括号之间的链接部分。我建议使用正则表达式。

    【讨论】:

    • 抱歉,会正确格式化问题,但仍将其视为普通文本。这是我现在看到的输出:

      Blocker:以下票证在 fortify 中不合规


      MT- 3195 - 弱加密
      check.com/login/browse/MT-3196'>MT-3196</a> - 弱加密
    • 代码如下所示: msg = "

      \nBlocker: 以下工单在 fortify 中不符合要求\n

      " issue1 = data['issues'][ 0]['key'] for item in data['issues']: issue = item['key'] issues[issue] = item['fields']['summary'] print u"* {0} - { 1}".format(self._link(issue), item['fields']['summary']) print "\n" linking = "check.com/login/browse" + str(issue) data = u"{1} - {2}".format(linking, issue, item['fields']['summary']) msg​​ += '
      ' + data跨度>
    • 好的,现在您需要在您的 SimpleEmail 实例上找到一个方法,将您的正文设置为 html 内容。您从哪个模块导入 SimpleEmail?
    • from org.apache.commons.mail import EmailException, SimpleEmail, HtmlEmail 这是来自此的导入语句:commons.apache.org/proper/commons-email
    • public Email setMsg(String msg) throws EmailException { if (EmailUtils.isEmpty(msg)) { throw new EmailException("Invalid message provided"); } 其他 { this.setTextMsg(msg); StringBuffer htmlMsgBuf = new StringBuffer(msg.length() + "
      ".length() + "
      ".length()); htmlMsgBuf.append("
      ").append(msg).append("
      "); this.setHtmlMsg(htmlMsgBuf.toString());返回这个; } }
    猜你喜欢
    • 1970-01-01
    • 2016-04-27
    • 2017-11-12
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多