×

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

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

Uint8 Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 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 Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

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 Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.