BC Robotics

Sending An Email With Attached Photo Using Python And The Raspberry Pi

  PRODUCT TUTORIAL

We have already covered using a Raspberry Pi and Python to send an email containing basic text – but what if you wanted to attach a picture instead? In this tutorial we are going to adapt our code from our basic email tutorial to allow picture attachments to be sent. We will use a Raspberry Pi Camera to capture an image and send the photo out as soon as the button is pressed. Going forwards, this code chunk could be adapted to send when your cookie alarm is tripped, at a specific time of day, or whatever other condition you come up with!

There are a lot of different operating systems out there for the Raspberry Pi, so we are going to focus on the most popular: Raspbian. We are using the version dated: 2019-7-10 (Pi 4 Compatible) available from the Raspberry Pi Foundations’ Download Page. You don’t need to use the Raspberry Pi 4, any Raspberry Pi will do. However, deviating from the recommended operating system version may result in different / additional steps so if you are very new to this, we recommend following the tutorial exactly.

Requirements:

This tutorial requires several items:

0%

Step 1 - Get Everything Prepared

In this short tutorial, we are going to get started with the assumption that you have already set up your Raspberry Pi, installed Raspbian, completed the Tutorial: Sending An Email Using Python On The Raspberry Pi and have your Python3 editor of choice open and ready to go. 

7.6%

Step 2 - How it works

We are going to reuse / modify our existing code from the last tutorial. We are going to add the library for the Pi Camera, take a picture, save it, and send that file as an attachment in our email. This means we need to modify our email sender class as well – this sounds complicated, but it is actually quite easy. Just a reminder: Do not use your normal email address – the email address and password are stored in plain text in your code! 

15.3%

Step 3 - The Starting Code

				
					import smtplib
import RPi.GPIO as GPIO
import time
  
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			

Our code from the last tutorial worked by sending an email when a button connected to GPIO17 was pressed. 

23.1%

Step 4 - Add Camera Library

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			

We are going to assume you have the camera plugged in, and the camera enabled in your Pi settings. If you have not used the camera before, we recommend having a look at the Raspberry Pi Foundation’s Camera Tutorial first.

Start by importing the PiCamera. 

30.7%

Step 5 - Set up Camera

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (8) Create the camera object
  • (9) Set the camera resolution to 5MP (2592 x 1944 pixels)
  • (10) Set the camera framerate to 15 frames per second (required when setting the resolution above 1920 x 1080) 

Next, we are going to set the image size. We are going to use the 5MP Image size as it works with both V1 and V2 Pi Camera modules, along with any other OVR5647 based cameras for the Raspberry Pi. Since we are setting the camera capture size above 1080p, we also need to define the frame rate for it to work.  

38.5%

Step 6 - Take Photo

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (52) Our file name (and storage location) as a variable
  • (53) Capture the image and save at our predefined location

To take a photo is actually very simple with the PiCamera Module. We are going to define our image name and file location. Next, we capture the image with the camera.capture command. The camera will then capture the image and save it at the defined location “/home/pi/Desktop/” as “image.jpg”. Since we have defined the name and location like this, every time we take a new photo it will overwrite the last image. A dynamic image name could be used if you want to keep each image – however we wont go into that in this tutorial. 

46.1%

Step 7 - Import MIME Libraries

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
 
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (6) Import MIMEMultipart
  • (7) Import MIMEText for the text portion of our email
  • (8) Import MIMEImage for the image attachment portion of our email

It was a lot easier to send a plain text email – we need a little more help to send an email that has text content and a file attachment. We will need the MIME Libraries to help create this type of message.

53.8%

Step 8 - Update Arguments for Emailer

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
 
class Emailer:
    def sendmail(self, recipient, subject, content, image):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent, image)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (31) Update our Emailer class to accept the image path and filename
  • (60) Add our image path/filename

We need to update our Emailer Class to accept an image file location in addition to our existing arguments.

61.5%

Step 9 - Modify Header Generation / Create Email Object

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
 
class Emailer:
    def sendmail(self, recipient, subject, content, image):
          
        #Create Headers
        emailData = MIMEMultipart()
        emailData['Subject'] = subject
        emailData['To'] = recipient
        emailData['From'] = GMAIL_USERNAME
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent, image)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (34) Create the email object
  • (35) Set our email subject(the subject line of the email)
  • (36) Set who we are sending this email to
  • (37) Set who is sending this email

Our new email is constructed as a MIME Multipart Object – so we will have to scrap our old headers and do this a little differently. We will create the overall email with its headers and then add our text and image components before sending. Start by removing the old headers code and replace with this new code. 

69.2%

Step 10 - Attach Text

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content, image):
          
        #Create Headers
        emailData = MIMEMultipart()
        emailData['Subject'] = subject
        emailData['To'] = recipient
        emailData['From'] = GMAIL_USERNAME
 
        #Attach our text data  
        emailData.attach(MIMEText(content))
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent, image)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (40) Attach our text to the email

Attaching text is very simple – we don’t need to do anything other than attach our Text.

76.9%

Step 11 - Attach Image

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content, image):
          
        #Create Headers
        emailData = MIMEMultipart()
        emailData['Subject'] = subject
        emailData['To'] = recipient
        emailData['From'] = GMAIL_USERNAME
 
        #Attach our text data  
        emailData.attach(MIMEText(content))
 
        #Create our Image Data from the defined image
        imageData = MIMEImage(open(image, 'rb').read(), 'jpg') 
        imageData.add_header('Content-Disposition', 'attachment; filename="image.jpg"')
        emailData.attach(imageData)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent, image)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (43) Open the image file
  • (44) Create a header to pass along the filename and extension
  • (45) Attach our image to the email

Attaching an image takes a little extra configuration. To ensure the file arrives on the other side with the correct file name and file extension we need to add a header to this component. Without this, the file appears to the recipient as a .dat file – not very practical. 

84.6%

Step 12 - Update Sendmail

				
					import smtplib
import RPi.GPIO as GPIO
import time
 
from picamera import PiCamera
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
#Camera Settings
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.framerate = 15
 
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
  
class Emailer:
    def sendmail(self, recipient, subject, content, image):
          
        #Create Headers
        emailData = MIMEMultipart()
        emailData['Subject'] = subject
        emailData['To'] = recipient
        emailData['From'] = GMAIL_USERNAME
 
        #Attach our text data  
        emailData.attach(MIMEText(content))
 
        #Create our Image Data from the defined image
        imageData = MIMEImage(open(image, 'rb').read(), 'jpg') 
        imageData.add_header('Content-Disposition', 'attachment; filename="image.jpg"')
        emailData.attach(imageData)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, emailData.as_string())
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        image = '/home/pi/Desktop/image.jpg'
        camera.capture(image)
        sendTo = 'anotheremail@email.com'
        emailSubject = "Button Press Detected!"
        emailContent = "The button has been pressed at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent, image)
        print("Email Sent")
 
    time.sleep(0.1)  
				
			
  • (57) Update our email send to use the new email we created as a string

The last thing we need to do is update the send command.

92.3%

Step 13 - Test

With the code above (and your credentials entered) pressing the button will now take a photo and email it to whatever email address you have entered. Test it out by pressing the button – the email should show up in just a moment!

Going forwards, this code can be modified to take a picture and email it out based on whatever pre-defined conditions you wish to set up. The picture size and other camera attributes could also be changed to better suit your application.

100%

3 thoughts on “Sending An Email With Attached Photo Using Python And The Raspberry Pi”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare