1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2013 LEAP
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 Utility functions for email.
24 def get_email_charset(content, default="utf-8"):
26 Mini parser to retrieve the charset of an email.
28 :param content: mail contents
29 :type content: unicode or str
30 :param default: optional default value for encoding
31 :type default: str or None
33 :returns: the charset as parsed from the contents
36 if isinstance(content, unicode):
37 content.encode("utf-8", "replace")
41 em = email.message_from_string(content)
42 # Miniparser for: Content-Type: <something>; charset=<charset>
43 charset_re = r'''charset=(?P<charset>[\w|\d|-]*)'''
44 charset = re.findall(charset_re, em["Content-Type"])[0]
45 if charset is None or len(charset) == 0: