【问题标题】:How to find the body and the subject in a mailto link?如何在 mailto 链接中找到正文和主题?
【发布时间】:2018-04-21 08:23:33
【问题描述】:

我有这个mailto链接:

mailto:email@address.com?&subject=test&body=type%20your&body=message%20here

我想找tosubjectbody

其实我用的是:

uri = URI('mailto:email@address.com?&subject=test&body=type%20your&body=message%20here')

<URI::MailTo mailto:email@address.com?&subject=test&body=type%20your&body=message%20here>

我有 :to : uri.to

但我无法提取subjectbody

你知道怎么做吗?

【问题讨论】:

    标签: ruby uri mailto


    【解决方案1】:

    您可以使用URI::MailTo#headers,它返回一个数组数组:

    uri.headers
    #=> [[], ["subject", "test"], ["body", "type%20your"], ["body", "message%20here"]]
    

    但是,您的 mailto 链接略有损坏。它应该是这样的:

    uri = URI('mailto:email@address.com?subject=test&body=type%20your%0D%0Amessage%20here')
    #                                   ^                            ^
    #                               no '&' here                newline as %0D%0A
    

    这给了:

    uri.headers
    #=> [["subject", "test"], ["body", "type%20your%0D%0Amessage%20here"]]
    

    可以通过assoc访问:

    uri.headers.assoc('subject').last
    #=> "test"
    

    或者被转换成哈希:

    headers = uri.headers.to_h
    #=> {"subject"=>"test", "body"=>"type%20your%0D%0Amessage%20here"}
    

    获取解码值:

    URI.decode_www_form_component(headers['body'])
    #=> "type your\r\nmessage here"
    

    【讨论】:

    • 谢谢您的回答实际上,我目前就是这样做的,但它很难看,而且我认为它不是很稳定,具体取决于格式。 Deplus,我们有一个很棒的工具 (URI),我认为还有更简单的。我希望能够做到uri.subject & uri.body
    • @HugoBarthelemy URI 提供 decode_www_form_component 但我认为您必须自己调用它。我已经相应地更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多