How To Send A Captured Image Through Email Using Raspberry Pi, Pi Camera, And Python

Introduction

 
This article demonstrates how to send a captured image through email using Raspberry Pi, Pi camera, and Python.
 
How To Send The Captured An Image Through The Mail 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
  • USB Keyboard
  • USB Mouse
Installation
 
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).
 
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python 
 
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.
  1. hostname -I 
Next, open PuTTY or VNC software and paste the Host Name (or IP Address) on this PuTTY.
 
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python
 
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.
  1. sudo apt-get update
  2. sudo apt-get upgrade
or
  1. sudo apt-get update && upgrade
If you want to install Python 3.0
  1. sudo apt-get install python3
Install an SMTP service 
  1. sudo apt-get install ssmtp
Configure the SMTP
  1. 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.
 
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python
 
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.
  1. from picamera import PiCamera  
  2. from time import sleep  
  3. camera = PiCamera()  
  4. camera.start_preview()  
  5. sleep(05)  
  6. camera.stop_preview() 
Save with Ctrl + S and run with F5. The camera preview should be shown for 05 seconds, and then close. 
 
or
 
Run the program.
  1. sudo python camera.py 
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.
 
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python
 
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“.
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python
 
After this, change the code for file attachment format. The code is given below.
  1. import smtplib,ssl  
  2. from picamera import PiCamera  
  3. from time import sleep  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.base import MIMEBase  
  6. from email.mime.text import MIMEText  
  7. from email.utils import formatdate  
  8. from email import encoders  
  9.   
  10. camera = PiCamera()  
  11.   
  12. camera.start_preview()  
  13. sleep(5)  
  14. camera.capture('/home/pi/image.jpg')     # image path set
  15. sleep(5)  
  16. camera.stop_preview()  
  17. def send_an_email():  
  18.     toaddr = '[email protected]'      # To id 
  19.     me = '[email protected]'          # your id
  20.     subject = "What's News"              # Subject
  21.   
  22.     msg = MIMEMultipart()  
  23.     msg['Subject'] = subject  
  24.     msg['From'] = me  
  25.     msg['To'] = toaddr  
  26.     msg.preamble = "test "   
  27.     #msg.attach(MIMEText(text))  
  28.   
  29.     part = MIMEBase('application'"octet-stream")  
  30.     part.set_payload(open("image.jpg""rb").read())  
  31.     encoders.encode_base64(part)  
  32.     part.add_header('Content-Disposition''attachment; filename="image.jpg"')   # File name and format name
  33.     msg.attach(part)  
  34.   
  35.     try:  
  36.        s = smtplib.SMTP('smtp.gmail.com'587)  # Protocol
  37.        s.ehlo()  
  38.        s.starttls()  
  39.        s.ehlo()  
  40.        s.login(user = '[email protected]', password = '*********')  # User id & password
  41.        #s.send_message(msg)  
  42.        s.sendmail(me, toaddr, msg.as_string())  
  43.        s.quit()  
  44.     #except:  
  45.     #   print ("Error: unable to send email")    
  46.     except SMTPException as error:  
  47.           print ("Error")                # Exception
  48.   
  49. 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 
 
How To Send The Captured An Image Through The Mail Using Raspberry Pi, Pi Camera, And Python
 

Summary 

 
Finally, we have successfully sent the captured image through email using Raspberry Pi, Pi Camera, and Python.