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

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:

start

This tutorial requires several items:
[list type=”check”]

  • A Raspberry Pi
  • 1 x microSD card with Raspbian Buster installed (2019-7-10)
  • 1 x Power Supply for Pi4 or Pi 3B+ & Earlier
  • 1 x Raspberry Pi Camera
  • A USB Keyboard & Mouse
  • A HDMI compatible monitor
  • A momentary pushbutton
  • Internet access
  • [/list]

    [tutorialStep title=”Step 1 – Get Everything Prepared” image=”https://bc-robotics.com/wp-content/uploads/2016/06/sdcard.jpg” imageLink=”https://bc-robotics.com/wp-content/uploads/2016/06/sdcard.jpg” imgSide=”left”]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. [/tutorialStep]

    [tutorialStep title=”Step 2 – How it works” image=”#” imageLink=”#” imgSide=”right”] 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![/tutorialStep]

    [tutorialStep title=”Step 3 – The Starting Code” image=”#” imageLink=”#” imgSide=”right”]
    Our code from the last tutorial worked by sending an email when a button connected to GPIO17 was pressed.

    [python]
    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)

    [/python]
    [/tutorialStep]

    [tutorialStep title=”Step 4 – Add Camera Library” image=”#” imageLink=”#” imgSide=”right”]
    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.

    [python highlight=”5″]
    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)

    [/python]
    [/tutorialStep]

    [tutorialStep title=”Step 5 – Set up Camera” image=”#” imageLink=”#” imgSide=”right”]
    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.

    [python highlight=”8-10″]
    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)

    [/python]

    • (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)

    [/tutorialStep]

    [tutorialStep title=”Step 6 – Take Photo” image=”#” imageLink=”#” imgSide=”right”]

    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.

    [python highlight=”53-54″]
    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)

    [/python]

    • (56) Our file name (and storage location) as a variable
    • (57) Capture the image and save at our predefined location

    [/tutorialStep]

    [tutorialStep title=”Step 7 – Import MIME Libraries” image=”#” imageLink=”#” imgSide=”right”]

    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.

    [python highlight=”6-8″]
    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)

    [/python]

    • (6) Import MIMEMultipart
    • (7) Import MIMEText for the text portion of our email
    • (8) Import MIMEImage for the image attachment portion of our email

    [/tutorialStep]

    [tutorialStep title=”Step 8 – Update Arguments for Emailer” image=”#” imageLink=”#” imgSide=”right”]

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

    [python highlight=”32,61″]
    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)

    [/python]

    • (35) Update our Emailer class to accept the image path and filename
    • (64) Add our image path/filename

    [/tutorialStep]

    [tutorialStep title=”Step 9 – Modify Header Generation / Create Email Object” image=”#” imageLink=”#” imgSide=”right”]

    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.

    [python highlight=”35-38″]
    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)

    [/python]

    • (38) Create the email object
    • (39) Set our email subject (the subject line of the email)
    • (40) Set who we are sending this email to
    • (41) Set who is sending this email

    [/tutorialStep]

    [tutorialStep title=”Step 10 – Attach Text ” image=”#” imageLink=”#” imgSide=”right”]

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

    [python highlight=”40-41″]
    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)

    [/python]

    • (44) Attach our text to the email

    [/tutorialStep]

    [tutorialStep title=”Step 11 – Attach Image ” image=”#” imageLink=”#” imgSide=”right”]

    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.

    [python highlight=”43-46″]
    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)

    [/python]

    • (47) Open the image file
    • (48) Create a header to pass along the filename and extension
    • (49) Attach our image to the email

    [/tutorialStep]

    [tutorialStep title=”Step 12 – Update Sendmail ” image=”#” imageLink=”#” imgSide=”right”]

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

    [python highlight=”58″]
    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)

    [/python]

    • (61) Update our email send to use the new email we created as a string

    [/tutorialStep]

    [tutorialStep title=”Step 13 – Test ” image=”#” imageLink=”#” imgSide=”right”]

    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.

    [/tutorialStep]

    [info]Have A Question?
    If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!
    [/info]