How to debug notification error: SMTPException sending email

I’m new to HA but not to IT. I have HA Core 2021.12.7 running on HA OS 7.0 (as a VirtualBox VM.)

Having added sensors and devices to HA I now want to use SMTP notifications, and followed the docs to add

notify:
  - name: "fastmail"
    platform: smtp
    sender: "[email protected]"
    recipient: "[email protected]"
    server: mail.messagingengine.com
    port: 587
    encryption: starttls
    user: [email protected]
    password: lsecret fastmail_password
    sender_name: "MY-NAME"
    debug: true

But when I use Developer Tools to test “Notifications: Send a notification with fastmail” (entering a Message and Title, and just to be sure, specifying [email protected] as a target) I receive no email and HA logs in homeassistant.log:

2022-01-23 00:19:12 WARNING (SyncWorker_3) [homeassistant.components.smtp.notify] SMTPException sending mail: retrying connection

The domain is one I own with email hosted at FastMail.com for years. The password is an app password I’m using elsewhere, but just to be sure I tested sending using the same settings above (copied and pasted) via CMail (CMail Command Line Mailer), which I use elsewhere with this account for alerting, and the email was sent successfully.

I specified “debug: true” but can’t find any info on where to find a SMTP debug log.

I’m a Python noob but my read of homeassistant/components/notify/smtp.py suggests that if there was a server or account problem HA would log a different exception before trying to send the message.

How can I debug this?

Thanks!

Start by setting the debug on HA level (and restart), e.g.
add to the logger in configuration.yaml
logs:
homeassistant.components.notify: debug

Thanks for the suggestion. I added that to my existing logger section, which is now

logger:
  default: info
  logs:
    # log level for HA core
    homeassistant.core: warning

    # log level for MQTT integration
    homeassistant.components.mqtt: warning

    # log level for all python scripts
    homeassistant.components.python_script: debug

    # log level for notifications
    homeassistant.components.notify: debug

I also added an explicit timeout: 5 to the SMTP settings, and restarted HA at least twice. But there are at most 2 additional log entries that don’t add any helpful info (to me, at least):

2022-01-23 17:10:46 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Running websocket_api script
2022-01-23 17:10:46 INFO (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Executing step call service
2022-01-23 17:10:48 WARNING (SyncWorker_5) [homeassistant.components.smtp.notify] SMTPException sending mail: retrying connection
2022-01-23 17:10:49 WARNING (SyncWorker_5) [homeassistant.components.smtp.notify] SMTPException sending mail: retrying connection

Any other suggestions?

A long path to finding 2 syntax errors:

  • Looked to see if HA OS might have a firewall blocking outbound email. But eventually found iptables in use, with no outbound rules.
  • Checked the latest HA code and discovered that what I’d found earlier via Google must have been quite old, as the component that had been components/notify/smtp.py was now components/smtp/notify.py. So enabled debug logging for “homeassistant.components.smtp”. But this provided just additional one not-very-useful log entry.
  • Looked into how to override the core smtp component, so I could try to increase the logging in it. Eventually got this working, after figuring out how to override (via /config/custom_components), and how to download from git (one file at a time, using wget with raw URL), and the need to add an arbitrary “version” to manifest.json (not included in core components, but required for custom ones).
  • In the overridden version of notify.py, replace the original “smtplib.SMTPException” exception handler with one for each of the possible exceptions listed in smtplib — SMTP protocol client — Python 3.9.10 documentation.
  • This showed me that the server was returning “SMTP code = 530, SMTP error = ‘5.7.1 Authentication required’” - despite my have configured authentication above.
  • So I added additional logging in notify.py which showed me that the username and password I’d entered weren’t being passed on to the SMTP notification.
  • Which had me look more closely at the notify: entry above, where I found I’d entered “user:” not “username:” (misread or mistyped?) and “password: lsecret” not “password !secret” (definitely misread).

After fixing these the notification is working, at least via Developer Tools.

Much learned, not least that I shouldn’t assume a successful Check Configuration means there aren’t syntax errors.

1 Like

At least you found it … sometimes very small mistakes can produce a lot of problems…e.g. indentation :slight_smile:

Thank you for giving such a detailed writeup, this helped me solve it.

For me the problem was more complicated, I didn’t have the permissions set correctly in AWS’s Simple Email Service I’m using, and was getting an error code 554.

I will probably submit a pull request to add more exception catching with more specific error messages for people in the future.

Edit: I submitted a pull request for the improvement: Add Exception Clauses With Informative Logging for SMTP Notify by ThioJoe · Pull Request #91763 · home-assistant/core · GitHub

Thank you very much, @jd9411 and @TJoe!