0

How do I send mail through PostgreSQL database?

I have installed the TCL untrusted language pltclu.

What should I do next?

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Nidhi
  • 1
  • 1
  • 2
  • How to send attachments in mail – Nidhi Dec 16 '16 at 05:15
  • Possible duplicate of [sp\_send\_dbmail alternative in postgres? Easy way to send Postgres email reports?](https://stackoverflow.com/questions/52114625/sp-send-dbmail-alternative-in-postgres-easy-way-to-send-postgres-email-reports) –  Dec 05 '18 at 15:08

2 Answers2

0

Below code is working

CREATE OR REPLACE FUNCTION public.sendmail(
    p_from text,
    p_to text,
    p_subject text,
    p_content text)
  RETURNS void AS
$BODY$

use strict;

use warnings;

my ($from, $to, $subject, $content) = @_;



open(MAIL, "|/usr/sbin/sendmail -t") or
die 'Cannot send mail';

print MAIL "From: $from\n";

print MAIL "To: $to\n";

print MAIL "Subject: $subject\n\n";

print MAIL "$content";



close(MAIL);

$BODY$
  LANGUAGE plperlu VOLATILE;
Jan Trienes
  • 2,501
  • 1
  • 16
  • 28
Nidhi
  • 1
  • 1
  • 2
0

You can use py_pgmail from: https://github.com/lcalisto/py_pgmail

Then you can just call:

select py_pgmail('sentFromEmail',array['destination emails'],array['cc'],array['bcc'],'Subject','<USERNAME>','<PASSWORD>','Text message','HTML message','<MAIL.MYSERVER.COM:PORT>')

array['cc'] and array['bcc'] can be empty arrays like array['']

lcalisto
  • 46
  • 5