Skip to content

Send emails from a PostgreSQL Database using Python3 (plpython3u)

License

Notifications You must be signed in to change notification settings

gioman/py_pgmail

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

py_pgmail

Sends emails from a PostgreSQL Database using Python3 (plpython3u)

This is a simple function in plpython3u (Python3) that sends an email from inside a PostgreSQL database.

In order to use this function please run the folowing steps:

  1. Create py_pgmail function by running py_pgmail.sql

  2. After the function is created you can simply call the function as:

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

WARNING: You can send a message in plain text or in HTML. If you provide both plain text and HTML then only the HTML will be sent!

For example:

-------------------   
-- HTML message --
-----------------
select py_pgmail(
	'sentFromEmail@whatever.com',
	array['dest@email.com','dest2@email.com'],
	array['cc@email.com','cc2@email.com','cc3@email.com'],
	array['bcc@email.com','bcc2@email.com','bcc3@email.com'],
	'My subject',
	'<USERNAME>',
	'<PASSWORD>',
	'Text message! This is an email sent from a database!',
	'<!DOCTYPE html>
		<html>
		<body>
		<p>html message! This is an email sent from a database!</p>
		</body>
		</html>',
	'smtp.gmail.com:587');
    
-------------------------   
-- Plain text message --
-----------------------
select py_pgmail(
	'sentFromEmail@whatever.com',
	array['dest@email.com','dest2@email.com'],
	array['cc@email.com','cc2@email.com','cc3@email.com'],
	array['bcc@email.com','bcc2@email.com','bcc3@email.com'],
	'My subject',
	'<USERNAME>',
	'<PASSWORD>',
	'Text message! This is an email sent from a database!',
	'',
    	'smtp.gmail.com:587');

Make sure you replace <USERNAME> <PASSWORD> and <MAIL.MYSERVER.COM:PORT> by the your email server config values.

If you plan to send emails using gmail make sure you allow less secure apps into your account

You can use this function to send emails with cc and bcc.

About

Send emails from a PostgreSQL Database using Python3 (plpython3u)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PLpgSQL 100.0%