Skip to content

Commit 0961011

Browse files
authored
Feature/use smtp to send email (#182)
1 parent a144542 commit 0961011

File tree

6 files changed

+147
-62
lines changed

6 files changed

+147
-62
lines changed

.env.local_dev

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ DO_REGION=us-west
4444
# local storage
4545
USE_LOCAL=FALSE
4646

47-
# Email mailgun config (The app will not initialize if any of these 3 variables are not set) *********************************************************************************************************************
48-
MAILGUN_API_KEY=XXXXX
47+
# Email using Mailgun or SMTP if set enable config (The app will not initialize if any of these variables are not set) *********************************************************************************************************************
48+
MAILGUN_API_KEY=
4949
MAILGUN_DOMAIN=mail.yourdomain.com
5050
51+
SMTP_ENABLE=
52+
SMTP_HOST=
53+
SMTP_PORT=
54+
SMTP_USER_EMAIL=
55+
SMTP_PASS=
56+
5157

5258
# Base64 encoded PFX or p12 document signing certificate file *********************************************************************************************************************
5359
PFX_BASE64='MIIKLwIBAzCCCeUGCSqGSIb3DQEHAaCCCdYEggnSMIIJzjCCBEIGCSqGSIb3DQEH

apps/OpenSignServer/cloud/parsefunction/SendMailv1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function SendMailv1(request) {
55
const recipient = request.params.email;
66
const otp = request.params.otp;
77
const res = await Parse.Cloud.sendEmail({
8-
from: 'Test user' + ' <' + process.env.MAILGUN_SENDER + '>',
8+
from: 'Test user' + ' <' + process.env.SMTP_ENABLE ? process.env.SMTP_USER_EMAIL : process.env.MAILGUN_SENDER + '>',
99
recipient: recipient,
1010
subject: 'Your OpenSign™ OTP',
1111
text: 'This email is a test.',

apps/OpenSignServer/cloud/parsefunction/sendMail.js

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import fs from 'node:fs';
2+
import https from 'https';
23
import formData from 'form-data';
34
import Mailgun from 'mailgun.js';
4-
import https from 'https';
5+
import { createTransport } from 'nodemailer';
6+
7+
let transporterSMTP;
8+
let mailgunClient;
59

6-
const mailgun = new Mailgun(formData);
7-
const mailgunClient = mailgun.client({
8-
username: 'api',
9-
key: process.env.MAILGUN_API_KEY,
10-
});
11-
const mailgunDomain = process.env.MAILGUN_DOMAIN;
10+
if (process.env.SMTP_ENABLE) {
11+
transporterSMTP = createTransport({
12+
host: process.env.SMTP_HOST,
13+
port: process.env.SMTP_PORT || 465,
14+
secure: process.env.SMTP_SECURE || true,
15+
auth: {
16+
user: process.env.SMTP_USER_EMAIL,
17+
pass: process.env.SMTP_PASS,
18+
},
19+
});
20+
} else {
21+
const mailgun = new Mailgun(formData);
22+
mailgunClient = mailgun.client({
23+
username: 'api',
24+
key: process.env.MAILGUN_API_KEY,
25+
});
26+
}
1227

1328
async function sendmail(req) {
1429
try {
@@ -38,46 +53,76 @@ async function sendmail(req) {
3853
const pdfName = req.params.pdfName && `${req.params.pdfName}.pdf`;
3954
const file = {
4055
filename: pdfName || 'exported.pdf',
41-
data: PdfBuffer, //fs.readFileSync('./exports/exported_file_1223.pdf'),
56+
content: process.env.SMTP_ENABLE ? PdfBuffer : undefined, //fs.readFileSync('./exports/exported_file_1223.pdf'),
57+
data: process.env.SMTP_ENABLE ? undefined : PdfBuffer,
4258
};
4359

4460
// const html = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body style='text-align: center;'> <p style='font-weight: bolder; font-size: large;'>Hello!</p> <p>This is a html checking mail</p><p><button style='background-color: lightskyblue; cursor: pointer; border-radius: 5px; padding: 10px; border-style: solid; border-width: 2px; text-decoration: none; font-weight: bolder; color:blue'>Verify email</button></p></body></html>"
4561

4662
const from = req.params.from || '';
4763

4864
const messageParams = {
49-
from: from + ' <' + process.env.MAILGUN_SENDER + '>',
65+
from:
66+
from + ' <' + process.env.SMTP_ENABLE
67+
? process.env.SMTP_USER_EMAIL
68+
: process.env.MAILGUN_SENDER + '>',
5069
to: req.params.recipient,
5170
subject: req.params.subject,
5271
text: req.params.text || 'mail',
5372
html: req.params.html || '',
54-
attachment: file,
73+
attachments: process.env.SMTP_ENABLE ? [file] : undefined,
74+
attachment: process.env.SMTP_ENABLE ? undefined : file,
5575
};
5676

57-
const res = await mailgunClient.messages.create(mailgunDomain, messageParams);
58-
console.log('Res ', res);
59-
if (res.status === 200) {
60-
return {
61-
status: 'success',
62-
};
77+
if (transporterSMTP) {
78+
const res = await transporterSMTP.sendMail(messageParams);
79+
80+
console.log('Res ', res);
81+
if (!res.err) {
82+
return {
83+
status: 'success',
84+
};
85+
}
86+
} else {
87+
const res = await mailgunClient.messages.create(mailgunDomain, messageParams);
88+
console.log('Res ', res);
89+
if (res.status === 200) {
90+
return {
91+
status: 'success',
92+
};
93+
}
6394
}
6495
}
6596
} else {
6697
const from = req.params.from || '';
6798
const messageParams = {
68-
from: from + ' <' + process.env.MAILGUN_SENDER + '>',
99+
from:
100+
from + ' <' + process.env.SMTP_ENABLE
101+
? process.env.SMTP_USER_EMAIL
102+
: process.env.MAILGUN_SENDER + '>',
69103
to: req.params.recipient,
70104
subject: req.params.subject,
71105
text: req.params.text || 'mail',
72106
html: req.params.html || '',
73107
};
74108

75-
const res = await mailgunClient.messages.create(mailgunDomain, messageParams);
76-
console.log('Res ', res);
77-
if (res.status === 200) {
78-
return {
79-
status: 'success',
80-
};
109+
if (transporterSMTP) {
110+
const res = await transporterSMTP.sendMail(messageParams);
111+
112+
console.log('Res ', res);
113+
if (!res.err) {
114+
return {
115+
status: 'success',
116+
};
117+
}
118+
} else {
119+
const res = await mailgunClient.messages.create(mailgunDomain, messageParams);
120+
console.log('Res ', res);
121+
if (res.status === 200) {
122+
return {
123+
status: 'success',
124+
};
125+
}
81126
}
82127
}
83128
} catch (err) {

apps/OpenSignServer/index.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import S3Adapter from 'parse-server-s3-adapter';
1717
import FSFilesAdapter from 'parse-server-fs-adapter';
1818
import AWS from 'aws-sdk';
1919
import { app as customRoute } from './cloud/customRoute/customApp.js';
20+
import { createTransport } from 'nodemailer';
2021

2122
const spacesEndpoint = new AWS.Endpoint(process.env.DO_ENDPOINT);
2223
// console.log("configuration ", configuration);
23-
if (process.env.USE_LOCAL !== "TRUE") {
24+
if (process.env.USE_LOCAL !== 'TRUE') {
2425
const s3Options = {
2526
bucket: process.env.DO_SPACE, // globalConfig.S3FilesAdapter.bucket,
2627
baseUrl: process.env.DO_BASEURL,
@@ -36,18 +37,31 @@ if (process.env.USE_LOCAL !== "TRUE") {
3637
var fsAdapter = new S3Adapter(s3Options);
3738
} else {
3839
var fsAdapter = new FSFilesAdapter({
39-
"filesSubDirectory": "files" // optional, defaults to ./files
40+
filesSubDirectory: 'files', // optional, defaults to ./files
4041
});
4142
}
4243

44+
let transporterMail;
4345
let mailgunClient;
4446
let mailgunDomain;
45-
if (process.env.MAILGUN_API_KEY) {
47+
48+
if (process.env.SMTP_ENABLE) {
49+
transporterMail = createTransport({
50+
host: process.env.SMTP_HOST,
51+
port: process.env.SMTP_PORT || 465,
52+
secure: process.env.SMTP_SECURE || true,
53+
auth: {
54+
user: process.env.SMTP_USER_EMAIL,
55+
pass: process.env.SMTP_PASS,
56+
},
57+
});
58+
} else if (process.env.MAILGUN_API_KEY) {
4659
const mailgun = new Mailgun(formData);
4760
mailgunClient = mailgun.client({
4861
username: 'api',
4962
key: process.env.MAILGUN_API_KEY,
5063
});
64+
5165
mailgunDomain = process.env.MAILGUN_DOMAIN;
5266
}
5367

@@ -64,36 +78,39 @@ export const config = {
6478
// Your apps name. This will appear in the subject and body of the emails that are sent.
6579
appName: 'Open Sign',
6680
allowClientClassCreation: false,
67-
emailAdapter: process.env.MAILGUN_API_KEY
68-
? {
69-
module: 'parse-server-api-mail-adapter',
70-
options: {
71-
// The email address from which emails are sent.
72-
sender: process.env.MAILGUN_SENDER,
73-
// The email templates.
74-
templates: {
75-
// The template used by Parse Server to send an email for password
76-
// reset; this is a reserved template name.
77-
passwordResetEmail: {
78-
subjectPath: './files/password_reset_email_subject.txt',
79-
textPath: './files/password_reset_email.txt',
80-
htmlPath: './files/password_reset_email.html',
81+
emailAdapter:
82+
process.env.SMTP_ENABLE || process.env.MAILGUN_API_KEY
83+
? {
84+
module: 'parse-server-api-mail-adapter',
85+
options: {
86+
// The email address from which emails are sent.
87+
sender: process.env.SMTP_ENABLE ? process.env.SMTP_USER_EMAIL : process.env.MAILGUN_SENDER,
88+
// The email templates.
89+
templates: {
90+
// The template used by Parse Server to send an email for password
91+
// reset; this is a reserved template name.
92+
passwordResetEmail: {
93+
subjectPath: './files/password_reset_email_subject.txt',
94+
textPath: './files/password_reset_email.txt',
95+
htmlPath: './files/password_reset_email.html',
96+
},
97+
// The template used by Parse Server to send an email for email
98+
// address verification; this is a reserved template name.
99+
verificationEmail: {
100+
subjectPath: './files/verification_email_subject.txt',
101+
textPath: './files/verification_email.txt',
102+
htmlPath: './files/verification_email.html',
103+
},
81104
},
82-
// The template used by Parse Server to send an email for email
83-
// address verification; this is a reserved template name.
84-
verificationEmail: {
85-
subjectPath: './files/verification_email_subject.txt',
86-
textPath: './files/verification_email.txt',
87-
htmlPath: './files/verification_email.html',
105+
apiCallback: async ({ payload, locale }) => {
106+
if (mailgunClient) {
107+
const mailgunPayload = ApiPayloadConverter.mailgun(payload);
108+
await mailgunClient.messages.create(mailgunDomain, mailgunPayload);
109+
} else if (transporterMail) await transporterMail.sendMail(payload);
88110
},
89111
},
90-
apiCallback: async ({ payload, locale }) => {
91-
const mailgunPayload = ApiPayloadConverter.mailgun(payload);
92-
await mailgunClient.messages.create(mailgunDomain, mailgunPayload);
93-
},
94-
},
95-
}
96-
: null,
112+
}
113+
: null,
97114
filesAdapter: fsAdapter,
98115
auth: {
99116
google: {

apps/OpenSignServer/package-lock.json

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/OpenSignServer/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@
2727
"express-sse": "^0.5.3",
2828
"form-data": "^4.0.0",
2929
"jsonschema": "^1.4.1",
30-
"mailgun.js": "^9.0.1",
30+
"mailgun.js": "^9.3.0",
3131
"mongoose": "^7.2.1",
3232
"multer": "^1.4.5-lts.1",
3333
"multer-s3": "^2.10.0",
3434
"node-forge": "^1.3.1",
3535
"node-signpdf": "^1.5.1",
36+
"nodemailer": "^6.9.7",
3637
"openai": "^4.8.0",
3738
"parse": "4.1.0",
3839
"parse-server": "6.3.1",
3940
"parse-server-api-mail-adapter": "^3.0.0",
40-
"parse-server-s3-adapter": "^1.2.0",
4141
"parse-server-fs-adapter": "1.0.1",
42+
"parse-server-s3-adapter": "^1.2.0",
4243
"pdf-lib": "^1.16.0",
4344
"pdfkit": "^0.13.0",
4445
"razorpay": "^2.8.6",

0 commit comments

Comments
 (0)