Skip to content

Commit

Permalink
Merge pull request #2 from deine-Landschaft/function-is-not-useable-w…
Browse files Browse the repository at this point in the history
…ith-appwrite-0.13
  • Loading branch information
D3nn7 authored Apr 1, 2022
2 parents fafbb2e + 0feec67 commit 987efff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 45 deletions.
5 changes: 0 additions & 5 deletions .env.example

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/*
package-lock.json
.env
.env
appwrite.json
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ In this repository is a [Node.js](https://nodejs.org/) project that uses [Appwri

## Installation
1. Clone this repo
2. rename `.env.example` to `.env`
3. add your data to envioment variable in `.env`
4. customize the mail layouts in `/templates` and variables in `/src/app.js`
5. Create a `.tar.gz` archive from your project (On UNIX: `tar -zcvf dl-appwrite-mail.tar.gz dl-appwrite-mail`)
6. Go to `[appwrite instance]/[project]/Functions` and click on **Add Function**
- Give the function a name (e.g. `user mail service`) and select the `Node.js 16.0` runtime
- After creating, go into the new function and click on **Deploy Tag** and click on **Manual**
- In the **Command** field write `npm run start`
- Add your `.tar.gz` archive to the **Gzipped Code (tar.gz file)** field.
- Click **Create**
- Click **Activate**
7. Go to function **Settings**
2. Customize the mail layouts in `/templates` and variables in `/src/app.js`
3. Deploy the code as a function over the [appwrite-cli](https://appwrite.io/docs/command-line)
```bash
appwrite functions createDeployment \
--functionId=[YOUR_FUNCTION_ID] \
--activate=true \
--entrypoint="/src/app.js" \
--code="[/myrepo/myfunction]"
```
4. Go to function **Settings** on your appwrite instance
- Select the **Events**: `users.update.email`, `users.create` and `users.delete`.
- Add important variables:
- **MAIL_NAME** : This is the name from the sender
- **MAIL_ADRESS** : mail adress from sender
- **MAIL_PASSWORD** : password to connect to mailserver
- **MAIL_SMTP_HOST** : SMTP Host
- **MAIL_SMTP_PORT** : SMTP Port
- Click on **Update**

Now, if a user is created, deleted or changed to a new email, he will receive an email about this event.
Expand Down
48 changes: 21 additions & 27 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
const path = require('path');
require('dotenv').config()
"use strict";

const mailer = require("nodemailer");
const env = process.env;
var template_path = path.join(__dirname, '../templates/');


async function main() {
module.exports = async (request, response) => {
//get the current event
var event = env.APPWRITE_FUNCTION_EVENT;
const env = request.env;

//get data from user
const appwrite_data = JSON.parse(env.APPWRITE_FUNCTION_EVENT_DATA)
var user_email = appwrite_data['email'];
var user_name = appwrite_data['name'];
const data = JSON.parse(env.APPWRITE_FUNCTION_EVENT_DATA)
var user_email = data['email'];
var user_name = data['name'];
var mail_subject;

switch(event){
switch(env.APPWRITE_FUNCTION_EVENT){
case "users.create":
mail_subject = "CHANGE THIS SUBJECT";
break;
Expand All @@ -32,26 +32,23 @@ async function main() {
throw Error("event is not implemented");
}

rewriteMailContentAndSendMail(template_path + event, mail_subject, user_name, user_email);
const mail = await rewriteMailContent(template_path + env.APPWRITE_FUNCTION_EVENT, mail_subject, user_name, env);
const log = await sendMail(user_email, mail_subject, mail, env);
response.json(log);
}

function rewriteMailContentAndSendMail(file, mail_subject, user_name, user_email) {
fs.readFile(file + ".html", 'utf8', function (err, data) {
if (err) {
throw Error(err);
}

async function rewriteMailContent(file, mail_subject, user_name, env) {
let content = await readFile(file + ".html", 'utf8');
if (content !== null) {
//replace placeholder with user data
data = data.replace("{SUBJECT}", mail_subject);
data = data.replace("{NAME}", user_name);
content = content.replace("{SUBJECT}", mail_subject);
content = content.replace("{NAME}", user_name);

if (data !== null && mail_subject !== null && user_email !== null) {
sendMail(user_email, mail_subject, data);
}
});
return content;
}
};

async function sendMail(user_email, mail_subject, mail_content){
async function sendMail(user_email, mail_subject, mail_content, env){
//create reuseable transporter for SMTP transporter
let transporter = mailer.createTransport({
host: env.MAIL_SMTP_HOST,
Expand All @@ -72,8 +69,5 @@ async function sendMail(user_email, mail_subject, mail_content){
});

// log if message was send
console.log("Message sent: %s", info.messageId);
console.log("Send to:" + user_email + " type:" + env.APPWRITE_FUNCTION_EVENT);
};

main().catch(console.error);
return "Message sent: %s", info.messageId + "\n Send to:" + user_email + " type:" + env.APPWRITE_FUNCTION_EVENT;
};

0 comments on commit 987efff

Please sign in to comment.