SMTP
SMTP 平台允许您将来自 Home Assistant 的通知发送给电子邮件收件人。
要在您的安装中启用电子邮件通知,请将以下内容添加到您的 configuration.yaml
configuration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] 文件中:
# 示例 configuration.yaml 条目
notify:
- name: "NOTIFIER_NAME"
platform: smtp
sender: "YOUR_SENDER"
recipient: "YOUR_RECIPIENT"
检查您的电子邮件提供商的配置或帮助页面,以获取正确的 SMTP 设置。需要重新启动 Home Assistant 以应用配置更改。
Configuration Variables
通知接收人的默认电子邮件地址。这可以是接收地址或多个接收地址的列表。
这是您希望默认发送电子邮件通知的地址(在操作中未指定 target
时)。在操作的 target
字段中指定的任何电子邮件地址将覆盖此接收人内容。
在电子邮件头中设置自定义 ‘发件人名称’ (From: 自定义名称 example@mail.com)。
用法
将使用没有空格的名称创建一个通知集成。在上面的示例中,它将被称为 notify.NOTIFIER_NAME
。要使用 SMTP 通知,请在自动化或脚本中引用它,如以下示例所示:
- alias: "每天早上发送电子邮件"
triggers:
- platform: time
at: "08:00:00"
actions:
- action: notify.NOTIFIER_NAME
data:
title: "早上好"
message: "起床吧"
target:
- "morning@example.com"
可选的 target
字段用于指定此特定操作的接收人。如果未使用 target
字段,此消息将发送到默认接收人,指定在 configuration.yaml
中的 smtp 通知器的 recipient
部分。可以通过使用 \r\n
在电子邮件的正文中添加换行符,例如 message: "起床吧\r\n\r\n新的一天开始了!"
另一个示例是在脚本中附加本地存储的图像:
burglar:
alias: "防盗警报"
sequence:
- action: shell_command.snapshot
- delay:
seconds: 1
- action: notify.NOTIFIER_NAME
data:
title: "入侵警报"
message: "公寓出现入侵者!!"
target:
- "my_intruder_alert@example.com"
data:
images:
- /home/pi/snapshot1.jpg
- /home/pi/snapshot2.jpg
可选的 html
字段创建一个自定义文本/HTML 多部分消息,允许完全自由地通过定义 HTML 内容发送丰富的 HTML 电子邮件。如果需要包含图像,可以同时传递两个参数(html
和 images
)。图像将使用图像的基名附加,因此可以在 HTML 页面中以 src="cid:image_name.ext"
的形式包含。
可选的 images
字段将图像附件添加到电子邮件中。如果定义了 html
,图像需要像上面所述(以及下面的示例所示)在消息中内联添加。如果未定义 html
,图像将作为单独的附件添加。
burglar:
alias: "防盗警报"
sequence:
- action: shell_command.snapshot
- delay:
seconds: 1
- action: notify.NOTIFIER_NAME
data:
message: "公寓出现入侵者!!"
data:
images:
- /home/pi/snapshot1.jpg
- /home/pi/snapshot2.jpg
html: >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>入侵者警报</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<style type="text/css">
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
src: local('Open Sans Light'), local('OpenSans-Light'), url(http://fonts.gstatic.com/s/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTZS3E-kSBmtLoNJPDtbj2Pk.ttf) format('truetype');
}
h1,h2,h3,h4,h5,h6 {
font-family:'Open Sans',Arial,sans-serif;
font-weight:400;
margin:10px 0
}
</style>
</head>
<body>
<div class="jumbotron jumbotron-fluid" style="background-color: #f00a2d; color: white;">
<div class="container py-0">
<h1>公寓出现入侵者!!</h1>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-6 px-0">
<img class="rounded" style="width: 100%;"
alt="snapshot1" src="cid:snapshot1.jpg" />
</div>
<div class="col-xs-12 col-md-6 px-0">
<img class="rounded" style="width: 100%;"
alt="snapshot2" src="cid:snapshot2.jpg" />
</div>
</div>
<br>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js"></script>
</html>
要了解有关如何在自动化中使用通知的更多信息,请参阅 开始使用自动化页面。
特定电子邮件提供商配置
以下是一些特定电子邮件提供商的配置示例。 如果您对所需的 SMTP 设置有疑问,请检查您的电子邮件提供商的配置或帮助页面,以获取有关其特定 SMTP 配置的更多信息。
Google 邮件
Google 邮件的示例配置条目。
# Google 邮件的示例 configuration.yaml 条目。
notify:
- name: "NOTIFIER_NAME"
platform: smtp
server: "smtp.gmail.com"
port: 587
timeout: 15
sender: "YOUR_USERNAME@gmail.com"
encryption: starttls
username: "YOUR_USERNAME@gmail.com"
password: "YOUR_APP_PASSWORD"
recipient:
- "RECIPIENT_1@example.com"
- "RECIPIENT_N@example.com"
sender_name: "SENDER_NAME"
Google 有一些额外的保护层,需要特别注意。您必须在通知配置中使用 应用程序专用密码
如果满足以下任何条件,您将无法创建应用程序密码:
- 您的帐户未启用两步验证。
- 您启用了两步验证,但仅添加了安全密钥作为验证机制。
- 您的 Google 帐户注册了 Google 的 高级保护计划
。 - 您的 Google 帐户属于禁用此功能的 Google Workspace。学校、企业或其他组织拥有的帐户都是 Google Workspace 帐户的示例。