> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/mail/read-mail-code.md).

# 读取邮件代码

读取邮件代码是一个高级操作，帮助脚本自动登录到电子邮件（Gmail、Outlook、Hotmail、Yahoo...）的邮箱，通过安全连接协议IMAP。系统将自动扫描最新邮件，寻找指定发件人的电子邮件，并提取验证码（OTP/代码）以填写到网页上，而无需您手动打开电子邮件标签进行操作。

#### 配置参数解释：

* 电子邮件：填写需要读取验证码的邮箱地址（例如：`gpm_user@gmail.com`）。
* 密码：填写连接密码。

  > ⚠️ 重要注意事项：对于像Gmail这样的高安全性邮件，您不能在这里填写账户的主密码，而必须填写在该电子邮件的安全设置中单独创建的应用密码（App Password）（获取指南见下文）。
* 邮件服务器：邮件服务提供商的IMAP服务器地址。
  * *例如Gmail*：`imap.gmail.com`
  * *例如Outlook/Hotmail*：`outlook.office365.com`
* 发送到的电子邮件包含：包含该短语的关键字或电子邮件地址（用于过滤正确的电子邮件，例如：`noreply@pinterest.com`或`no-reply@pinterest`）。
* 代理：用于登录邮件服务器的IP配置选项，帮助避免在操作大量账户时被服务提供商（如Google、Microsoft）阻止连接。您可以通过三种方式进行设置：
  * 留空：系统将使用原始网络（实际机器的IP）进行连接和下载代码。
  * 填写变量`$profileProxy`：脚本将自动提取当前GPM配置文件分配的代理包进行使用。
  * 自行填写代理：直接输入特定格式的代理字符串（例如：`ip:port`或`ip:port:user:pass`），您希望为此操作指定的代理。
* 代码类型：需要提取的OTP代码的结构格式。系统支持三种选项：
  1. 数字（Number）：当OTP代码完全由连续数字组成时使用（例如：`475997`）。
     * *代码长度*：需要提取的数字串的确切长度（例如，如果代码由6个数字组成，则填写`6`）。
  2. 文本（Text）：当OTP代码包含字母和数字交替，或深藏在电子邮件的HTML结构标签中时使用。
     * *代码元素xpath*：直接指向电子邮件内容中包含OTP代码的标签的XPath路径（例如：`//h1`或`//span[@id="otp"]`）。
     * *代码元素属性*：如果OTP代码直接位于两个标签之间，请填写关键字`text`（例如：`<h1>ABCDEF</h1>` ➔ 填写`text`）。或者如果OTP代码隐藏在标签的属性中，请填写\_属性名称\_（例如：`<h1 code="ABCDEF"></h1>` ➔ 填写`code`）。
  3. 全部（Full）：高级选项，允许系统提取返回的整个电子邮件的文本/HTML源代码并赋值给变量。然后，您可以通过正则表达式或执行JS代码的操作将此变量传递，以便根据需要编写灵活的提取算法（适用于结构极其复杂的OTP邮件）。
* 输出变量：成功获取后保存验证码字符串（或整个电子邮件内容）的变量名称，以便传递到下一个输入操作。

#### 如何启用两步验证（2FA）和获取Gmail的应用密码（App Password）

为了安全，Google阻止所有第三方应用直接使用主密码连接到邮箱。您必须执行以下流程以授权GPM Automate访问IMAP：

**1. 为Google账户启用两步验证（2FA）**

1. 登录您的Google账户，访问账户管理页面：<https://myaccount.google.com>。
2. 在左侧菜单栏中，选择安全性（Security）选项。
3. 向下滚动到\_“您如何登录Google”\_部分，找到两步验证（2-Step Verification）选项。如果处于\_关闭\_状态，请点击并按照说明（手机号码或验证应用的链接）将其切换为启用状态。

**2. 创建并获取应用密码（App Password）**

1. 成功启用两步验证后，您可以访问此处的应用密码（App password）部分：<https://myaccount.google.com/apppasswords>。
2. 应用名称（App name）框中，输入一个便于记忆的名称（例如：`GPM_Automate`），然后点击创建（Create）按钮。
3. 弹出一个小窗口，显示一段由16个字符组成的黄色文本代码（例如：`abcd efgh ijkl mnop`）。
4. 请复制这16个字符（如果有空格，请删除）并直接粘贴到GPM Automate的\_读取邮件代码\_操作配置中的密码框中。

*(注意：对于Outlook/Hotmail账户，您可以在设置 > 安全 > 高级安全选项 > 应用密码中执行相同流程以获取相应的16个字符密码串。)*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/mail/read-mail-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
