×

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.


Related Topics

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Python hasattr() function

Python hasattr() function The hasattr() function in Python returns a Boolean value ‘True’ if the given object has the specified attribute, else it returns False. Syntax hasattr(object, name) Parameter object: it is a required parameter which represents an object. attribute:...

1 minute read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Yield Statement In Python

The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns back the...

2 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.