Giter Site home page Giter Site logo

mimeemailparser's Introduction

MimeEmailParser

MimeEmailParser Swift: Email parsing and validation

Developed for use in Mailio mobile iOS app

What is MimeEmailParser

MimeEmailParser is a Swift 5 library which may be used for parsing and validation of email addresses. If follows Multipurpose Internet Mail Extension (MIME), as defined in RFC 5322 for email address parsing and RFC 2047 ("Q"-encoded) for decoding Non-ASCII text. List of IETF specifications

Note that this is not the full implementation of MIME, but deals only with parsing of email addresses and decoding words as defined in RFC2047.

Supported "high level formats":

address = mailbox / group
mailbox = name-addr / addr-spec
group = display-name ":" [group-list] ";" [CFWS]

The code has been adapted from official GO source; mail package: https://golang.org/src/net/mail/message.go

Features

  • Parsing single and multiple email adresses
  • Validation of email addresses
  • Decoding RFC2047 words

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding MimeEmailParser as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/igorrendulic/MimeEmailParser.git", .upToNextMajor(from: "1.0.0"))
]

Manually

If you prefer not to use Swift Package Manager, you can integrate MimeEmailParser into your project manually as an Embedded Framework

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add MimeEmailParser as a git submodule by running the following command:
$ git submodule add https://github.com/igorrendulic/MimeEmailParser.git
  • Open the new MimeEmailParser folder, and drag the Package.xcworkspace into the Project Navigator of your application's Xcode project.

It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select MimeEmailParser in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • And that's it!

Using MimeEmailParser

MimeEmailParser returns a single or array of Address objects:

public struct Address {
    let Name:String?
    let Address: String
}

Parsing single email address

let address = try MimeEmailParser().parseSingleAddress(address: "[email protected]")

Some of the supported formats (for more examples check MimeEmailParserTests.swift source code)

let address = try MimeEmailParser().parseSingleAddress(address: "[email protected]")
let address = try MimeEmailParser().parseSingleAddress(address: "John Doe <[email protected]>")
let address = try MimeEmailParser().parseSingleAddress(address: "[email protected]")
let address = try MimeEmailParser().parseSingleAddress(address: "[email protected]")
let address = try MimeEmailParser().parseSingleAddress(address: "John !@M@! Doe <[email protected]>") // yes. it's a valid address 

Supports also "Q"-encoded email addresses:

// expected result: Address(Name: "Jörg Doe", Address: "[email protected]")
let address = try MimeEmailParser().parseSingleAddress(address: "=?iso-8859-1?q?J=F6rg_Doe?= <[email protected]>")

// expected result: Address(Name: "Jörg Doe", Address: "[email protected]")
let address = try MimeEmailParser().parseSingleAddress(address: "=?utf-8?q?J=C3=B6rg?=  =?utf-8?q?Doe?= <[email protected]>")

By RFC 5322 only utf-8 and ISO-8859-1 and ASCII is supported but MimeEmailParser doesn't acknowledge those limitations.

Parsing mulitple email addresses

// expected result: [Address(Name: "Mary Smith", Address: "[email protected]"),Address(Name: nil, Address: "[email protected]"),Address(Name: "Who?", Address: "<[email protected]>")]

let addresses = try MimeEmailParser().parseAddressList(addresses: "Mary Smith <[email protected]>, [email protected], Who? <[email protected]>")

// expected results: Address(Name: "André Pirard", Address: "[email protected]")
let addresses = try MimeEmailParser().parseAddressList(addresses: "=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>")

// expected result: [Address(Name: nil, Address: "[email protected]"), Address(Name: nil, Address: "[email protected]"), Address(Name: "John", Address: "[email protected]")] 
let addresses = try MimeEmailParser().parseAddressList(addresses: "Group1: <[email protected]>;, Group 2: [email protected];, John <[email protected]>")

Email Validation

MimeEmailParser can also be used for email validation.

do {
    _ = try MimeEmailParser().parseSingleAddress(address: "John [email protected]")
} catch EmailError.noAngleAddr {
}

Possible email format errors:

Error Description
noAddrSpec No email specified
utf8Invalid Invalid character in address
leadingDotInAtom Detected leading dot
doubleDotInAtom Detected double dots
trailingDotInAtom Detected trailing dot
unclosedQuotedString Unclosed quotes in the name
badCharacter Bad character in address
missingAtInAddrSpace Missing @ symbol
noDomainInAddrSpec Domain not specified
invalidEmailAddress Email could not be parse (unknown)
missingWordInPhrase Missing word in phrase
expectedComma Missing comma in multiple email addresses
noAngleAddr Likely meant to be "Full Name <...>"
unclosedAngleAddr Missing trailing >
commendNotStartedWithParantheses Failed parsing quoted string in comment
misformattedParentheticalComment CFWS validation

Decoding "Q"-Encoded words

// expcted result: This is a horsey: 🐎
let decoded = try rfc2047.decodeRFC2047Word(word: "=?UTF-8?B?VGhpcyBpcyBhIGhvcnNleTog8J+Qjg==?=")

Contributing

The first thing you'll need to do is fork MimeEmailParser to your own GitHub repository. For instructions on how to do that, see the section titled Fork the repository.

Once you've got some changes that you'd like to submit upstream to the official MimeEmailParser repository, send me a Pull Request and I will try to review your changes in a timely manner.

Known Issues

  • failing to parse utf-8 characters in domains and local address parts

License

MimeEmailParser is released under the MIT license. See LICENSE for details.

mimeemailparser's People

Contributors

igorrendulic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mimeemailparser's Issues

Parsing subject MIME Header (RFC2047)

Given a subject line from a Hacker News Digest email:

"=?UTF-8?Q?How_to_be_a_-10x_Engineer_=E2=80=94_Creator_of_Catan,?=\r\n =?UTF-8?Q?_Klaus_Teuber,_has_died_=E2=80=94_and_Finland_becomes_the_31st?=\r\n =?UTF-8?Q?_member_of_NATO?="

I noticed that your RFC2047 word decoder will throw a .notEncoded error.

The sub-clause input.countInstances(of: "?") != 4 appears to be the culprit.

Maybe detecting/splitting on newlines and then parsing them individually is the solution.

Would like to get your thoughts on this before submitting a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.