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. camera = PiCamera()
  10. camera.start_preview()
  11. sleep(5)
  12. camera.capture('/home/pi/image.jpg') # image path set
  13. sleep(5)
  14. camera.stop_preview()
  15. def send_an_email():
  16. toaddr = '[email protected]' # To id
  17. me = '[email protected]' # your id
  18. subject = "What's News" # Subject
  19. msg = MIMEMultipart()
  20. msg['Subject'] = subject
  21. msg['From'] = me
  22. msg['To'] = toaddr
  23. msg.preamble = "test "
  24. #msg.attach(MIMEText(text))
  25. part = MIMEBase('application', "octet-stream")
  26. part.set_payload(open("image.jpg", "rb").read())
  27. encoders.encode_base64(part)
  28. part.add_header('Content-Disposition', 'attachment; filename="image.jpg"') # File name and format name
  29. msg.attach(part)
  30. try:
  31. s = smtplib.SMTP('smtp.gmail.com', 587) # Protocol
  32. s.ehlo()
  33. s.starttls()
  34. s.ehlo()
  35. s.login(user = '[email protected]', password = '*********') # User id & password
  36. #s.send_message(msg)
  37. s.sendmail(me, toaddr, msg.as_string())
  38. s.quit()
  39. #except:
  40. # print ("Error: unable to send email")
  41. except SMTPException as error:
  42. print ("Error") # Exception
  43. 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.