[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

Ruby and Regex and POP

I have 2 questions:

1.) -------------------
How to write a string.split command that will take the following line and return an array of 2 email addresses:

To: "Stuart Smith" <stuart@email.com>, "Community" <comm@email.com>


2.)--------------------
I built a pop3 reader using something very similar to below however, I am looking to store the actual attachment as the mime-type itself like a PDF.  Any help?

   



POP3.start('pop.example.com', 110,
               'YourAccount, 'YourPassword') do |pop|
      n = 1
      pop.mails.each do |popmail|
        File.open("inbox/#{n}", 'w') do |f|
          f.write popmail.pop
        end
        popmail.delete
        n += 1
      end
    end

Open in new window

0
bobbit31
Asked:
bobbit31
  • 4
1 Solution
 
Geert BormansCommented:
I can answer 1) first

email_addresses = '"Stuart Smith" <stuart@email.com>, "Community" <comm@email.com>'

emails = email_addresses.split(/,/).collect{|item| item.gsub(/^.*<([^>]+)>.*$/, '\1')}

0
 
Geert BormansCommented:
1) is a simple split and the collect generates a new array from executing the block on each item of the array generated by the split
the block is a simple regex substitution

2) I am not sure I understand the second part of your question actually
Do you want to safe the attachment and give it an extension based on the mime type?
0
 
Geert BormansCommented:
sorry bobbit I can't agree to your deletion
- I answered the first half of your question, you should award that at least
- I asked for clarification for the second part, you never gave that clarification
0
 
bobbit31Author Commented:
sorry, i don't recall trying to delete this. i agree you deserve points.
0
 
Geert BormansCommented:
no problem,
cheers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now