Introduction
This article demonstrates how to send a captured image through email using Raspberry Pi, Pi camera, and Python.
Pi can handle Python IDE 3.0. If you can easily write the mail scripting code, a python script can be used to send a picture via email.
Requirement
I used the following equipment for this Raspberry Pi web server article.
- Raspberry Pi 3
- Minimum 8GB SD Card if you're using Raspberry Pi 3
- Ethernet or Wifi
- Putty
- Pi Camera
- VNC Viewer
Optional
You can learn
the Raspberry Pi installation from my previous article and then follow my instructions to connect the Pi Camera.
Pi Camera
This package provides a pure Python interface to the Raspberry Pi Camera Module for Python 2.7 (or above) or Python 3.2 (or above).
Step 1
First, the Raspberry Pi must be connected to your desktop. Then, connect your wi-fi on your Pi or connect your ethernet. After that, open the terminal of your Operating System using the following code.
Next, open
PuTTY or
VNC software and paste the Host Name (or IP Address) on this PuTTY.
After that, open the terminal box and enter your default PI name and password.
- Username (pi)
- Password (raspberry)
Step 2
Next, we need to update the Raspberry Pi. So, install the latest packages. You can do that using the following commands.
- sudo apt-get update
- sudo apt-get upgrade
or
- sudo apt-get update && upgrade
If you want to install Python 3.0
- sudo apt-get install python3
Install an SMTP service
- sudo apt-get install ssmtp
Configure the SMTP
- sudo nano /etc/ssmtp/ssmtp.conf
Step 3
Next, you need to enable the permissions and options like SSH and Camera. So, go to the Raspberry Pi configuration.
Step 4
Open the Python IDE 2.7 or above 3.2, create a new file and save it as camera.py. It’s important that you do not save it as picamera.py
The Camera testing code is given below.
- from picamera import PiCamera
- from time import sleep
- camera = PiCamera()
- camera.start_preview()
- sleep(05)
- camera.stop_preview()
Save with Ctrl + S and run with F5. The camera preview should be shown for 05 seconds, and then close.
or
Note
The camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview.
Allowing Gmail SMTP Access for Accounts with Standard Authentication
To allow access to Gmail’s SMTP server from your app, you can follow these steps,
- Login to your Gmail account using your username and password.
- From the top right corner go to “My Account“.
- Under the “Sign-in & security” section locate “Connected apps & sites” and click on it.
- Locate “Allow less secure apps” setting and turn it “On“.
After this, change the code for file attachment format. The code is given below.
- import smtplib,ssl
- from picamera import PiCamera
- from time import sleep
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email.mime.text import MIMEText
- from email.utils import formatdate
- from email import encoders
-
- camera = PiCamera()
-
- camera.start_preview()
- sleep(5)
- camera.capture('/home/pi/image.jpg') # image path set
- sleep(5)
- camera.stop_preview()
- def send_an_email():
- toaddr = '[email protected]' # To id
- me = '[email protected]' # your id
- subject = "What's News" # Subject
-
- msg = MIMEMultipart()
- msg['Subject'] = subject
- msg['From'] = me
- msg['To'] = toaddr
- msg.preamble = "test "
-
-
- part = MIMEBase('application', "octet-stream")
- part.set_payload(open("image.jpg", "rb").read())
- encoders.encode_base64(part)
- part.add_header('Content-Disposition', 'attachment; filename="image.jpg"') # File name and format name
- msg.attach(part)
-
- try:
- s = smtplib.SMTP('smtp.gmail.com', 587) # Protocol
- s.ehlo()
- s.starttls()
- s.ehlo()
- s.login(user = '[email protected]', password = '*********') # User id & password
-
- s.sendmail(me, toaddr, msg.as_string())
- s.quit()
-
-
- except SMTPException as error:
- print ("Error") # Exception
-
- send_an_email()
Save with Ctrl + S and run with F5. When you check your mail, you will find that the image has been received.
Output
Summary
Finally, we have successfully sent the captured image through email using Raspberry Pi, Pi Camera, and Python.