15 – Mail and SMS from Python

Why Mail or SMS from Python?

 Consider a situation you have deployed a Raspberry Pi in a remote place. To monitor it, we can send Mail or SMS frequently. It is very simple to send Mail and SMS from python.

SEND EMAIL:

Step 1: Go to https://myaccount.google.com/lesssecureapps to enable permission to send mail from your Gmail account from less secure apps.

Step 2: Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha to allow your account to add a new application.

 

Step 3: Replace the fromaddr, toadddr and password of the code given below to your Gmail account credentials.

CODE FOR PYTHON 2.7:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#MYBTECHPROJECTS
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
 
#Your Email Address
fromaddr = "xyz@gmail.com"
 
#To Email Address
toaddr = "abc@gmail.com"
password=""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
 
#Subject Of Your Mail
msg['Subject'] = "Mail from Python"
 
#Body of your Mail     
body = """
<p>
Test Mail from python. To know how to send SMS from python click <a href=www.mybtechprojects.tech>here</a>.
</p>"""
 
#To send Email
msg.attach(MIMEText(body, 'html'))
 
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

CODE FOR PYTHON 3.6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#MYBTECHPROJECTS
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
#Your Email Address
fromaddr = "xyz@gmail.com"
 
#To Email Address
toaddr = "abc@gmail.com"
password=""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
 
#Subject Of Your Mail
msg['Subject'] = "Mail from Python"
 
#Body of your Mail     
body = """
<p>
Test Mail from python. To know how to send SMS from python click <a href=www.mybtechprojects.tech>here</a>.
</p>"""
 
#To send Email
msg.attach(MIMEText(body, 'html'))
 
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

SEND SMS:

Step 1: Go to www.way2sms.com and create an account.

Step 2: Replace the number, username, password in the code given below with your credentials.

CODE FOR PYTHON 2.7:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import urllib2
import cookielib
from getpass import getpass
from stat import *
import sys
import os
 
message = "Message from Python. Reference for code : www.mybtechprojects.tech"   # Message
number   = ""  #Mobile no
 
username = ""  #username
passwd   = ""  #password
 
message = "+".join(message.split(' '))
 
#GET request to log into the sms site 
url ='http://site24.way2sms.com/Login1.action?'
data = 'username='+username+'&password='+passwd+'&Submit=Sign+in'
 
#For cookies
 
cj= cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 
#Adding header details
opener.addheaders=[('User-Agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120')]
try:
    usock =opener.open(url, data)
except IOError:
    print "error"
     #return()
 
#Send request
jession_id =str(cj).split('~')[1].split(' ')[0]
send_sms_url = 'http://site24.way2sms.com/smstoss.action?'
send_sms_data = 'ssaction=ss&Token='+jession_id+'&mobile='+number+'&message='+message+'&msgLen=136'
opener.addheaders=[('Referer', 'http://site25.way2sms.com/sendSMS?Token='+jession_id)]
try:
    sms_sent_page = opener.open(send_sms_url,send_sms_data)
except IOError:
    print "error"
    #return()
 
print "success" 
#return ()

CODE FOR PYTHON 3.6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import urllib.request
import http.cookiejar
from getpass import getpass
from stat import *
import sys
import os
 
message = "Message from Python. Reference for code : www.mybtechprojects.tech"   # Message
number   = ""  #Mobile no
 
username = ""  #username
passwd   = ""  #password
 
message = "+".join(message.split(' '))
 
#GET request to log into the sms site 
url ='http://site24.way2sms.com/Login1.action?'
data = 'username='+username+'&password='+passwd+'&Submit=Sign+in'
 
binary_data = data.encode('"utf-8"')
 
#For cookies
 
cj= http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
 
#Adding header details
opener.addheaders=[('User-Agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120')]
try:
    usock =opener.open(url, binary_data)
except IOError:
    print ("error")
     #return()
 
#Send request
jession_id =str(cj).split('~')[1].split(' ')[0]
send_sms_url = 'http://site24.way2sms.com/smstoss.action?'
send_sms_data = 'ssaction=ss&Token='+jession_id+'&mobile='+number+'&message='+message+'&msgLen=136'
 
binary_data1 = send_sms_data.encode('"utf-8"')
opener.addheaders=[('Referer', 'http://site25.way2sms.com/sendSMS?Token='+jession_id)]
try:
    sms_sent_page = opener.open(send_sms_url,binary_data1)
except IOError:
    print ("error")
    #return()
 
print ("success" )
#return ()

 

THANK YOU
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.