Setting up OctoPi (OctoPrint) with Email Notifier yagmail

I found a few guides to this on the internet but none of them worked for me out of the box using the latest (as of today) Octopi image and a Raspberry Pi 3. What I did eventually get to work was the following

  1. Login to your raspberrypi using SSH. I use Bitvise SSH for all my SSH needs.
  2. Run a few commands to make sure your octoprint is setup correctly. The explanation for the lines is as follows. Activate octoprint environment -> install yagmail (probably already installed) -> install keyrings.alt which is needed by yagmail -> activate python -> run the yagmail registration command
    source ~/oprint/bin/activate
    pip install yagmail
    pip install keyrings.alt
    python
    import yagmail
    yagmail.register('youraccount@gmail.com', 'yourpassword')
  3. While you’re here do a test run to make sure you can connect (this will only work for Gmail SMTP servers. It’s possible to get it working with other servers but you would have to modify some source code to get the ports right. Much easier to just use Gmail)
    yagmail.SMTP(user='youraccount@gmail.com')

    You should get back

    <yagmail.yagmail.SMTP instance at ...>
  4. Install “Email Notifier” through the OctoPrint web interface if you haven’t already.
  5. Restart your RaspberryPi – This may seem unnecessary but I highly recommend it. I ran into lots of problems with emails not sending that were fixed by a simple reboot.