Saturday 20 July 2013

How to send Email using Python using SMTP Server


The following tutorial showcases the use of python to send email using Google smtp server. Now if you have a gmail account then the following steps will guide you to achieve our objective to send email using Python:

  1. The library we are going to use is python smtplib. Python's smtplib is a library that supports sending of email using smtp protocol.That is Simple Mail Transfer Protocol.
  2. The smtp protocol requires a smtp server so we are going to use gmail smtp server.
  3. You can also use other free smtp server but i prefer google over any other services..:) Now the following code snippet will show you how to send a simple email using this service.
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls() 
#Now, log in to the google smtp server
server.login("your google e-mail id", "your password")
 
#Create the message to send mail
msg = "\nHello!" 
try:
 server.sendmail("your e-mail", "recipient email ", msg)
except Exception as e:
 print e

Explanation of the Code:

  1. The first line imports the inbuilt smtplib library of python.
  2. Then we tell python that we are using google smtp server on port 587
  3. Next up we call the ehlo command that is a part of ESMTP protocol and this command is used to initiate communication it is pretty much same as helo.
  4. Next we activate the Transport Layer Security for our SMTP connection.
  5. Next we login to google smtp server and then we create a message with hello in it.
  6. Then we call the sendmail() method to send email it takes your email, recipient email, message that we created as argument we put this block under try and except clause to catch the raised exception if any.
  7. Thus we can easily send email using python smtplib.
Now you will be thinking why not open gmail and send email but with python what you can do is send
emails to multiple recipients and you can also use Cron Jobs to send emails in a timely manner
and use this tool extensively to send Marketing and Advertising Emails.

Ads :Online Shopping from eBay www.ebay.com is a wonderful site that allows you to have a unique shopping experience
check out the auctions here



Friday 18 January 2013

Crontab : Automating our bot


After reading the first post you might be thinking that the Bot we just made is not at all automated it is just an application to post tweets.
Don't be disappointed after reading this post you definitely will be able to automate your application.
If you are a unix/linux programmer you might very well about unix cron jobs that allows you to automate any command line program chronologically and thats the sole magic we are looking forward to right know.
First of edit your login.py file as follows:


import twitter
import random as rm
a=['python with php ver1','python with php ver2','python with php ver3','python with php ver4','python with php ver5','python with php ver6','python with php ver7','python with php ver8','python with php ver9','python with php ver10']
api = twitter.Api(consumer_key='your_key',consumer_secret='your_key',access_token_key='your_key',access_token_secret='your_key')
status=api.PostUpdate(a[rm.randrange(1,10)])
print status.text


Here as you can see what we did is that we created a list of post as twitter doesn't allow to post same tweet twice.
Now we will automate this login.py chronologically to post tweets in a timely manner.
Now start your unix system and goto terminal and do the following things:

1.) Type the following command:

crontab -e.

What this command does is it opens the editor for creating a cron job file in your /var/tmp folder.

2.) The basic format of your cron job has following syntax:


       minute hour day month day-of-week command-line-to-execute

This defines the timely format for running your script
You find the reference here and here too

3.) Now I wanted to post a tweet each and every minute of everyday of every month and so on...So I edited my cron file as follows

       */2 * * * * /usr/bin/python /home/maitray/htdocs/login.py
       


Now enter the new line at the end of the file and close it.It will be auto
saved.
Here the last parameter shows the location of your login.py file.

4.) Alls well that ends well just check your twitter account for bots tweets
You can see my twitter page That has all the tweets posted by the bot.

And that concludes the complete tutorial for twitter bots using python.Thanks
And feel free to contact me for any related queries and feedback


Ads : Auctions ,Electronics, Cars, Fashion, Collectibles, Coupons and Much More Online Shopping from eBay www.ebay.com is a wonderful site that allows you to have a unique shopping experience
check out the auctions here


Saturday 5 January 2013

The Python Tutorial for Twitter Bots

A Python Tutorial For Bots With Twitter
If you are a programmer and if even once in your lifetime a thought popped out of your mind to build a python bot for twitter then you have arrived the right place.
Today I will write down the step by step tutorial to build a simple python bot.

  • First Things First Up
  1. As you cannot directly start exercising and you need to warm up first, in the same way you need a twitter application for the purpose of building twitter bot. If you already have an application with all 'read,write and access direct messages' privileges set you can directly skip below steps.
  2. To create an application go to http://dev.twitter.com and sign in with your twitter account
  3. Then click on create new application
  4. Fill out name, description, and website fields also one thing you need to keep in mind that you don't need to specify the callback URL field as no callback is needed for our bot. And then click on create your twitter application.
  5. After the application has been created go to settings tab and click on 'read,write and access direct messages' radio button under Application Type and then click on update this settings(Note: This is one of the most important steps as because if you forget to give all privileges in app you will get exhausted finding out the error after whole set up is ready.)
  6. Now again go to details tab and click on 'create my access token' and now go to oauth tool tab and you can see all the keys and secret ready.Keep it open or copy all the keys and token because you will need it again.
  •  Gearing Up Further
  1. Now I believe you already have python ready on your laptop then you can move along as below else check out How to install python on windows? on this page.
  2. Now download the following following zipped file here and extract it in a suitable folder.
  3. Now using command line in your respective directory enter the following command.
    python setup.py build
    python setup.py install
  4. Now create a new file and name it login.py and enter the following python code in to it.
import twitter 

#enter your consumer key,secret and access token secret,key in below function as parameters 

api=twitter.Api(consumer_key='',consumer_secret='',access_token_key='',access_token_secret='') 

#now using PostUpdate method of the api we can use to post an update on twitter account 

status = api.PostUpdate('My first tweet using python..:):):):)') 
print status.text 
   4. Now go to command line and the respective dir and then run your login.py file.
   You can get all the Api reference here.
Now everything goes well then congrats you have made your first tweet auto updater using python!!
Also you can check your tweet on your twitter page.
The following post shows how to use crontab to automatically update tweets.
   



Ads : Electronics, Cars, Fashion, Collectibles, Coupons and Much More Online Shopping from eBay www.ebay.com is a wonderful site that allows you to have a unique shopping experience Malaysians have it here: www.ebay.com.my For uk:www.ebay.co.uk