SMTP setup with gmail - errors

I’m trying to setup SMTP for the first time in HA. I’ve gone through tons of documentation on smtp, looked up the errors that I’m receiving but just can’t find what is wrong.

After I couldn’t get the setup to work in HA, I wrote a simple AutoHotkey script to test all of the settings that I’m using to connect to my gmail account. Using the ahk script I was able to connect and send an email without any problems.

Here’s what I have in my configuration.yaml:

notify:
  - name: gmail_smtp
    platform: smtp
    sender: [email protected]
    recipient: [email protected]
    server: smtp.gmail.com
    port: 465
    timeout: 60
    username: [email protected]
    password: appKeyPwd
   # encryption: starttls
    #sendername: [email protected]
    verify_ssl: true
    debug: true
# not listed in doc: https://www.home-assistant.io/integrations/smtp/
#starttls: true

note: I’ve tried the above with and without the encryption.

Here are the errors from my log file:

Logger: homeassistant.components.notify
Source: components/smtp/notify.py:148
Integration: Notifications (documentation, issues)
First occurred: 4:29:28 PM (1 occurrences)
Last logged: 4:29:28 PM

Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/notify/legacy.py", line 93, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/smtp/notify.py", line 100, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/smtp/notify.py", line 162, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/smtp/notify.py", line 148, in connect
    mail = smtplib.SMTP(self._server, self._port, timeout=self._timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
                  ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", line 405, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Your help/suggestions would be greatly appreciated!

1 Like

I had this working quite a while ago but it presented unreliable, not sure where the issue was back then, probably my setup. I now went through the setup again and this works for me
Google Mail - Home Assistant (home-assistant.io)

Thanks for the info @vingerha. Does this mean that using smtp with google is never going to work and I should just use the gmail integration?

I used it over 2 year without any problems, and the, last year Google changed something and it stopped working. Now I use my ISP’s mail server.

Seems so…took me 20mins to set it up but it works

@francisp did you switch to using the gmail generated app key? I had to do this with my other emails - camera, website and previous home automation software. With those, once I switched I had no problem - not so with HA though. I just can’t get smtp working with HA with the exact same settings that I use with other software.

Since I can’t get HA smtp working with gmail, I guess I’ll have to go that route. Thanks for the advice @vingerha.

At least they did not add some stupid captcha like other integrations making it further unusable in HA.
The question from my side remains why microsoft is still with the ‘old’ setup and google has all the complexitites. Both are simillarly interested in secure access a.o. so…why the diff?

fyi I did get the gmail api set up and working. Thank heavens there are such complete directions - I REALLY needed them. Thanks again @vingerha for pointing me to them.

With your posts, the question now becomes … did YOU manage to get gmail working outside the one I mentioned above ? I would love a simpler setup but I am not going on a wild goose chase…so …what is your finding?

Sorry for not replying @dmd3 and @vingerha. For some reason I was never notified of your posts.

I gave up on trying to set up SMTP after I set up the Gmail integration as vingerha suggested. I’m not sure whether I had “less secure apps” turned on or not. However I had the account set up, I was able to send emails using the gmail account from other devices (camera, website, etc.) directly - just not through HA.

I know google really discourages the use of “less secure apps” these days so it’s probably better to use the OAuth connection anyway.

I GOT THIS WORKING !!!

I find it hard to believe that what I changed had a difference but I can’t complain about the results.
First I changed the order of everything in the notify command to exactly match the order in the documentation for google mail:

# Example configuration.yaml entry for Google Mail.
notify:
  - name: "NOTIFIER_NAME"
    platform: smtp
    server: "smtp.gmail.com"
    port: 587
    timeout: 15
    sender: "[email protected]"
    encryption: starttls
    username: "[email protected]"
    password: "YOUR_PASSWORD"
    recipient:
      - "[email protected]"
      - "[email protected]"
    sender_name: "SENDER_NAME"

second I changed the port from 465 to 587 (although google is supposed to support 25, 465, or 587)
I still had “less secure apps” turned off
I used the google generated app key since I have 2-step verification turned on.

Now the huge bummer - I can’t send a picture! When I added

              - /config/www/tmp/FloodLightTrigger.jpg

I got an error - extra keys not allowed @ data[images]. BTW, the file does exist and I have the directory listed in allowlist_external_dirs:. I have no problem writing to the directory with the camera snapshot.

Do any of you send attachments using gmail? I really want to be able to send .jpg files from my camera. I wasn’t able to do this with the google mail integration either. I am able to send pictures from my garage camera and doorbell using a gmail account so I’m really surprised I’m not able to do this with HA. Is there any kind of work around? Since this is a separate issue, I’ll do a different post asking about emailing pictures from gmail.

Thank you all for your help. It’s taking me a really long time to get switched from my old home automation system to HA so all of the assistance I’m getting here is so appreciated!