2012-08-29 3 views
6

의 필드는,이 필드는 주소를 하나 이상 포함 할 수 있습니다 "로"A "에서"나처럼 보이는, 각 주소는 "[email protected]" 또는 "John D Jr <[email protected]>"구문 분석 이메일 주소 "에"이메일에 루비

처럼 될 수 있습니다

그래서 "에서"필드는 다음 중 하나처럼 보일 수 있습니다 등등

"[email protected]" 

"[email protected], Bob Blue <[email protected]>" 

"Abe Allen <[email protected]>, [email protected]" 

"Abe Allen <[email protected]>, Bob Blue <[email protected]>" 

"Abe Allen <[email protected]>, Bob Blue <[email protected]>, [email protected]" 

하고 있습니다.

이 필드를 구문 분석하고 각 주소의 이메일이 유효한지 추출한 다음 이름이있는 경우 해당 이름을 추출합니다. 전자 메일 표준에 익숙하지 않기 때문에 필자는 주소 필드의 모양을 알 수없는 경우가 있습니다. 이 일을 할 수있는 루비 라이브러리가 있습니까?

+1

전자 메일 주소의 구문 분석 및 유효성 검사는 간단한 작업이 아닙니다. 형식의 범위가 크고 주소가 유효하더라도 가짜이며 배달 가능한 주소가 아닐 수도 있습니다. "[RFC를 읽을 때까지 전자 메일 주소의 유효성을 검증하는 방법을 알고있었습니다] (http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address- until-i.aspx) "에 대한 흥미로운 기사가 ​​있습니다. 또한 [Wikipedia의 기사] (http://en.wikipedia.org/wiki/Email_address)와 [SMTP RFC] (http://tools.ietf.org/html/rfc5322) 자체. –

답변

11

예, 이것에 대한 보석있다; mail이라고합니다.

require 'mail' 

addresses = [] 
raw_addresses = Mail::AddressList.new("Abe Allen <[email protected]>, Bob Blue <[email protected]>, [email protected]") 

raw_addresses.addresses.each do |a| 
    address = {} 

    address[:address] = a.address 
    address[:name] = a.display_name if a.display_name.present? 

    addresses << address  
end 
+1

고마워요, 이건 내 사용 사례에 잘 맞습니다. Btw, 나는 당신이 "raw_addresses.addresses.each"를 할 의도가 있다고 생각하니? – foobar

+0

네, 고마워요. – deefour

0

데이터가 당신이 준 예제를 다음과 가정이 작동합니다 :

def extract_emails(string) 
    string.split(', ').map do |user_string| 
    if user_string.include? '<' 
     user_string =~ /^([^<]*)<([^>]*)>$/ 
     {user: $1.strip, email: $2} 
    else 
     {user: nil, email: user_string} 
    end 
    end 
end 

extract_emails "[email protected]"           
# => [{:user=>nil, :email=>"[email protected]"}] 

extract_emails "[email protected], Bob Blue <[email protected]>"      
# => [{:user=>nil, :email=>"[email protected]"}, {:user=>"Bob Blue", :email=>"[email protected]"}] 

extract_emails "Abe Allen <[email protected]>, [email protected]"      
# => [{:user=>"Abe Allen", :email=>"[email protected]"}, {:user=>nil, :email=>"[email protected]"}] 

extract_emails "Abe Allen <[email protected]>, Bob Blue <[email protected]>"   
# => [{:user=>"Abe Allen", :email=>"[email protected]"}, {:user=>"Bob Blue", :email=>"[email protected]"}] 

extract_emails "Abe Allen <[email protected]>, Bob Blue <[email protected]>, [email protected]" 
# => [{:user=>"Abe Allen", :email=>"[email protected]"}, {:user=>"Bob Blue", :email=>"[email protected]"}, {:user=>nil, :email=>"[email protected]"}] 
+0

OP는 "전자 메일 표준에 익숙하지 않아 주소 필드의 모양을 알 수없는 경우가 있습니다."... –

+0

전자 메일 또는 사용자 이름에 "<" and ">"이 포함되어 있지 않은 한 모든 데이터와 잘. 이들이 포함 할 수 있다면 기본적으로 파서를 작성해야합니다. –

0

도서관에 대해 잘 모르겠지만 전자 메일 목록을 얻으려면 다음 작업을 직접 수행 할 수 있습니다. (목적이 길다)

@a = "Abe Allen <[email protected]>, Bob Blue <[email protected]>, [email protected]" 
b = @a.split(',') #=> ["Abe Allen <[email protected]>", " Bob Blue <[email protected]>", " [email protected]"] 
c = b.collect{|x| x[/<(.*?)>|^([^<>]*)$/]} #=> ["<[email protected]>", "<[email protected]>", " [email protected]"] 
d = c.gsub(/[<>]/,'') #=> ["[email protected]", "[email protected]", " [email protected]"] 

이름과 이메일 주소를 일치 시키려면 다른 것이 필요합니다.

또한 '<'또는 '>'이 (가) 이메일 주소에있는 경우 작동하지 않지만 매우 드뭅니다.