-
Notifications
You must be signed in to change notification settings - Fork 7
/
py_pgmail.sql
52 lines (48 loc) · 2.39 KB
/
py_pgmail.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
DROP FUNCTION py_pgmail(text,text[], text[], text[], text, text, text, text, text, text);
CREATE OR REPLACE FUNCTION public.py_pgmail(from_addr text, to_addr_list text[], cc_addr_list text[], bcc_addr_list text[], subject text, login text, password text, message_text text DEFAULT ''::text, message_html text DEFAULT ''::text, _filename text DEFAULT ''::text, _attachment text DEFAULT ''::text, smtpserver text DEFAULT 'localhost'::text)
RETURNS boolean
LANGUAGE plpython3u
AS $function$
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
msg = MIMEMultipart()
msg["Subject"] = subject
msg['From'] = from_addr
msg['To'] = ', '.join(to_addr_list)
#msg['Cc'] = ', '.join(cc_addr_list)
if message_text.replace(' ', '')!='':
part1 = MIMEText(message_text, 'plain')
msg.attach(part1)
if message_html.replace(' ', '')!='':
part2 = MIMEText(message_html, 'html')
msg.attach(part2)
#If no message (html or text) then stop script execution.
if message_html.replace(' ', '')=='' and message_text.replace(' ', '')=='':
plpy.info('An error ocurred: No message to send.')
return False
#Bcc needs to be added now, it should not be added to message.
all_addr_list = to_addr_list #+ cc_addr_list + bcc_addr_list
if _filename.replace(' ', '') != '' and _attachment.replace(' ', '') != '':
filename = _filename
attachment = open(_attachment, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP(smtpserver)
#server.starttls()
if login!='':
server.login(login, password)
problems = server.sendmail(from_addr, all_addr_list,msg.as_string())
server.quit()
#if we have problems then print the problem and return False
if len(problems)>0:
plpy.info('An error ocurred: '+str(problems))
return False
else:
return True
$function$