In today’s world where one in three tasks can be automated, why not build an email sending program which can send a number of emails with personalized content in a single click?
There are many useful libraries available for Python to do this task, which are being constantly being updated by the Python community.
In today’s project, we are going to use the yagmail module, which is specially configured to send emails through your Gmail account.
There other modules such as smtplib, which allow us to send emails using any email address such as Hotmail or RediffMail, but when it comes to simplicity and ease of use, yagmail beats them all. Thus we’ll be using yagmail to perform our tasks.
Installing yagmail
Open your terminal and type pip install yagmail .
If this doesn’t work then try using python -m pip install yagmail .

- First, we import
yagmail. - Then we define a global empty variable
x, which is required in the later part of our code. - Then we create a list which we’ll be using to store our recipients’ data.
Remember, we can also use an Excel sheet for storing data, and by using the pandas module we can read our .xlsx file and get them converted into Dataframe {datatype in pandas} (highly recommended when dealing with large data).
- Then, we register our email (sender’s email) with a password.
Storing your password in a python script is not a great option, and hence we prompt the user to enter their password at the beginning of the program, which we then store in a variable named password.
Sending an attachment along with with the e-mail

- To attach an attachment file to be sent through email, we have to make a variable storing the full file path with the file extension. You can send all types of files such as
.jpeg,.pdf,.png, etc.

- Now we use a for loop to use the different elements of the lists we created earlier.
- The content forms the main body of our email in which we can use as many variables as we want.
There are some other parameters also which we can use.
[• to • subject • contents • attachments • cc • bcc • preview_only • headers ]
Now we are done with our code. Executing this will send emails to all recipients in the list with name, subject and mobile number personalized for each recipient.
import yagmail
x=""
password=input("Enter your password:")
receiver=["anymail1@gmail.com","anymail2@gmail.com","anymail3@gmail.com"] #list storing specific parameters
name=["anirudh","Raj","Aaryak"]
subject=["maths","computer","programming"]
mobile_number=["7777777777","8888888888","9999999999"]
yagmail.register("anirudh1102gautam@gmail.com",password) #saving email address from where mail is going to be send
filename="C:\Users\Asus\Desktop\your_file_path" # anyfile with complete path , to be send as attachment
yag=yagmail.SMTP("anirudh1102gautam@gmail.com")
for x in range(0,3): #loops to iterate all variables
yag.send(
to=receiver[x],
subject="Testing a email script "+name[x],
contents="Hey "+name[x]+" \n How are you? I hope you are doing good!"
" I know you have opted for "+subject[x]+
" and your current mobile number is "+mobile_number[x],
attachments=filename,
)
We hope you were able to successfully run this Python script on your computer. If you had any problems, please leave them in the comments, and we will try to help you out. Thank you for reading this article!