Solved:Is the send email service servered or not?

I can send email with belowing python code:

import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def sendemail(subject="title",content="info"):
    port = 465
    smtp_server = "smtp.qq.com"
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
    password = "xxxx"
    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = sender_email
    message["To"] = receiver_email
    text = content
    part = MIMEText(text, "plain")
    message.attach(part)
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("smtp.qq.com", port, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message.as_string())

The email can be found in [email protected] when to call sendemail() in python console.

Setting in configuration.yaml:

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 465
    timeout: 15
    sender: "[email protected]"
    encryption: starttls
    username: "[email protected]"
    password: xxxxx
    recipient:
      - "[email protected]"
    sender_name: "hass service"

Restart homeassistant service and click the CALL SERVICE.

I have found no email in my [email protected], is the send email service servered or not?

Not sure what you mean. The integration connects directly to the smtp server you specified.

You need to look at, and post your logs :slight_smile:

cat  /home/homeassistant/.homeassistant/home-assistant.log

2023-08-19 09:38:35.258 WARNING (MainThread) [homeassistant.setup] Setup of input_button is taking over 10 seconds.
2023-08-19 09:38:35.590 WARNING (MainThread) [homeassistant.setup] Setup of input_text is taking over 10 seconds.
2023-08-19 09:38:35.591 WARNING (MainThread) [homeassistant.setup] Setup of input_number is taking over 10 seconds.
2023-08-19 09:38:35.592 WARNING (MainThread) [homeassistant.setup] Setup of application_credentials is taking over 10 seconds.
2023-08-19 09:38:35.592 WARNING (MainThread) [homeassistant.setup] Setup of script is taking over 10 seconds.
2023-08-19 09:38:35.593 WARNING (MainThread) [homeassistant.setup] Setup of automation is taking over 10 seconds.
2023-08-19 09:38:39.216 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/smtplib.py", line 398, in getreply
    line = self.file.readline(_MAXLINE + 1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 705, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/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 "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 100, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 162, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/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 401, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed: "
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out

Perhaps because target is not a valid email address. See docs, I think target is

recipient list | string REQUIRED

Default E-mail address of the recipient of the notification. This can be a recipient address or a list of addresses for multiple recipients.
This is where you want to send your E-mail notifications by default (when not specifying target in the service call). Any E-mail address(es) specified in the service call’s target field will override this recipient content.

Where can run?

recipient list | string REQUIRED

It is no use to call as :

You have it set, just don’t set target: in your service call.

So what error do you get now?

Same as before:


2023-08-19 12:22:57.917 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/smtplib.py", line 398, in getreply
    line = self.file.readline(_MAXLINE + 1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", line 705, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/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 "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 100, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 162, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/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 401, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed: "
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out

Make port and protocol matched:

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 587
    encryption: starttls

Or:

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 465
    encryption: tls
1 Like