pg_smtp_client

使用SMTP从PostgreSQL内发送邮件的客户端扩展

概览

扩展包名版本分类许可证语言
pg_smtp_client0.2.1UTILMITRust
ID扩展名BinLibLoadCreateTrustReloc模式
4170pg_smtp_clientsmtp_client
相关扩展http pg_net pg_html5_email_address gzip bzip zstd pg_curl pgjq

manual updated pgrx by Vonng

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.2.11817161514pg_smtp_client-
RPMPIGSTY0.2.11817161514pg_smtp_client_$v-
DEBPIGSTY0.2.11817161514postgresql-$v-pg-smtp-client-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
d13.x86_64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
d13.aarch64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
u22.x86_64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
u22.aarch64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
u24.x86_64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
u24.aarch64
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1
PIGSTY 0.2.1

构建

您可以使用 pig build 命令构建 pg_smtp_client 扩展的 RPM / DEB 包:

pig build pkg pg_smtp_client         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_smtp_client 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_smtp_client;          # 当前活跃 PG 版本安装
pig ext install -y pg_smtp_client -v 18  # PG 18
pig ext install -y pg_smtp_client -v 17  # PG 17
pig ext install -y pg_smtp_client -v 16  # PG 16
pig ext install -y pg_smtp_client -v 15  # PG 15
pig ext install -y pg_smtp_client -v 14  # PG 14
dnf install -y pg_smtp_client_18       # PG 18
dnf install -y pg_smtp_client_17       # PG 17
dnf install -y pg_smtp_client_16       # PG 16
dnf install -y pg_smtp_client_15       # PG 15
dnf install -y pg_smtp_client_14       # PG 14
apt install -y postgresql-18-pg-smtp-client   # PG 18
apt install -y postgresql-17-pg-smtp-client   # PG 17
apt install -y postgresql-16-pg-smtp-client   # PG 16
apt install -y postgresql-15-pg-smtp-client   # PG 15
apt install -y postgresql-14-pg-smtp-client   # PG 14

创建扩展

CREATE EXTENSION pg_smtp_client;

用法

启用扩展

连接到 PostgreSQL 并执行以下命令。

CREATE EXTENSION IF NOT EXISTS pg_smtp_client CASCADE;

使用 smtp_client.send_email() 函数发送电子邮件。

函数参数

参数类型说明系统配置(可选)
subjecttext邮件主题
bodytext邮件正文
htmlboolean正文是否为 HTML 格式(true)或纯文本格式(false)
from_addresstext发件人邮箱地址smtp_client.from_address
recipientstext[]收件人邮箱地址列表
ccstext[]抄送邮箱地址列表
bccstext[]密送邮箱地址列表
smtp_servertext使用的 SMTP 服务器地址smtp_client.server
smtp_portintegerSMTP 服务器端口smtp_client.port
smtp_tlsboolean是否使用 TLS 加密smtp_client.tls
smtp_usernametextSMTP 服务器登录用户名smtp_client.username
smtp_passwordtextSMTP 服务器登录密码smtp_client.password

默认配置

您可以为上表中标注的部分参数配置系统级默认值,方法如下:

ALTER SYSTEM SET smtp_client.server TO 'smtp.example.com';
ALTER SYSTEM SET smtp_client.port TO 587;
ALTER SYSTEM SET smtp_client.tls TO true;
ALTER SYSTEM SET smtp_client.username TO 'MySmtpUsername';
ALTER SYSTEM SET smtp_client.password TO 'MySmtpPassword';
ALTER SYSTEM SET smtp_client.from_address TO 'from@example.com';
SELECT pg_reload_conf();

使用示例

发送邮件:

SELECT smtp_client.send_email('test subject', 'test body', false, 'from@example.com', array['to@example.com'], null, null, 'smtp.example.com', 587, true, 'username', 'password');

使用已配置的默认值发送邮件:

SELECT smtp_client.send_email('test subject', 'test body', false, null, array['to@example.com']);

或者使用命名参数:

SELECT smtp_client.send_email('test subject', 'test body', recipients => array['to@example.com']);

最后修改 2026-03-08: add extension catalog (baacba6)