Hi there, welcome to my blog. As you stumbled upon this post I hope you are finding new ways to impress your loved ones. Being a programmer it’s really hard to get a girl (I feel you, trust me.) sometimes. But after implementing this in your love life you will definitely have your edge over it, trust me (at least that was something I hoped *facepalm*). So before we start let’s talk about what we are going to do here.
We will be fetching love SMS from a website and email those messages to our desired email address (in this case to our girl). In geeky terms the whole tutorial will be on
1) Web Scrapping
2) Email Bombing
We will be using following python modules for our both purposes.
from bs4 import BeautifulSoup import urllib3 import smtplib import mimetypes import sys import time from email.mime.text import MIMEText
So before we go deep let’s talk about web scrapping first. Web Scrapping is a process where we process web pages and extract useful data from it.Web Scrapping is a vast field and a pretty serious field but here we will be witnessing .01% of it here.There are separate courses on Web Scrapping only, so you can guess the complexity and robustness of it pretty well I hope. We will be using BeautifulSoup here which is python module for Web Scrapping.
You can use any web page which provides Love SMS you want but here I will be using this link as it HTML code is well structured and yeah of course I am super lazy and it came at the top of Google search result.
DISCLAIMER: The content of the web page mentioned above is property of the website. This script just uses the data
for demonstration purposes only not for any kind of commercial usage.
So before we start programming in Python let’s have a look at the source code of the page we are gonna scrap. If you are using google chrome then right click anywhere on the page and click ‘view page source‘.There you will see the messages wrapped around some p tags. What we want is the text lying between the p tags. We will tell BeautifulSoup first to give us all the p tags in the HTML code and after that we will loop through it and will access the text of it. The function we are gonna be using is find_all(). It takes the tag name as the argument,in our case it is the p HTML tag.If you can’t find where the messages are in the HTML code and don’t know what I am talking about below is the screenshot of the part of HTML code.
We will be accessing those p tags shown in the screenshot.After getting the messages we will be storing them in a list for future use in our case as the body of the email.The code to do that is as following
http=urllib3.PoolManager() url="http://www.lovelysms.com/cute-love-sms-messages.htm" response = http.request('GET', url) soup = BeautifulSoup(response.data,"lxml") posts=soup.find_all('p') messages= for post in posts: messages.append(post.text)
We will fist open a PoolManager from urllib3 which will open the the connection to the url. We will be using a simple GET request as shown in the code and will store the HTML code in a variable named response.As our response variable now has the HTML code we will tell BeautifulSoup to parse the HTML code with lxml parser.Remember if you omit the second argument to the constructor it will give you error.You have to give it a parser.There are other parsers also but lxml is the most efficient one.Then we will grab all the p elements from the parsed HTML code and store that in a variable named posts.We will also create a second variable named messages which is a list and will contain the texts which we will be sending later as the email body.
That’s all with the web scrapping part.Now the fun part begins which is bombing your Girlfriend’s email inbox with those messages we just scrapped.
We will create a class named SMTP which will contain three functions.
title() function will nothing but printing some shit to let us know that script is working. SMTPconnect() will be the place we will configure our SMTP settings and will login with our email credentials.buildemail() will be the place where we will construct our email and will send the email to our desired email address.
Before I show you the code of all the functions let me inform you the I will be using GMail here and the SMTP settings for GMail is like following
- SMTP Server: smtp.gmail.com
- SMTP Port(TLS): 587
It’s quite possible that you may want to use another mail service.This script will work with all other email services as long as you provide the correct SMTP settings of your Email Service Provider.Now the code for both the functions mentioned above are as following
class SMTP(object): def title(self): print("PYTHON MAIL BOMBER IS WORKING") def SMTPconnect(self): print ("We are in the SMTPconnect") self.smtpserver="smtp.gmail.com" self.smtpport=587 try: self.mailServer = smtplib.SMTP(self.smtpserver,self.smtpport) except IOError as e: print(e) time.sleep(5) sys.exit(1) self.mailServer.starttls() self.username="your username" #Username self.password="your password" #password try: self.mailServer.login(self.username,self.password) except BaseException as e: print(e) time.sleep(3) sys.exit(1)
Here we first create a mail server which will initiate the login process.As arguments we pass our SMTP server name and the port to it.After that we start a TLS connection and create two variables to hold our username and password.
Then we try to login with our credentials and in case any error occurs we print it down in the console and exit the program.Now it’s time to build the email configuration part.In our buildemail() function we will create that as below
def buildemail(self): x = 1 while x < len(messages)-1: print( " We are inside Buildemail ") print ("Building message part") self.From = self.username # From self.To = "Your Girlfriend's Email ID" # TO self.Subject = "I Love You" #Subject self.Message = messages[x] #message mail = MIMEText(self.Message) mail['From']=self.From mail['To']=self.To mail['Subject']=self.Subject self.mailServer.sendmail(self.From, self.To, mail.as_string()) x+=1 print( "sending successful") time.sleep(7) sys.exit()
We wanna send those many emails our messages list contains SMS in it.So we create a while loop and within the loop
we assign our From,To and Subject and the message itself from the list.There you can see a line where we create a object of MIME type. MIME is nothing but Multipurpose Internet Mail Extensions. It's a way of identifying files on the Internet according to their nature and format.There are different kind of MIME objects available in python but what we are gonna use here is MIMEText as our email contains only text.We then feed our MIMEText object with our From,To and Subject and lastly we send the mail to our desired email address.After successful sending it prints a successful message on the console and exits the program.
At last we create our SMTP object and call the functions one by one.
s = SMTP() s.title() s.SMTPconnect() s.buildemail()
If you are lazy enough and don't want to write the code by your own or you are script kiddie and want to impress your non techhy girlfriend you can download the source code from my GitHub Repository and fire the script by simply typing
python love_email.py in the command prompt.
Please note that this script is made to work for only the web page I mentioned above.Different web pages have different HTML code structure.So this script will not work with all of them.You need to view the page source and you have to decide how to get to the text fetching.The process will be more or less same but if you need help with the BeautifulSoup I suggest you to read their documentation for further help.
That's all you have to do for now.If you did it correctly run the script from where ever you want and you will see the script working and probably will get a text from your girlfriend stating she loves you too.Think Smart,Spread Love.