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.
Address:
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
Agra, Ahmedabad, Amritsar, Anand, Anantapur, Bangalore, Bhopal, Bhubaneswar, Chengalpattu, Chennai, Cochin, Dehradun, Malaysia, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Hebbal, Hyderabad, Jabalpur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Khammam, Kolhapur, Kothrud, Ludhiana, Madurai, Meerut, Mohali, Moradabad, Noida, Pimpri, Pondicherry, Pune, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thane, Thiruvananthapuram, Tiruchchirappalli, Trichur, Udaipur, Yelahanka, Andhra Pradesh, Anna Nagar, Bhilai, Borivali, Calicut, Chandigarh, Chromepet, Coimbatore, Dilsukhnagar, ECIL, Faridabad, Greater Warangal, Guduvanchery, Guntur, Gurgaon, Guwahati, Hoodi, Indore, Jaipur, Kalaburagi, Kanpur, Kharadi, Kochi, Kolkata, Kompally, Lucknow, Mangalore, Mumbai, Mysore, Nagpur, Nashik, Navi Mumbai, Patna, Porur, Raipur, Salem, Surat, Thoraipakkam, Trichy, Uppal, Vadodara, Varanasi, Vijayawada, Visakhapatnam, Tirunelveli, Aurangabad
ECIL, Jaipur, Pune, Gurgaon, Salem, Surat, Agra, Ahmedabad, Amritsar, Anand, Anantapur, Andhra Pradesh, Anna Nagar, Aurangabad, Bhilai, Bhopal, Bhubaneswar, Borivali, Calicut, Cochin, Chengalpattu , Dehradun, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Guduvanchery, Gwalior, Hebbal, Hoodi , Indore, Jabalpur, Jaipur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Kanpur, Khammam, Kochi, Kolhapur, Kolkata, Kothrud, Ludhiana, Madurai, Mangalore, Meerut, Mohali, Moradabad, Pimpri, Pondicherry, Porur, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thoraipakkam , Tiruchirappalli, Tirunelveli, Trichur, Trichy, Udaipur, Vijayawada, Vizag, Warangal, Chennai, Coimbatore, Delhi, Dilsukhnagar, Hyderabad, Kalyan, Nagpur, Noida, Thane, Thiruvananthapuram, Uppal, Kompally, Bangalore, Chandigarh, Chromepet, Faridabad, Guntur, Guwahati, Kharadi, Lucknow, Mumbai, Mysore, Nashik, Navi Mumbai, Patna, Pune, Raipur, Vadodara, Varanasi, Yelahanka