-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function that parses an RFC5322 email address string #67
Comments
I tried to write the function myself: {-# LANGUAGE NamedFieldPuns #-}
module Email where
import Text.Parsec (parse)
import Text.Parsec.Rfc2822 (NameAddr(..), name_addr)
import Network.Mail.Mime (Address(..))
import Data.Text (pack)
import Data.List (elem)
mapMaybe :: (a -> b) -> Maybe a -> Maybe b
mapMaybe _ Nothing = Nothing
mapMaybe f (Just a) = Just (f a)
-- | Given a string email address, spits out an Address record
parseEmailAddress :: String -> Maybe Address
parseEmailAddress str =
case parse name_addr "" addr of
Left _ ->
Nothing
Right (NameAddr { nameAddr_name, nameAddr_addr }) ->
Just (Address (mapMaybe pack nameAddr_name) (pack nameAddr_addr))
where
-- Standardize the email address
addr = if '<' `elem` str then str else "<" ++ str ++ ">"
|
Probably you want some variant of check function: https://hackage.haskell.org/package/check-email-1.0.2/docs/Network-Email-Check.html ? |
@psibi the function I wrote just returns a |
TLDR I agree that it would be handy to have a smart constructor like this, but we have a responsibility to do it well or not at all.The situation is a little unfortunate since we have at least three types for an e-mail address on Hackage: from
I think the right way forward is to talk to the maintainers of the libraries linked above (@peti and @Porges) and propose a convergence to a unified type for an e-mail address. Then @psibi What do you think? |
I think it would be very handy to have a function that does exactly the opposite action of the
renderAddress
function. So you give the function an email address (format specified in RFC5322) and the function spits out anAddress
value.Application:
I can't write this function myself as I am new to Haskell.
The text was updated successfully, but these errors were encountered: