Mail gem - how to clean up the body string

前端 未结 4 1864
失恋的感觉
失恋的感觉 2021-02-05 19:06

I\'m trying to read an email using ruby mail gem. But mail.body.decoded returns me not just the body message. How can I clean up this body message and remove unwan

相关标签:
4条回答
  • 2021-02-05 19:34

    looks like you've got a multipart email, so you can use mail.parts[0].body.decoded These will probably come in handy too: mail.multipart?
    mail.parts.length

    The gem documentation at github is pretty decent

    0 讨论(0)
  • 2021-02-05 19:39

    If you have a properly formatted email, you can use Mail helper methods:

    mail = Mail.new(email_string)
    mail.text_part # finds the first text/plain part
    mail.html_part # finds the first text/html part
    

    This doesn't always work if you have e.g. single part messages (text only) or receive email from the internet at large since you can't rely on formatting from every client out there. Believe me, I've learned the hard way.

    0 讨论(0)
  • 2021-02-05 19:39

    Add the mail gem and just use email body format with mail.parts[1].body.decoded.

    0 讨论(0)
  • 2021-02-05 19:45

    With the mail gem, you can do:

    text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`
    
    0 讨论(0)
提交回复
热议问题