From 9805ec3c0290c2f74128af0bcfe946985c642a69 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Fri, 10 Sep 2021 10:41:32 +0200 Subject: [PATCH] added welcome-email-smtp nodejs --- nodejs/welcome-email-smtp/README.md | 46 ++++++++++++++++++++++ nodejs/welcome-email-smtp/index.js | 54 ++++++++++++++++++++++++++ nodejs/welcome-email-smtp/package.json | 14 +++++++ 3 files changed, 114 insertions(+) create mode 100644 nodejs/welcome-email-smtp/README.md create mode 100644 nodejs/welcome-email-smtp/index.js create mode 100644 nodejs/welcome-email-smtp/package.json diff --git a/nodejs/welcome-email-smtp/README.md b/nodejs/welcome-email-smtp/README.md new file mode 100644 index 00000000..c010841c --- /dev/null +++ b/nodejs/welcome-email-smtp/README.md @@ -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. diff --git a/nodejs/welcome-email-smtp/index.js b/nodejs/welcome-email-smtp/index.js new file mode 100644 index 00000000..6949d8bc --- /dev/null +++ b/nodejs/welcome-email-smtp/index.js @@ -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); + } +}); \ No newline at end of file diff --git a/nodejs/welcome-email-smtp/package.json b/nodejs/welcome-email-smtp/package.json new file mode 100644 index 00000000..2f6ad58b --- /dev/null +++ b/nodejs/welcome-email-smtp/package.json @@ -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" + } +}