[vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Send Email” tab_id=”1387502637-1-10″][vc_column_text]Python’s smtplib provides a way for send Email. It has carried on the simple to the SMTP protocol encapsulation. SMTP (Simple Mail Transfer Protocol) is the Simple Mail Transfer Protocol, it is a set of used by the source address to the destination address send Mail rules, by which way to control the Transfer of letters. The SMTP protocol is TCP/IP protocol, it helps to each computer on the sending or transit letters to find the next destination.Through the SMTP protocol as specified by the server, you can send E-mail to the recipient’s server, the process takes a few minutes.SMTP server is follow the SMTP protocol to send a mail server, used to send or email from transit.
* the host: SMTP server host name
* the port: SMTP port, the default is 25;If at the time of create SMTP object provides the two parameters (host, port), at the time of initialization will automatically call the connect method to connect to the server.
Described below under smtplib. SMTP provide some of the classes:
* SMTP. Set_debuglevel (level) : if set to debug mode.The default is False, that is, the debug mode, said no output debugging information.
* SMTP. The connect () : the connection to the specified SMTP server.Parameter respectively SMPT host and port.Note: in the host parameters specified in the port number (such as: smpt.yeah.net: 25), so you don’t need the port parameters is given.
* SMTP. Docmd (CMD) : send instructions to the SMTP server.Optional parameters argstring said instructions.
* SMTP. Helo () : confirm your identity using the helo commands to the server.”Who am I” is equivalent to tell the SMTP server.
* SMTP. Has_extn (name) : judge the specified name exists in the server list.For security reasons, SMTP server is often blocked the order.
* SMTP. Verify (address) : to determine whether a specified email address in the server.For security reasons, SMTP server is often blocked the directive.
* SMTP. The login (user, password) : log in to the SMTP server.Now almost all the SMTP server must be legally in validating the user information is allowed to send mail after.
* SMTP. Sendmail (from_addr to_addrs, MSG) : send mail.Attention should be paid to the first three parameters again here, MSG is a string, said.Usually by the title, we know that mail sender, recipient, E-mail content, accessories, etc, send E-mail, should pay attention to the format of the MSG.This format is defined in the SMTP protocol format.
* SMTP. Quit () : disconnection and SMTP server connection
Send Email of process :
1. Connect the STMP server
2. Send the user name and password for authentication
3. Write E-mail information
4. Send mail
5. Quit
Example code:
import smtplib
from email.mime.text import MIMEText
mail_to=”xxxxxx”
#received Email address
def send_mail(to_list,sub,content):
#to_list received address
#sub subject for email
#content Emailof content
mail_host = ”smtp.qq.com”
#qq Emailstmp server address smtp.qq.com
#If you used other Email,you can check the stmp server address
mail_user=”xxxxx”
#enter you Email name
mail_pass=”xxxxx”
#enter you Email password
mail_postfix=”xxx.com”
#enter you Email suffix,here used qq Email,as qq.com,if you uesd 163Email,so:163.com
me=mail_user+”<”+mail_user+”@”+mail_postfix+”>”
msg = MIMEText(content)
msg[‘Subject’] = sub
msg[‘From’] = me
msg[‘To’] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == ’__main__’:
if send_mail(mail_to,”From pcDuino”,”Hello World !”):
print ”send mail successful !”
else:
print ”send mail failed !”
on pcDuino test of run (pcDuino must have network)
must used test code save as “sendEmail.py”files、run code ,we can see send Email complete.
Open purpose Email (mail to)we can see form pcDuino send Email
[/vc_column_text][/vc_tab][vc_tab title=”Received Email” tab_id=”1387502637-2-5″][vc_column_text]Python of poplib modulesi used form pop3received Email.
Below is the poplib provide some classes:
* poplib. POP3 (server) to connect to the mail server
* poplib. User (username) send a user name to the server, waiting for the server are waiting for return information of the user’s password
* poplib pass_ (password) will be the password sent to the server, to log in.If the login fails, will throw an exception.
* poplib stat () returns the length of 2 yuan group: says the number of mailbox mail, E-mail messages respectively of the total size (in bytes).
* poplib. List () returns the length of 3 yuan group, respectively, the server returns the size of the information, the message list, return information.
* poplib retr (messagenumber) returns the length of 3 yuan group, respectively, the server returns information, E-mail specified label all the lines and the size of the mail message (bytes).
* poplib. Quit () to end connection and exit.
Received Email of process
1. Connect to the pop3 server
2. Send the user name and password for authentication
3. Get mailbox mail information
4. Collect the mail
5. Quit
The sample code
import poplib
from email import parser
host = ’pop.xx.com’
username = ’xxxxxxx@xx.com’
password = ’xxxxx’
pop_conn = poplib.POP3_SSL(host)
pop_conn.user(username)
pop_conn.pass_(password)
#Get messages from server:
Messages = [pop_conn.retr(i) for i in range(1,len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = [“\n”.join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print message[‘From’]
print message[‘To’]
print message[‘Subject’]
pop_conn.quit()
Test run on pcDuino
As test code save as “readEmail.py”files run of code
We can see read Email of addresser recipients and theme
[/vc_column_text][/vc_tab][vc_tab title=”Email control GPIO and reply Email” tab_id=”1387506363803-2-2″][vc_column_text]Now we can used Email control GPIO ,First install pcduino-python on the pcDuino. If you don’t install git – core board above will prompt you git command, we install git – the core first, then to install pcduino – python.
Prompt error:
Install git-core
Install pcduino-python test library
In ubuntu check pcduino-python
Everything is ok,,Now we can used Email control GPIO, Is the function of concrete realization: in some GPIO pcDuino connected to an LED, send an email to pcDuino pcDuino change the current state of the LED, and reply to an email to the sender, tell the sender LED destroy the light of the current state.
Code complete ways:
(1) initializes the GPIO, set LED the initial state to destroy;
(2) setting pcDuino service email login information
(3) set email login, see if any new email:
A) : to change the current state and destroy the LED light reply to the sender of the current state of the LED
B) no, the LED current state
Testing the new python code:
(1) to pcduino – python/Samples directory:
(2) copy “blink_led” to the current directory, named “email_test
(3) Enter email-test files,delete blink-led py files、
Save below code as “email-test py”,
import gpio
import sys
import smtplib
from email.mime.text import MIMEText
import poplib
from email import parser
import time
led_pin = ”gpio8″
mail_to=” ”
host = ’pop.qq.com’
username = ’xxxxx@qq.com’
password = ’xxxx’
pop_conn = poplib.POP3_SSL(host)
pop_conn.user(username)
pop_conn.pass_(password)
def delayMicroseconds(us):
time.sleep(1.0*us/1000/1000)
def delay(ms):
time.sleep(1.0*ms/1000)
def send_mail(to_list,sub,content):
mail_host=”smtp.qq.com”
mail_user=”xxxx”
mail_pass=”xxxx
mail_postfix=”qq.com”
me=mail_user+”<”+mail_user+”@”+mail_postfix+”>”
msg = MIMEText(content)
msg[‘Subject’] = sub
msg[‘From’] = me
msg[‘To’] = to_list
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
def ReadEmail():
pop_conn = poplib.POP3_SSL(host)
pop_conn.user(username)
pop_conn.pass_(password)
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
#Concat message pieces:
messages = [“\n”.join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
message = parser.Parser().parsestr(mssg)
#print message[‘From’]
return message[‘From’]
pop_conn.quit()
def Email_checker():
pop_conn = poplib.POP3(host)
pop_conn.user(username)
pop_conn.pass_(password)
ret = pop_conn.stat()
return ret[0]
def setup():
gpio.pinMode(led_pin, gpio.OUTPUT)
gpio.digitalWrite(led_pin, gpio.LOW)
def loop():
new_num = Email_checker()
old_num = new_num
print ’initialization is successful !’
while True:
new_num = Email_checker()
if new_num!=old_num:
if new_num%2== 1:
gpio.digitalWrite(led_pin, gpio.HIGH)
mail_to = ReadEmail()
if send_mail(mail_to,”LED Status”,”The LED status is HIGH !”):
print ”Replay mail successful !”
else:
print ”Replay mail failed !”
else :
gpio.digitalWrite(led_pin, gpio.LOW)
mail_to = ReadEmail()
if send_mail(mail_to,”LED Status”,”The LED status is LOW !”):
print ”Replay mail successful !”
else:
print ”Replay mail failed !”
old_num = new_num
delay(1000);
setup()
while True :
loop()
Lining one LED on the pcDuino ,then run email-test.py
Before does not send Email,LED put out state
Enter other Email ,and to Emial pcDuino on service email.
Now we can see LED is bright,pcDuino already send information to addresser,So send Email will complete
And then send Email to pcDuino ,you can see LED haven’t bright,pcDuino already send Email to addresser ,So all test is OK.[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_column_text]This post tell you how to used send Email and received Email on the pcDuino ,And then uesd Email of control GPIO.So this post have three ways :[/vc_column_text][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.