如何从PostgreSQL发送电子邮件触发器? [英] How can I send email from PostgreSQL trigger?

查看:196
本文介绍了如何从PostgreSQL发送电子邮件触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pgsql设置触发器,当更新表数据集(更改状态为完成)
它将自动发送电子邮件到电子邮件帐户使用数据集电子邮件值,并将此电子邮件保存在服务器

I using pgsql to set a trigger, when update the table dataset(change the status to Finished) it will automatic send a email to the email account using dataset email value and save this email in server

但我不知道如何写入触发函数发送电子邮件,并在服务器中发送电子邮件。
提前感谢

but i don't know how to write in trigger function to send email, and send email in server. Thank you in advance

Pg版本是9.1和CentOS 5.8

Pg version is 9.1, and CentOS 5.8

CREATE OR REPLACE FUNCTION sss()
RETURNS trigger AS
$BODY$begin
if(NEW.publisher== 'aaaa')
then
//send email and save to server 192.168.171.64
end if;
return NEW;
end

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sss()
OWNER TO postgres;
GRANT EXECUTE ON FUNCTION sss() TO postgres;


推荐答案

请参阅优秀的常用depesz文章pg-message-queue

直接从数据库发送电子邮件可能不是很好理念。如果DNS解析慢,一切挂起30秒然后超时怎么办?如果您的邮件服务器有抖动,需要5分钟接受邮件,该怎么办?你会得到数据库会话挂在你的触发器,直到你在 max_connections ,突然你不能做任何事情,但等待或开始手动取消交易。

Sending email directly from the database may not be a great idea. What if DNS resolution is slow and everything hangs for 30 seconds then times out? What if your mail server is having a wobbly and takes 5 minutes to accept messages? You'll get database sessions hung up in your trigger until you're at max_connections and suddenly you can't do anything but wait or start manually cancelling transactions.

我建议您使用触发器 NOTIFY a LISTEN ing 帮助程序脚本,它将永久运行并连接到DB(但不​​在事务中)。

What I'd recommend is having your trigger NOTIFY a LISTENing helper script that remains permanently running and connected to the DB (but not in a transaction).

您的所有触发器必须将 INSERT 一行插入队列表,并发送 NOTIFY 。您的脚本获取 NOTIFY 消息,因为它已注册到 LISTEN ,它检查队列表, 。

All your trigger has to do is INSERT a row into a queue table and send a NOTIFY. Your script gets the NOTIFY message because it has registered to LISTEN for it, examines the queue table, and does the rest.

您可以使用任何语言编写帮助程序;我通常使用Python psycopg2

You can write the helper program in whatever language is convenient; I usually use Python with psycopg2.

该脚本可以根据在数据库中找到的信息发送电子邮件。你不必在PL / PgSQL中做所有的丑陋的文本格式化,你可以用一个更强大的脚本语言代替一个模板,并且当一个 NOTIFY时,从数据库获取变量数据

That script can send the email based on information it finds in the database. You don't have to do all the ugly text formatting in PL/PgSQL, you can substitute things into a template in a more powerful scripting language instead, and just fetch the variable data from the database when a NOTIFY comes in.

使用这种方法,您的助手可以发送每条消息,然后从队列表中删除信息。这样,如果您的邮件系统出现暂时性问题,导致发送失败,您就没有丢失信息,可以继续尝试发送,直到您成功。

With this approach your helper can send each message and only then remove the info from the queue table. That way if there are transient problems with your mail system that causes sending to fail, you haven't lost the info and can continue to attempt to send it until you succeed.

如果您真的必须在数据库中执行此操作,请参阅 PgMail

If you really must do this in the database, see PgMail.

这篇关于如何从PostgreSQL发送电子邮件触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆