Python email utils
Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny.
In this article, we will talk about a handling package called "email.utils" of a python library called email package. This package is mainly for managing emails.
Email.utils is a collection of python tools and scripts to search and praise mail-box email files, like changes in date and time, changing the strings in the mail, and finding the message ID. There are a few useful utilities provided in the email.util module they are
1) email.utils.localtime(dt=none)
Through this function, it returns local time like a familiar DateTime object. Instead of dt, we use DateTime, which is converted to the time zone according to the system time zone of that database. For this function, the values must be positive or zero to get an accurate local-time, but a negative value might affect the local time's accuracy.
2) email.utils.make_msgid(idstring=none, domain=None)
This function returns a string suitable to an RCF 2822 complaint message ID header. If an idstring is given, then that string is used to strengthen the identity of the message ID. And if the domain name is provided, then it provides the msgid.
3) email.utils.quote(str)
This function returns a new string containing where two backslashes replace backslashes in the string, and backslash-double quotes replace the double quotes.
4) email.utils.unquote(str)
This function returns a new string which was an unquoted string. If that string ends and starts with double quotes, they are removed. In the same way, if the string starts or ends with angle brackets, they are released.
5) email.utils.parseaddr(address)
Parse address is the value of the addresses like Cc-, Bcc- in the email. So this function returns a tuple containing information in which a case-2 tuple of (",") unless the parse fails.
6) email.utils.formataddr(pair)
This function is the inverse of the parseaddr(), so it takes a case-2 tuple from the name part of the email and returns the string value containing the address information like Cc and Bcc header. If the first element is false, then the unmodified 2nd element is returned.
7) email.utils.getaddresses(fieldvalues)
This function returns a list of 2 tuples of the same function as parseaddr(). Feildvalues are a sequence of header values that will be returned.
8) email.util.parsedate(date)
This function tries to parse a date according to rules (RCF 2822), as some mailers won't follow a proper format. So this function tries to make correct changes to that email. The format must be In day-date-time format. Else the returning tuple will not be used.
9) email.utils.parsedate_tz(date)
This function is similar to the parsedate(), but this returns either none or a 10 tuple; the first 9 elements make up a tuple that could be sent directly to mktime(), and the last element is the offset of the time-zone.
10) email.utils.mktime_tz(tuple)
This function turns the 10-tuple of parsedate_tz() into a UTC timestamp. If the tuple time zone is none, then it assumes local time.
11) email.utils.formatdate(timeval=none, localtime =False, usegmt=False)
This function returns a date string as per the day-date-time format. If the primeval is given, it uses a floating point time value as accepted by time.gmtime() and time.localtime(), or the current time is used. Also, if localtime is a flag is given and interprets timeval and returns a date relative to the local time zone, then it takes daylight time.
12) email.utils.decode_rfc2231(s)
This function decodes the string according to the rule RFC 2231.
13) email.utils.encode_rfc2231(s, charset=None, language=none)
This function encodes the string according to the rule RFC 2231. If charset and language are given, each character sets a name and language name to use. If none is given, then the string is returned the same, and if only the charset name is given but not the language, then the string is encoded by the empty string to get the language name.
14) email.util.collapse_rfc2231_value(value,erros=’replace’, fallback_charset= ‘us-ascii’)
This function is used when a header is encoded in RFC 2231 format. Then message.get_param function may return a 3-tuple containing a character set, language, and value, then this function turns into a Unicode string. If errors are passed, then it defaults to replace.
These are a few valuable utilities provided by the email.utils module to modify the email. This module to no way related to the sending of an email. Somewhat it only changes the body and structure of the email.