├── LICENSE ├── README.md └── email_reply_parser.ex /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 thoughtbot, inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### An Elixir module that can be added to your project to parse incoming email 2 | 3 | Copy the module to your project and use it as needed: [email_reply_parser.ex](email_reply_parser.ex) 4 | 5 | ```elixir 6 | # Returns the email body stripped with just the new email 7 | EmailReplayParser.remove_original_email("email_body") 8 | ``` 9 | -------------------------------------------------------------------------------- /email_reply_parser.ex: -------------------------------------------------------------------------------- 1 | defmodule EmailReplyParser do 2 | def remove_original_email(email) do 3 | email 4 | |> remove_quoted_email 5 | |> remove_trailing_newlines 6 | end 7 | 8 | defp remove_quoted_email(body) do 9 | Enum.reduce(reply_header_formats(), body, fn(regex, email_body) -> 10 | match = Regex.split(regex, email_body) 11 | List.first(match) 12 | end) 13 | end 14 | 15 | defp reply_header_formats do 16 | [ 17 | ~r/\n\>?[[:space:]]*On.*?.*\n?wrote:\n?/, 18 | ] 19 | end 20 | 21 | defp remove_trailing_newlines(body) do 22 | Regex.replace(~r/\n+$/, body, "") 23 | end 24 | end 25 | --------------------------------------------------------------------------------