Python Examples

Python Program to Merge Mails

To understand how Python mail merge works, one should be aware of file handling and file I/O in Python. Along with it, one should also understand and be aware of different string methods that are available in Python. 

python mail merge

                                                                                                                                                                                  Source

When one needs to send the same mail content to different people, one does not need to separately send the mail to each person by entering their email address. Instead, only the name of the person and their email address is changed without needing to write the complete message multiple times. 

Mail merging is an infinitely helpful process through which all the names of the people to which a particular mail needs to be sent are merged to the main body of the mail. 

To execute the process of mail merging, first, the file which contains all the names needs to be accessed. Once the names are read, the essential mail content that does not need to be changed will be stored in a variable. 

After this, a loop will be created that will iterate through each name stored in the file. The loop will create a new mail file for each name through which it iterates. 

Source Code to Merge Mails

The code provided for Python mail merge below saves the template of the body of the mail and iterates through the list of mail. It creates a new mail file for each name present in the name file. The content of the mail does not change. However, the name of each person is included in the individual mail file. 

The complete body of the template of the mail is saved in the variable named body. This variable will be called while creating the individual mail files for each name.  

                    

with open (“names.txt”, ‘r’, encoding = ‘utf-8’) as file_with_names:

              with open (“template.txt”, ‘r’, encoding = ‘utf-8’) as body_of_the_mail:

                             body = body_of_the_mail.read ()

                             for name in file_with_names:

                                            mail = “Dear” + name.strip () + “\n” + body

                                            with open (name.strip () + “.txt”, ‘w’m encoding = ‘utf-8’) as mail_file:

                                                           mail_file.write (mail)

In the program for Python mail merge, the names of the people to which the mail needs to be sent are the file names names.txt. Notice that the file is opened in reading mode. After accessing the file for names, the file where the template of the mail is stored is accessed. This file is named template.txt. 

Once the files are open, the FOR loop is used to iterate over each name present in the file. A new mail file is created for each name that is named after the name of the person to which the mail is being sent. 

The strip () method is used to remove any leading and trailing whitespaces that might be present in the name or template file. The \n character is used to move to the next line of the mail. The mail files are created using the write (). Each of these files is opened in write mode. Therefore ‘w’ is mentioned after the file is opened. The content of any file can be changed or modified only when it is opened in write mode. While in reading mode, for which ‘r’ is used, the content of the file can be read but not changed.  

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.