Home » Email module in Python

Email module in Python

by Online Tutorials Library

Email module in Python

The email package is an email message management library. The email package’s overall structure can be broken down into three basic components, plus a fourth component that regulates the behaviour of the others.

The package’s main component is an “object model” that represents email messages. The primary interface via which an application interacts with the package is the object model interface described in the message sub-module. The generator converts an EmailMessage back to a serialised byte stream. (The parser and generator can also handle streams of text characters, but this is discouraged because it’s all too easy to get messages that aren’t valid in some way.)

The policy module is the control component. Every EmailMessage, generator, and parser has a policy object that governs its behaviour. The policy is usually only specified when an EmailMessage is formed, either by directly instantiating an EmailMessage to create a new email or by parsing an input stream via a parser. However, when the message is serialised using a generator, the policy can be altered. This allows, for example, a generic email message to be processed from the disc but serialised when sent to an email server using typical SMTP settings.

The email package keeps the application’s knowledge of the many controlling RFCs to a minimum. The programme should theoretically be able to treat the email message as a hierarchical tree of Unicode text and binary attachments without having to worry about how they are serialised. In practice, however, it is typically necessary to know at least some of the rules controlling MIME messages and their structure, particularly the names and nature of MIME “content types” and how they identify multipart documents.

The main feature of the email module is explained in brief in this article. The policy controls are then discussed, bringing the treatment of the library’s primary components to a close.

The header registry and content manager sub-components, which provide capabilities for more detailed header and payload manipulation, respectively, are next discussed. Both of these components provide functionality for consuming and creating non-trivial messages and documentation for their extensibility APIs, which will be useful for advanced applications.

The above represents the email package’s modern (Unicode friendly) API. Starting with the Message class, the subsequent sections describe the historical compat32 API, which is considerably more intimately concerned with the intricacies of how email messages are represented.

The built-in smtplib module in Python allows you to send emails using the Simple Mail Transfer Protocol (SMTP). For SMTP, smtplib employs the RFC 821 protocol. The Gmail SMTP server will be used in the examples in this lesson, but the same ideas apply to other email services. Although most email providers utilise the same connection ports as those described in this tutorial, you may double-check yours with a simple Google search.

A local SMTP debugging server can help you troubleshoot email issues and ensure that your email operations are bug-free before sending any emails. Set up a Gmail account for development or an SMTP debugging server that rejects emails and prints them to the command prompt instead to get started with this guide. Both possibilities are outlined in the table below.

Now let us look at a sample code:

Code:

Output:

[email protected]:~$ python3 mail1.py   Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  1  Enter the email of the sender::  [email protected]  Enter the Password of the sender::  [email protected]    wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  2  Enter the email of the receiver::  [email protected]  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  3  Enter the Subject of the sending email::  Test EMail Sub  Enter the Body of the sending email::  Hi message from Nirnay   wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  4  Enter the Subject of the sending email::  Test Email Subject  Enter the Body of the sending email::  Hi message from Nirnay.  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  6  Sender Mail::  [email protected]  Receiver Mail::  [email protected]mail.com  Email Subject::  Test Email Subject  Email Body::  Hi message from Nirnay.  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  5  Email sent successfully.  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the sending email's subject.  4. To enter the body of the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  7  

Explanation:

In the output of the above-written code, we have executed the program. After executing the code, the user is prompted with seven different options representing seven different functionalities of the code. The various functionalities that are offered to the user via the medium of the multi-option menu are entering the details of the sender, which is the sender email, getting the receiver’s email to whom the email is going to be sent, enter the subject and the body of the email, and in the last most important functionality that is displayed is sending the email for which all the details has been entered. One more option is there to print all the entered details like the sender credentials from which the mail will be sent, receiver’s email, the subject and body of the sending email; with the help of this option, the user can verify the details entered, and in case something faulty is found the user can easily correct it by simply adding that detail again. Eventually saving users from sending any wrong data over the email. And after completing all the operations, the code execution is exited by selecting the last option that will exit the program.

Suppose you want to send an attachment with the python code. The code for the same is:

Code:

Output:

[email protected]:~$ python3 mail2.py   Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  1  Enter the email of the sender::  [email protected]  Enter the Password of the sender::  [email protected]  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  2  Enter the email of the receiver::  [email protected]  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  3  Enter the Subject of the sending email::  Test Email  Enter the Body of the sending email::  This is the body of the sending email by Nirnay !!!          wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  4  Enter the path of the attachment for the sending email::  file_to_attach.txt  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  6  Sender Mail::  [email protected]  Receiver Mail::  [email protected]  Email Subject::  Test Email  Email Body::  This is the body of the sending email by Nirnay !!!  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  5  Email sent successfully.  wanna proceed with the program or want to exit the program [Y/n]  y  Please choose one of the below-listed options::  1. To enter the details of the sender. [Sender email and sender Password]  2. To enter the details of the receiver. [Receiver email]  3. To enter the Subject and Body of the sending email.  4. To enter the attachment for the sending email.  5. To send the email.  6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body.  7. To exit from the code execution.  7  

Explanation:

In the output of the above-written code, we have executed the program. After executing the code, the user is prompted with seven different options representing seven different functionalities of the code. The various functionalities that are offered to the user via the medium of the multi-option menu are entering the details of the sender, which is the sender email, getting the receiver’s email to whom the email is going to be sent, enter the subject and the body of the email, an additional option to specify the attachment that will be associated with the sending email and in the last most important functionality that is displayed is sending the email for which all the details has been entered. One more option is there to print all the entered details like the sender credentials from which the mail will be sent, receiver’s email, the subject and body of the sending email; with the help of this option, the user can verify the details entered, and in case something faulty is found the user can easily correct it by simply adding that detail again. Eventually saving users from sending any wrong data over the email. And after completing all the operations, the code execution is exited by selecting the last option that will exit the program.

Conclusion:

So, in this article, we have understood the email module and have seen various use case scenarios for the same.


You may also like