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" <john.smith@example.com> into ("John Smith" . "john.smith@example.com"). 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.")
  • 1
    Have a look at the aliases defined in Emacs sources under lisp/mail/mail-parse.el. – Wojciech Gac Aug 3 '17 at 12:34
  • 1
    Excellent! ietf-drums-parse-address is what I needed. If you answer, I'll accept :) – Felipe Aug 9 '17 at 10:21
  • Glad I could help ;). – Wojciech Gac Aug 9 '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 9 '17 at 15:17
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.