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..20255997 --- /dev/null +++ b/python/welcome-email/main.py @@ -0,0 +1,24 @@ +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') +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': email, + '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..9868d65a --- /dev/null +++ b/python/welcome-email/requirements.txt @@ -0,0 +1,2 @@ +requests==2.25.1 +urllib3==1.26.2