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
|
#!/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 = "rn".join(headers) session = smtplib.SMTP(server, port) session.ehlo() session.starttls() session.ehlo session.login(sender, password) session.sendmail(sender, recipient, headers + "rnrn" + 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:
Leave a Reply
You must be logged in to post a comment.