Saturday, July 6, 2013

How to send messages to billions of users on facebook with one simple script

 Although title looks good,but due to facebook's spam preventing algorithm this message will be sent to other folder[rarely people will see them],but if the user is a friend of your's then you can send message directly

This script can get the user data from facebook graph api.
This is written for better understanding of python 
Modules required:BeautifulSoup
Author:Ajay Kumar Medepalli
import smtplib
import email
from email.MIMEMultipart import MIMEMultipart
from email.parser import Parser
from email.MIMEText import MIMEText
import urllib2
#from BeautifulSoup import BeautifulSoup
"""There are two versions of BeautifulSoup.This script is updated with the latest one"""
from bs4 import Beautifulsoup
import time
import random

Algorithm Explaination:
Aim: To send messages to fb users 
To send messages we need to have their usernames. (FB has introduced this messaging system like if i've my username i can receive message from 
    email clients and fb email id would be

FB Graphi API provides us to acess usernames if users have created one.
get_fb_username ==> This function will get you all usernames 
send_mail ==> This functions will send messages to users (Note :  The message will be sent to other folder if you are not a friend )

user_name_array=[]  #List or array to store usernames of facebook
def get_fb_username(id):
        In fb graph api every url doesn't point to a user or json object from which username can be extracted
        There are some urls which we don't need,since we are using range function there will be errors.To avoid errors we are using
        id ==> it's the number.So i'm converting it into string.
        Our urls look like
        open your browser or install RESTFUL api addon and check the url 
        The above url leads you to the Mark Zuckerberg's page which has his details in json 

   "id": "101",
   "first_name": "xx",
   "gender": "male",
   "last_name": "yyy",
   "link": "",
   "locale": "en_US",
   "name": "Xxx YYy",
   "username": "cam"

        soup = BeautifulSoup(url)
        print all_attr
        if(all_attr[gend+9] == 'm'): # just a check to see if user is a male 
        elif (all_attr[gend+9] == 'f'): #checking if the user is a female
            gender = 'female'
            gender="The user didn't specify any gender"
        if all_attr.find('username') != -1: #if there is a username then proceed
            start_quote=all_attr.find('username')+10 #find the first occurence of username
#find the '"' after the username
#generated username and adding to the username
            user_name_array.append(str(user_name)) # adding username to list or array
            print "username ==>"+'\t'+user_name +'\t'+ "gender ==>"+"\t"+gender
            print "\n"
    except urllib2.HTTPError:
The range function in python is so useful . We can generate 1000s of username .I've called the function inside range function.
for i in range(124896015,124896016,1):
#for i in range(startvalue,stopvalue,stepvalue):
print user_name_array
def send_mail():
    random_text=["hi","hello","Nice to meet you","How are you","wassup","hi!!!",'just wanted to say hi']
    server = smtplib.SMTP()
    server.connect('', 587) # for eg. host = '', port = 587
    server.login('', 'password')
    #replace this with u r gmail id
    #password ==> ur gmail password
    fromaddr =''
    for i in range(len(user_name_array)-1):
        msg = email.MIMEMultipart.MIMEMultipart()
        msg['From'] = fromaddr
        msg['To'] = user_name_array[i]
        msg['Subject'] = 'hi'
        #msg.attach(MIMEText('put some custom message.', 'plain')) 

But you need to make few changes
1.change the numbers in the range function,if you've a fast computer then you can start all the way from 4 to 5000000 (Mark zuckerberg has id =4)
2.change the,password with your details
3.Edit the message you want to send aswell
How to use this??
pip install BeautifulSoup ==>Download this module
a.copy and paste this script somewhere on u rcomputer somewhere

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.


  1. this thing no longer works as facebook updated their API

      You can go to this link and create an access token and attach it to the end of the URL as a query string and this works.

    2. How ? could you show me a bit more clearly ?

  2. Facebook remove user_name fields in graph api, so this is no longer helpful