Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

feat: added implementation to send mail using python #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions python/welcome-email/README.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer to the readme here
https://github.com/appwrite/demos-for-functions/tree/master/python/object-detection
We need the dependencies to be installed in the .appwrite folder

Secondly it's missing the instructions to create a tarfile

```
.
├── 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.
24 changes: 24 additions & 0 deletions python/welcome-email/main.py
Original file line number Diff line number Diff line change
@@ -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()
Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wont be needing this once we upload the cloud function.


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 <[email protected]>',
'to': email,
'subject': 'Welcome on board {0}!'.format(name),
'text': 'Hi {0}\nGreat to have you with us. ! 😍'.format(name)
})
2 changes: 2 additions & 0 deletions python/welcome-email/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.25.1
urllib3==1.26.2