分享

Send Email in pcDuino by Script and Python | pcDuino: mini PC+Arduino

 xiaofenglib 2013-08-22

In this post, we describe how to send email in pcDuino by script and Python.

First let us look at send email by script:

Install the packages:

1
2
3
$sudo apt-get install ssmtp
$sudo apt-get install mailutils
$sudo apt-get install mpack

Next we configure SSMTP:

1
$sudo nano /etc/ssmtp/ssmtp.conf

Now we edit the fields,

1
2
3
4
5
AuthUser=youruserid@gmail.com
AuthPass=userpass
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

To send email, using:

1
$echo "sample text" | mail -s "Subject" username@domain.tld

If we want to send email with attachment,

1
$mpack -s "test" /home/pi/test/somefile.ext username@domain.tld

Now we begin to look at the Python script used to send email:

01
02
03
04
05
06
07
08
09
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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import smtplib
  
server= 'smtp.gmail.com'
port = 587
  
sender = 'sender@gmail.com'
recipient = 'receiver@linksprite.com'
password='password'
subject = 'Greeting from pcDuino'
body = 'Welcome to pcDuino!'
  
"Sends an e-mail to the specified recipient."
  
body = "" + body + ""
  
headers = ["From: " + sender,
           "Subject: " + subject,
           "To: " + recipient,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
headers = "\r\n".join(headers)
  
session = smtplib.SMTP(server, port)
  
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
  
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()

To run the code, copy the above code and save to reportbyemail.py do:

1
$python reportbyemail.py

We will then receive the email:

email

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多