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

added welcome-email-smtp nodejs #74

Open
wants to merge 1 commit 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
46 changes: 46 additions & 0 deletions nodejs/welcome-email-smtp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 📧 Sending Welcome Emails using your own SMTP server
A sample Node.js 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.

* **SMTP_HOST** - Your SMTP host
* **SMTP_PORT** - Your SMTP host port
* **SMTP_USER** - Your SMTP username
* **SMTP_PASSWORD** - Your SMTP password
* **SMTP_FROM** - From who the email should come from

## 🚀 Building and Packaging

To package this example as a cloud function, follow these steps.

```bash
$ cd demos-for-functions/nodejs/welcome-email-smtp

$ npm install
```

* Ensure that your folder structure looks like this
```
.
├── index.js
├── node_modules/
├── package-lock.json
└── package.json
```

* Create a tarfile

```bash
$ cd ..
$ tar -zcvf code.tar.gz welcome-email-smtp
```

* Navigate to the Overview Tab of your Cloud Function > Deploy Tag
* Input the command that will run your function (in this case "node index.js") as your entrypoint command
* Upload your tarfile
* 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.
54 changes: 54 additions & 0 deletions nodejs/welcome-email-smtp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Perform all your imports

const nodemailer = require('nodemailer');
let SMTPsecure;

//Get your env variables
const SMTPconfig = {
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
user: process.env.SMTP_USER,
password: process.env.SMTP_PASSWORD,
from: process.env.SMTP_FROM,
};

// Get the name and email of the newly created user from Appwrite's environment variable
const payload = JSON.parse(process.env.APPWRITE_FUNCTION_EVENT_DATA);
const name = payload['name'];
const email = payload['email'];


//Checks if you use SMTP secure
if( SMTPconfig.port == 465){
SMTPsecure = true;
}else{
SMTPsecure = false;
}

//Initialize nodemailer
var transporter = nodemailer.createTransport({
host: SMTPconfig.host,
port: SMTPconfig.port,
secure: SMTPsecure,
auth: {
user: SMTPconfig.user,
pass: SMTPconfig.password,
},})

//Create your email
var mailOptions = {
from: SMTPconfig.from,
to: email,
subject: `Welcome on board ${name}!`,
text: `Hi ${name}\nGreat to have you with us. ! 😍`
};


//send email
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
14 changes: 14 additions & 0 deletions nodejs/welcome-email-smtp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cloud-functions-demo-nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"nodemailer": "^6.6.3"
}
}