Let’s say that you have created a web application using one of the python framework technologies such as Django or Flask and you want to extend its functionality by adding a simple email sending program so you can interact with your customers and offer them your promotional products.
In this article, we will use a python library called SMTP to send a bulk of emails using Gmail at the same time for a purpose of promoting products and service to your email subscribers and adding some text images attachments to make it kinda professional.
Before jumping to the code you need first to enable the Less secure app access option inside your Gmail account so the program can access it and send emails.
Click here to know more about Data Science Course in Hyderabad
The good thing is python has the SMTP library installed by default so you don’t need to do any extra installation for that. Starting the code by a simple import statement followed by the library name.
We also need to import another library called EmailMessage so we can send a customized email message to our subscribers.
Now, we need to define some variables that we need in our program so if you are going to change something like changing the SMTP service provider or the port number you won’t have to look and searching for those variables inside your code and also your login credentials.
Now, we will create our message body and subject and some other variables such as our email the receiver email by first importing the email.message library:
Then we will create the subject set our email sender the receiver email and the content of our email by the following code:
The code above shows that we have utilized the same sender email as the receiver email just for demonstration purposes.
Let’s now connect with the SMTP server and start the process of sending emails. You can do that by using the following commands:
The whole code now looks like below:
Now, let’s test the code by run it on your IDE and see your email if you have a message on the inbox folder.
As you see above, the process of sending a simple text email with a tile has been succeeded. Let us now jump into sending an email with a simple image.
First, we have to import a library called imghdr that will determine the type of our image because we need it later to send our file which is an image in this case:
We need to read first the file which is an image in this case before we can use it in our program. Let us do that by a simple code:
First, we need to get the attachment name so it will be displayed. The image name will be displayed and the user doesn’t think that this message is spam and it doesn’t look professional in that way. Then we use the command message.add_attachment() with some parameters to send the file.
The final code looks like this:
Let us run the code and see if everything is working well:
As you see from the picture above, the code worked correctly and the image has been sent to the receiver email inbox. Let us now see how to send an HTML template.
This is a simple step to add an HTML template to our email message. We will use the function message.add_alternative() and passing the HTML code in it:
The final code seems as below:
Let us run the code and see what will happen.
As you see that it worked correctly but the massage that we have set earlier in the message.set_content() has been overwritten and it won’t appear till your browser can’t display or read the HTML template.
There is a simple way to send a bulk of emails which is to create a list variable let’s called it Contacts and adding as much as you want emails then change the Email variable in message[“To”].
Let us see that in code:
The final code looks like below with a change in the message[“To”]:
After running the code you will see the emails have been sent to your emails list.
The Email Sent Successfully To Both Accounts
As you see above in the two pictures, the message has been sent successfully to both emails.
If you are trying to develop a web app and use different email provider which probably this is what you will do then you should use other parameters different on what you saw in this tutorial and some of the services such as sendgrid.com has its SMTP library but the workflow is pretty much the same.
360DigiTMG - Data Analytics, Data Science Course Training Hyderabad
2-56/2/19, 3rd floor,, Vijaya towers, near Meridian school,, Ayyappa Society Rd, Madhapur,, Hyderabad, Telangana 500081
Phone: 099899 94319