Wow, es macht richtig Spass, ein bisschen an der Oberfläche von Postgres PL/SQL zu kratzen.
Es ist zwar kein richtiges MWE mehr, aber für alle, die Mails mit Datensätzen verschicken wollen, halte ich hier mal meinen funktionierenden Zwischenstand fest:
$BODY$
DECLARE
mail_event text:= 'order_new';
mail_subject text;
mail_body text;
mail_recipient text;
order_orderer text;
order_date_ready text;
order_note text;
order_experiments text;
BEGIN
SELECT m.subject, m.body, u.email
INTO mail_subject, mail_body, mail_recipient
FROM kus_physlab.mail m
JOIN kus_physlab.user u ON u.id = m.user_id_recipient
WHERE m.event = mail_event;
SELECT u.nickname
INTO order_orderer
FROM kus_physlab.user u
WHERE u.id = NEW.user_id_orderer;
order_date_ready := TO_CHAR(TO_TIMESTAMP(NEW.date_ready), 'DD.MM.YYYY');
order_note := NEW.note;
mail_subject := REPLACE(mail_subject, '[order_orderer]', order_orderer);
mail_subject := REPLACE(mail_subject, '[order_date_ready]', order_date_ready);
mail_body := REPLACE(mail_body, '[order_orderer]', order_orderer);
mail_body := REPLACE(mail_body, '[order_date_ready]', order_date_ready);
mail_body := REPLACE(mail_body, '[order_note]', order_note);
PERFORM instance.mail_send(mail_subject,mail_body,mail_recipient);
RETURN NEW;
END;
$BODY$
Dabei war es für mich super hilfreich, schnell sehen zu können, ob Daten (hier NEW.note
) richtig extrahiert wurden:
PERFORM instance.log_warning(NEW.note, 'kus_physlab');