From 08e6791584cdd486cee0c52ad6fe45b3caa09b23 Mon Sep 17 00:00:00 2001 From: anushkrishnav Date: Mon, 4 Jan 2021 01:46:18 +0530 Subject: [PATCH 1/4] feat: added implementation to send mail using python --- python/welcome-email/README.md | 45 +++++++++++++++++++++++++++ python/welcome-email/main.py | 27 ++++++++++++++++ python/welcome-email/requirements.txt | 6 ++++ 3 files changed, 78 insertions(+) create mode 100644 python/welcome-email/README.md create mode 100644 python/welcome-email/main.py create mode 100644 python/welcome-email/requirements.txt diff --git a/python/welcome-email/README.md b/python/welcome-email/README.md new file mode 100644 index 00000000..aa557cc1 --- /dev/null +++ b/python/welcome-email/README.md @@ -0,0 +1,45 @@ +# 📧 Sending Welcome Emails using Mailgun's Email API +A sample Python Cloud Function for sending a welcome email to a newly registered user. + +## 📝 Environment Variables +Go to Settings tab of your Cloud Function. Add the following environment variables. + +* **MAILGUN_API_KEY** - API Key for Mailgun +* **MAILGUN_DOMAIN** - Domain Name from Mailgun + +## 🚀 Building and Packaging + +To package this example as a cloud function, follow these steps. + +```bash +$ cd demos-for-functions/python/welcome-email + +$ virtualenv env + +$ source env/bin/activate + +$ pip3 install requirements.txt +``` +Create a .env file with the following content + +``` +MAILGUN_API_KEY = 'Replace this with your key here' +MAILGUN_DOMAIN = 'Replace this with your Domain' + +``` + +* Ensure that your folder structure looks like this +``` +. +├── main.py +├── env +└── requirements.txt +``` + +* Navigate to the Overview Tab of your Cloud Function > Deploy Tag +* Input the command that will run your function (in this case "python3 main.py") as your entrypoint command +* Click 'Activate' + +## 🎯 Trigger + +Head over to your function in the Appwrite console and under the Settings Tab, enable the `users.create` and `account.create` event. diff --git a/python/welcome-email/main.py b/python/welcome-email/main.py new file mode 100644 index 00000000..4b48c966 --- /dev/null +++ b/python/welcome-email/main.py @@ -0,0 +1,27 @@ +import requests +import os +import json + +payload = json.parse(os.environ.get('APPWRITE_FUNCTION_EVENT_PAYLOAD')) +name = payload['name'] +email = payload['email'] + + +from dotenv import load_dotenv + +load_dotenv() + +basedir = os.path.abspath(os.path.dirname(__file__)) +key = os.environ.get('MAILGUN_API_KEY') +recipient = os.environ.get('EMAIL') +name = recipient.split('@') +name = name[0] +Domain = os.environ.get('MAILGUN_DOMAIN') + +request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(Domain) +request = requests.post(request_url, auth=('api', key), data={ + 'from': 'Welcome to My Awesome App ', + 'to': recipient, + 'subject': 'Welcome on board {0}!'.format(name), + 'text': 'Hi {0}\nGreat to have you with us. ! 😍'.format(name) +}) diff --git a/python/welcome-email/requirements.txt b/python/welcome-email/requirements.txt new file mode 100644 index 00000000..71186daa --- /dev/null +++ b/python/welcome-email/requirements.txt @@ -0,0 +1,6 @@ +certifi==2020.12.5 +chardet==4.0.0 +idna==2.10 +python-dotenv==0.15.0 +requests==2.25.1 +urllib3==1.26.2 From abd563406eee4ba6746430bdddad0904c958af82 Mon Sep 17 00:00:00 2001 From: A N U S H <54374648+anushkrishnav@users.noreply.github.com> Date: Mon, 4 Jan 2021 03:08:40 +0530 Subject: [PATCH 2/4] feat: made a minor change --- python/welcome-email/main.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/python/welcome-email/main.py b/python/welcome-email/main.py index 4b48c966..ab20dc53 100644 --- a/python/welcome-email/main.py +++ b/python/welcome-email/main.py @@ -13,15 +13,12 @@ basedir = os.path.abspath(os.path.dirname(__file__)) key = os.environ.get('MAILGUN_API_KEY') -recipient = os.environ.get('EMAIL') -name = recipient.split('@') -name = name[0] Domain = os.environ.get('MAILGUN_DOMAIN') request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(Domain) request = requests.post(request_url, auth=('api', key), data={ 'from': 'Welcome to My Awesome App ', - 'to': recipient, + 'to': email, 'subject': 'Welcome on board {0}!'.format(name), 'text': 'Hi {0}\nGreat to have you with us. ! 😍'.format(name) }) From d1c74d4f2a82390b75a73e59929335d8065ad252 Mon Sep 17 00:00:00 2001 From: A N U S H <54374648+anushkrishnav@users.noreply.github.com> Date: Tue, 2 Feb 2021 09:07:52 +0530 Subject: [PATCH 3/4] Update python/welcome-email/main.py Co-authored-by: Christy Jacob --- python/welcome-email/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/welcome-email/main.py b/python/welcome-email/main.py index ab20dc53..20255997 100644 --- a/python/welcome-email/main.py +++ b/python/welcome-email/main.py @@ -13,7 +13,7 @@ basedir = os.path.abspath(os.path.dirname(__file__)) key = os.environ.get('MAILGUN_API_KEY') -Domain = os.environ.get('MAILGUN_DOMAIN') +domain = os.environ.get('MAILGUN_DOMAIN') request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(Domain) request = requests.post(request_url, auth=('api', key), data={ From 25e2c001d5464d5434ef8af389229264e0dffc52 Mon Sep 17 00:00:00 2001 From: A N U S H <54374648+anushkrishnav@users.noreply.github.com> Date: Tue, 2 Feb 2021 09:09:17 +0530 Subject: [PATCH 4/4] Update requirements.txt --- python/welcome-email/requirements.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/python/welcome-email/requirements.txt b/python/welcome-email/requirements.txt index 71186daa..9868d65a 100644 --- a/python/welcome-email/requirements.txt +++ b/python/welcome-email/requirements.txt @@ -1,6 +1,2 @@ -certifi==2020.12.5 -chardet==4.0.0 -idna==2.10 -python-dotenv==0.15.0 requests==2.25.1 urllib3==1.26.2