How to add photos to your digital picture frame via email


How to add photos to your digital picture frame via email 1

Do you want to email a picture from your phone to your digital picture frame at home? And allow friends & family to do the same?

Here is a simple solution using a free dedicated Google Gmail account and a Python script running on your digital picture frame.

Once installed, every person who has the email address of your frame and adds the secret hashtag in the subject line can send you images that will be automatically added to your photos repository.

Simple is as simple does

Regularly adding images to the collection on your digital picture frame is a crucial ingredient to enjoying the experience every day a bit more.

I have described a solution to share your digital picture frame image folder with family & friends using Resilio Sync. But sometimes even this is too much hassle, and you want an even simpler solution.

This is why it is helpful to have alternative ways to add images that are both fully automated and require no maintenance.

One approach is to send one or several images to a dedicated digital picture frame email account. The emails have to be marked with a secret hashtag, and only emails with a valid hashtag are scanned. The photos will then be downloaded and added to your frame – without you having to do a thing.

Following this scan, all emails are being moved to a “Processed” folder, including those that did not have a matching hashtag – keeping your inbox always tidy.

I chose not to delete emails but move them to another folder instead because it makes debugging easier should there be any issues. As Gmail grants 15 GB of storage quota, you are unlikely to run out of space.

The following instructions assume that you have read the article “How to set up your Raspberry Pi for your digital picture frame“.

Create a Gmail account and set it up for your frame

Go to Gmail and create a new Email account. Always remember to use a random, unique and long password.

When you are done, log into your account. The first thing, we need to do is to activate IMAP. Under the gearwheel, go to “Settings” and enable IMAP.

How to add photos to your digital picture frame via email 2Next, create a label (this is what folders are called in Gmail) and call it “Processed”.

How to add photos to your digital picture frame via email 3Third, change the security settings so that you can access it from the Raspberry Pi.

How to add photos to your digital picture frame via email 4Go to your account settings and “Allow less secure apps”.

How to add photos to your digital picture frame via email 5And last, send an email from your new digital picture frame email account to your standard email address and reply. This seems to be helpful to show Google at least once that you are not a robot as your digital picture frame email will only receive emails in the future.

That’s it. You have now configured your digital picture frame’s email address.

The Python script to download the images

The Python script does the following:

It checks your Gmail account if you have any new emails. If there are new emails, it will verify if the subject line includes your secret hashtag like “#mypictures”. The hashtag can be anywhere in the subject line.

If new mails with the secret hashtag are being found, the script will check to see if there are any images and will download them into a folder called “attachments” in the Pi home directory. Several images can be included in one email, and all are downloaded.

Just tell your friends & family to send the photos with the highest resolution setting and to not resize them in the email program. That way, you will have the best photo quality on your digital picture frame.

You can copy and paste the following code into a new Python file called get_images_from_emails.py. Or download it here.

  1. #!/usr/bin/env python3
  2. # read emails and detach attachment in attachments directory
  3.  
  4. import email
  5. import getpass, imaplib
  6. import os
  7. import sys
  8. import time
  9.  
  10. detach_dir = '.'
  11. if 'attachments' not in os.listdir(detach_dir):
  12.     os.mkdir('attachments')
  13.  
  14. userName = 'enter-your-gmail-account-name-here'
  15. passwd = 'enter-your-password-here'
  16.  
  17. try:
  18.     imapSession = imaplib.IMAP4_SSL('imap.gmail.com',993)
  19.     typ, accountDetails = imapSession.login(userName, passwd)
  20.     if typ != 'OK':
  21.         print ('Not able to sign in!')
  22.         raise Exception
  23.  
  24.     imapSession.select('Inbox')
  25.     typ, data = imapSession.search(None, 'SUBJECT', '"#mypictures"')
  26.     if typ != 'OK':
  27.         print ('Error searching Inbox.')
  28.         raise Exception
  29.  
  30.     # Iterating over all emails
  31.     for msgId in data[0].split():
  32.         typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
  33.  
  34.         if typ != 'OK':
  35.             print ('Error fetching mail.')
  36.             raise Exception
  37.  
  38.         emailBody = messageParts[0][1] 
  39.         mail = email.message_from_bytes(emailBody) 
  40.  
  41.         for part in mail.walk():
  42.  
  43.             if part.get_content_maintype() == 'multipart':
  44.  
  45.                 continue
  46.             if part.get('Content-Disposition') is None:
  47.  
  48.                 continue
  49.  
  50.             fileName = part.get_filename()
  51.  
  52.             if bool(fileName): 
  53.                 filePath = os.path.join(detach_dir, 'attachments', fileName)
  54.                 if not os.path.isfile(filePath) : 
  55.                     print (fileName)
  56.                     print (filePath)
  57.                     fp = open(filePath, 'wb')
  58.                     fp.write(part.get_payload(decode=True))
  59.                     fp.close()
  60.  
  61.     #MOVING EMAILS TO PROCESSED PART BEGINS HERE
  62.     imapSession.select(mailbox='inbox', readonly=False)
  63.     resp, items = imapSession.search(None, 'All')
  64.     email_ids = items[0].split()
  65.     for email in email_ids:
  66.         latest_email_id = email
  67.  
  68.         #move to processed
  69.         result = imapSession.store(latest_email_id, '+X-GM-LABELS', 'Processed')
  70.  
  71.         if result[0] == 'OK':
  72.             #delete from inbox
  73.             imapSession.store(latest_email_id, '+FLAGS', '\\Deleted')
  74.  
  75.     #END OF MOVING EMAILS TO PROCESSED
  76.  
  77.     imapSession.close()
  78.     imapSession.logout()
  79.  
  80. except :
  81.     print ('No attachments were downloaded')

A few notes:

Enter your Gmail account name (without “@gmail.com”) and your password in

userName = 'enter-your-gmail-account-name-here'
passwd = 'enter-your-password-here'

You can change the download directory by modifying this line

os.mkdir('attachments')

You can change your secret code word by editing

#mypictures

Make sure that you keep the single and double quotes.

typ, data = imapSession.search(None, 'SUBJECT', '"#mypictures"')

Place the script into your Raspberry Pi home directory.

To call this script every hour, you have to modify your crontab file.

In Terminal enter

crontab -e

and add the following line in the editor:

0 * * * * python3 get_images_from_emails.py

Test your setup with a few emails (don’t forget the hashtag in the subject line) and see if it works as expected. You will find your images in the “attachments” folder in the Raspberry Pi home directory.

Are you ready for the next move?

To make this whole process of adding images even more convenient, newly arrived photos  can be automatically verified and cropped to fit the aspect ratio of your digital picture frame. Then, they will be moved to a directory based on the EXIF date the photo was taken.

This automation will be the subject of a forthcoming article.

Wolfgang Männel

Photography has always been a passion of mine. Since 2005, I have looked at countless ideas on how to enjoy beautiful images digitally, making the experience entertaining, amusing, and stimulating. In my other life, I am an entrepreneur, consultant, and bicyclist based near Frankfurt am Main, Germany.

Recent Content