Skip to content

Commit a1fd36e

Browse files
committed
Revise the email subscription mechanism using emailjs not php
1 parent 246d0de commit a1fd36e

3 files changed

Lines changed: 146 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.json

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,74 @@ In the current cryptocurrency market, fraud and chaos are rampant, greatly damag
1313
When the market is highly volatile and confidence is low, the system will automatically execute intelligent trading strategies to prevent a vicious cycle caused by panic selling; as market confidence gradually recovers, trading will be gradually opened to restore normal market liquidity. In this way, our project can achieve steady growth of the K-line, alleviate market volatility in a controlled manner, and create a more stable and predictable trading environment for users.
1414

1515
Our goal is to reduce uncertainty in trading through technological means, establish a trust-first cryptocurrency ecosystem, provide a safer and more reliable investment experience for the majority of investors, and strive to become a leader in enhancing market confidence.
16+
17+
## Setting Up for Mail Subscription Service
18+
19+
Here is the detailed step about how to configure the backend mail server for GitHub Pages (or other web services that only support frontend pages).
20+
21+
1. Generate HTML Mail Template (Postcards)
22+
23+
[Postcards - Designmodo](https://designmodo.com/postcards/app/)
24+
25+
After editing the contents, export as a ZIP file with the images and HTML files together.
26+
27+
2. Domain Email Account Registration and SMTP Server Setting
28+
29+
[GoDaddy Webmail](https://email.godaddy.com/)
30+
31+
3. Use EmailJS for Email Backend Service
32+
33+
Basic Setting
34+
35+
[Send email directly from your code | EmailJS](https://www.emailjs.com/)
36+
37+
REST API Documentation
38+
39+
[/send API | EmailJS](https://www.emailjs.com/docs/rest-api/send/)
40+
41+
Note:
42+
43+
- SMTP.js only supports elasticemail as its backend SMTP mail server, no third-party SMTP server is supported.
44+
- The limitation of the content body of EmailJS is no more than 50kb, be sure the size of the HTML file is less than the threshold.
45+
- We can use the following website to shrink the size of the HTML file by removing the unnecessary characters (like white space, etc)
46+
47+
[HTML Compressor - Reduce the size of HTML, CSS, JavaScript, PHP and Smarty code.](https://htmlcompressor.com/compressor/)
48+
49+
4. Backblaze B2 OBS Bucket for Image Storage
50+
51+
We need to upload the images extracted from the downloaded ZIP file to the OBS bucket and replace all of the image paths from the relative path to the HTTPS path, which can be obtained through the detailed property of the file in the OBS bucket.
52+
53+
## Setting Up `config.json` for Local Deployment
54+
55+
To successfully deploy and run the project locally, you need to create a `config.json` file in the root directory of the project. This file contains essential configuration details needed for the application to interact with the blockchain network.
56+
57+
### Step-by-Step Guide
58+
59+
1. **Create the File:**
60+
- In the root directory of your project, create a file named `config.json`.
61+
62+
2. **Add Basic Structure:**
63+
- Open the file in a text editor and add the following JSON structure:
64+
```json
65+
{
66+
"emailToken": "Your_Email_Token",
67+
"emailServiceID": "Your_Email_Service_ID",
68+
"emailTemplate": "Your_Email_Template"
69+
}
70+
```
71+
- Replace `Your_Email_Token` with the token for sending emails.
72+
- Replace `Your_Email_Service_ID` with the service ID for sending emails, for example: `lotso_email`.
73+
- Replace `Your_Email_Template` with the email template name, for example: `chubgame_email_template`.
74+
75+
3. **Save the File:**
76+
- Save your changes to `config.json`.
77+
78+
4. **Important Notes:**
79+
- The `config.json` file is crucial for the application's interaction with the blockchain. Ensure that the details are correct.
80+
- If you are working in a team or planning to push this code to a public repository, **do not** include sensitive information like private keys or secret tokens in the `config.json` file. Instead, use environment variables or other secure methods to handle sensitive data.
81+
82+
5. **.gitignore:**
83+
- If you are using Git, make sure to add `config.json` to your `.gitignore` file to prevent accidentally pushing it to a public repository:
84+
```bash
85+
echo "config.json" >> .gitignore
86+
```

js/script.js

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
(function ($) {
22
"use strict";
3+
4+
const isLocal = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
5+
6+
function loadConfig() {
7+
if (isLocal) {
8+
return fetch('../config.json')
9+
.then(response => response.json())
10+
.then(config => {
11+
return {
12+
EMAIL_SERVICE_ID: config.EMAIL_SERVICE_ID,
13+
EMAIL_TEMPLATE: config.EMAIL_TEMPLATE,
14+
EMAIL_TOKEN: config.EMAIL_TOKEN
15+
};
16+
});
17+
} else {
18+
return Promise.resolve({
19+
EMAIL_SERVICE_ID: process.env.EMAIL_SERVICE_ID,
20+
EMAIL_TEMPLATE: process.env.EMAIL_TEMPLATE,
21+
EMAIL_TOKEN: process.env.EMAIL_TOKEN
22+
});
23+
}
24+
}
325

426
// Pace.on("start", function () {
527
// $("#preloader").show();
@@ -81,6 +103,40 @@
81103
});
82104
});
83105

106+
function sendSubscriptionEmail(userEmail) {
107+
return loadConfig()
108+
.then(config => {
109+
return fetch('../email/index.html')
110+
.then(response => response.text())
111+
.then(htmlBody => {
112+
const templateParams = {
113+
to_email: userEmail,
114+
reply_to: userEmail,
115+
message_html: htmlBody
116+
};
117+
118+
return fetch('https://api.emailjs.com/api/v1.0/email/send', {
119+
method: 'POST',
120+
headers: {
121+
'Content-Type': 'application/json'
122+
},
123+
body: JSON.stringify({
124+
service_id: config.EMAIL_SERVICE_ID,
125+
template_id: config.EMAIL_TEMPLATE,
126+
user_id: config.EMAIL_TOKEN,
127+
template_params: templateParams,
128+
})
129+
});
130+
})
131+
.then(response => {
132+
if (!response.ok) {
133+
throw new Error('Failed to send email');
134+
}
135+
return response;
136+
});
137+
});
138+
}
139+
84140
function checkForm() {
85141
if ($(".form-holder").length > 0) {
86142

@@ -93,17 +149,24 @@
93149

94150
// triggers contact form validation
95151

96-
if (formStatus.errorList.length === 0)
97-
{
98-
$(".form-holder form").fadeOut(function () {
99-
$('#loading').css('visibility', 'visible');
100-
$.post('submit.php', $(".form-holder form").serialize(),
101-
function (data) {
102-
$('.message-box').html(data);
103-
$('#loading').css('visibility', 'hidden');
104-
}
105-
);
106-
});
152+
if (formStatus.errorList.length === 0) {
153+
$(".form-holder form").fadeOut(function () {
154+
$('#loading').css('visibility', 'visible');
155+
156+
const userEmail = $(".form-holder form").find('input[name="email"]').val();
157+
158+
sendSubscriptionEmail(userEmail)
159+
.then(() => {
160+
$('.message-box').html("<div class='alert alert-success'>Your Message Sent!</div>");
161+
})
162+
.catch((error) => {
163+
$('.message-box').html("<div class='alert alert-error'>Error Occurred: " + error.message + "</div>");
164+
})
165+
.finally(() => {
166+
$('#loading').css('visibility', 'hidden');
167+
$(".form-holder form").fadeIn();
168+
});
169+
});
107170
}
108171
});
109172
}

0 commit comments

Comments
 (0)