PIGSTY

pg_smtp_client

PostgreSQL extension to send email using SMTP

Overview

Attributes

YesNoYesNoYesNoYessmtp_client

Packages

EL
PIGSTY
pg_smtp_client_$v0.2.0-
17
16
15
14
13
Debian
PIGSTY
postgresql-$v-pg-smtp-client0.2.0-
17
16
15
14
13

Comments

pgrx=0.12.7


Availability

PG17PG16PG15PG14PG13
el8.x86_64
0.2.0
0.2.0
0.2.0
0.2.0
el8.aarch64
0.2.0
0.2.0
0.2.0
0.2.0
el9.x86_64
0.2.0
0.2.0
0.2.0
0.2.0
el9.aarch64
0.2.0
0.2.0
0.2.0
0.2.0
d12.x86_64
0.2.0
0.2.0
0.2.0
0.2.0
d12.aarch64
0.2.0
0.2.0
0.2.0
0.2.0
u22.x86_64
0.2.0
0.2.0
0.2.0
0.2.0
u22.aarch64
0.2.0
0.2.0
0.2.0
0.2.0
u24.x86_64
0.2.0
0.2.0
0.2.0
0.2.0
u24.aarch64
0.2.0
0.2.0
0.2.0
0.2.0
CONTRIB
PGDG
PIGSTY

Download

To add the required PGDG / PIGSTY upstream repository, use:

pig repo add pgdg -u    # add PGDG repo and update cache (leave existing repos)
pig repo add pigsty -u  # add PIGSTY repo and update cache (leave existing repos)
pig repo add pgsql -u   # add PGDG + Pigsty repo and update cache (leave existing repos)
pig repo set all -u     # set repo to all = NODE + PGSQL + INFRA  (remove existing repos)
./node.yml -t node_repo -e node_repo_modules=node,pgsql # -l <cluster>

Or download the latest packages directly:


Install

Install this extension with:

pig ext install pg_smtp_client; # install by extension name, for the current active PG version

pig ext install pg_smtp_client -v 17;   # install for PG 17
pig ext install pg_smtp_client -v 16;   # install for PG 16
pig ext install pg_smtp_client -v 15;   # install for PG 15
pig ext install pg_smtp_client -v 14;   # install for PG 14
dnf install pg_smtp_client_17;
dnf install pg_smtp_client_16;
dnf install pg_smtp_client_15;
dnf install pg_smtp_client_14;
apt install postgresql-17-pg-smtp-client;
apt install postgresql-16-pg-smtp-client;
apt install postgresql-15-pg-smtp-client;
apt install postgresql-14-pg-smtp-client;
./pgsql.yml -t pg_ext -e '{"pg_extensions": ["pg_smtp_client"]}' # -l <cls>

Create this extension with:

CREATE EXTENSION pg_smtp_client;

Usage

https://github.com/frectonz/pglite-fusion/blob/main/README.md

Enabling the extension

Connect to postgres and run the following command.

CREATE EXTENSION IF NOT EXISTS pg_smtp_client CASCADE;

Usage

Use the smtp_client.send_email() function to send an email.

Function Parameters

ParameterTypeDescriptionSystem Configuration (Optional)
subjecttextThe subject of the email
bodytextThe body of the email
htmlbooleanWhether the body is HTML (true) or plain text (false)
from_addresstextThe from email addresssmtp_client.from_address
recipientstext[]The email addresses of the recipients
ccstext[]The email addresses to CCs
bccstext[]The email addresses to BCCs
smtp_servertextThe SMTP server to usesmtp_client.server
smtp_portintegerThe port of the SMTP serversmtp_client.port
smtp_tlsbooleanWhether to use TLSsmtp_client.tls
smtp_usernametextThe username for the SMTP serversmtp_client.username
smtp_passwordtextThe password for the SMTP serversmtp_client.password

Default Configuration

You can configure the following system-wide default values for some of the parameters (as indiciated in the table above) like this:

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();

Usage Examples

Send an email:

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');

Send an email using configured default values:

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

Or, using named arguments:

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