E-mail is one of the most popular ways of digital communication. Python has a rich number of built-in libraries for dealing with e-mails. In this chapter, we will learn how to use Python to compose, send, and retrieve e-mails. The following topics will be covered in this chapter:
smtplib
librarypoplib
imapclient
logging
moduleBefore we start composing our first e-mail with the help of Python, let us revisit some of the elementary concepts of e-mail. Often, an end-user uses a piece of software or a graphical user interface (GUI) for composing, sending, and receiving e-mails. This piece of software is known as an e-mail client, for example, Mozilla Thunderbird, Microsoft Outlook, and so on are e-mail clients. The same tasks can be done by a web interface, that is, a webmail client interface. Some common examples of these are: Gmail, Yahoo mail, Hotmail and so on.
The mail that you send from your client interface does not reach the receiver's computer directly. Your mail travels through a number of specialized e-mail servers. These servers run a piece of software called the Mail Transfer Agent (MTA), and its primary job is to route the e-mail to the appropriate destinations by analyzing the mail header, among other things.
Lots of other things also happen en-route, and then the mail reaches the recipient's local e-mail gateway. Then, the recipient can retrieve the e-mail by using his or her e-mail client.
A few protocols are involved in the aforementioned process. The most common of those have been listed here:
Python has three modules, smtplib
, poplib
, and imaplib
, which support SMTP, POP3, and the IMAP protocols respectively. Each module has options for transmitting the information securely by using the Transport Layer Security (TLS) protocol. Each protocol also uses some form of authentication for ensuring the confidentiality of the data.