2

Is there a function somewhere in emacs already to parse RFC 2822-formatted email addresses? I don't particularly care for the obsolete parts, but would like to be able to parse things like "John Smith" <[email protected]> into ("John Smith" . "[email protected]"). I've gotten the following regex working, but I figured gnus or someone must have done this properly already.

(defconst fov-email-address-regex
  (let* ((cfws `(? (any space)))
         (atom `(any alpha digit "!#$%&*+/=?^_`{|}'~-"))
         (quoted-pair `(: "\"" (* (or (not (any "\"\\")) (: "\\" nonl))) "\""))
         (display-name `(* (or ,cfws ,atom ,quoted-pair)))
         (addr-spec `(group-n 1 (: (+ (any alpha digit ".!#$%&'*+-/=?\^_`{|}~-"))
                                   "@"
                                   (+ (any alpha digit "-")) (+ (: "." (+ (any alpha digit "-")))))))
         (angle-addr `(: ,cfws "<" ,addr-spec ">" ))
         (name-addr `(: (? ,display-name) ,angle-addr))
         (mailbox `(or ,addr-spec ,name-addr))
         (mailbox-list `(: ,mailbox (* (: "," ,mailbox))))
         (group `(: ,display-name ":" (or ,cfws ,mailbox-list) ";" ,cfws))
         (address `(or ,mailbox ,group)))
    (rx-to-string `(: string-start ,address string-end) t))
  "Regex used to parse an RFC 2822 email address.")
Felipe
  • 329
  • 1
  • 8
  • 1
    Have a look at the aliases defined in Emacs sources under lisp/mail/mail-parse.el. – Wojciech Gac Aug 03 '17 at 12:34
  • 1
    Excellent! ietf-drums-parse-address is what I needed. If you answer, I'll accept :) – Felipe Aug 09 '17 at 10:21
  • Glad I could help ;). – Wojciech Gac Aug 09 '17 at 10:40
  • 2
    @Felipe Note that it is preferred to use the alias mail-header-parse-address from mail-parse instead of its current value ietf-drums-parse-address from ietf-drums, as the former comprises a standard interface and the latter may become obsolete in the future. – Basil Aug 09 '17 at 15:17

1 Answers1

2

Have a look at the aliases defined in Emacs sources under lisp/mail/mail-parse.el.

Wojciech Gac
  • 557
  • 2
  • 13