触发发送多封邮件

时间:2018-11-20 14:00:24

标签: email postgresql-9.3

我安装了pgsmtp扩展名,用于从Postgres(版本9.3)发送邮件,我想创建一个触发器以根据表中的值发送邮件。

这是触发器从中提取数据以发送电子邮件的表:

    CREATE TABLE scadenze_lcl_fibra
(
  gid serial NOT NULL,
  lcl_numero numeric(10,0) NOT NULL,
  mail integer DEFAULT 0,
  CONSTRAINT scadenze_lcl_fibra_pkey PRIMARY KEY (gid)
)
WITH (
  OIDS=FALSE
);

这是触发器:

CREATE TRIGGER mail_scad_lcl_fibra
  AFTER INSERT
  ON scadenze_lcl_fibra
  FOR EACH ROW
  EXECUTE PROCEDURE mail_scad_lcl_fibra();
ALTER TABLE scadenze_lcl_fibra DISABLE TRIGGER mail_scad_lcl_fibra;

此功能:

CREATE OR REPLACE FUNCTION mail_scad_lcl_fibra()
  RETURNS trigger AS
$BODY$
BEGIN
IF new.mail = 0 THEN
perform pgsmtp.pg_smtp_mail('sender@mail.com','receiver@mail.com',array[''], (SELECT 'Number LCL ' || new.lcl_numero FROM scadenze_lcl_fibra LIMIT 1),(SELECT 'Notify LCL ' || new.lcl_numero FROM scadenze_lcl_fibra LIMIT 1));
ELSE NULL;
END IF;
RETURN NEW;
END
$BODY$

执行发送邮件的命令(pgsmtp.pg_smtp_mail)后,我希望触发器能够更改mail列中1的值并执行下一行的命令。

我该怎么办?

0 个答案:

没有答案