2023-04-19 20:13:54 +00:00
|
|
|
import Express, {
|
|
|
|
ExpressRequest,
|
|
|
|
ExpressResponse,
|
|
|
|
ExpressRouter,
|
|
|
|
} from 'CommonServer/Utils/Express';
|
|
|
|
const router: ExpressRouter = Express.getRouter();
|
|
|
|
|
|
|
|
import Response from 'CommonServer/Utils/Response';
|
|
|
|
import MailService from '../Services/MailService';
|
|
|
|
import EmailMessage from 'Common/Types/Email/EmailMessage';
|
|
|
|
import EmailTemplateType from 'Common/Types/Email/EmailTemplateType';
|
|
|
|
import { JSONObject } from 'Common/Types/JSON';
|
|
|
|
import Email from 'Common/Types/Email';
|
|
|
|
import EmailServer from 'Common/Types/Email/EmailServer';
|
|
|
|
import ProjectSmtpConfig from 'Model/Models/ProjectSmtpConfig';
|
2023-04-20 07:27:32 +00:00
|
|
|
import ProjectSMTPConfigService from 'CommonServer/Services/ProjectSmtpConfigService';
|
2023-04-19 20:13:54 +00:00
|
|
|
import BadDataException from 'Common/Types/Exception/BadDataException';
|
|
|
|
import ObjectID from 'Common/Types/ObjectID';
|
|
|
|
import logger from 'CommonServer/Utils/Logger';
|
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
router.post('/test', async (req: ExpressRequest, res: ExpressResponse) => {
|
|
|
|
const body: JSONObject = req.body;
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
const smtpConfigId: ObjectID = new ObjectID(body['smtpConfigId'] as string);
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
const config: ProjectSmtpConfig | null =
|
|
|
|
await ProjectSMTPConfigService.findOneById({
|
2023-04-19 20:13:54 +00:00
|
|
|
id: smtpConfigId,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
hostname: true,
|
|
|
|
port: true,
|
|
|
|
username: true,
|
|
|
|
password: true,
|
|
|
|
fromEmail: true,
|
|
|
|
fromName: true,
|
|
|
|
secure: true,
|
2023-04-19 20:16:29 +00:00
|
|
|
},
|
|
|
|
});
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
if (!config) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException(
|
|
|
|
'smtp-config not found for id' + smtpConfigId.toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
const toEmail: Email = new Email(body['toEmail'] as string);
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
if (!toEmail) {
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException('toEmail is required')
|
|
|
|
);
|
|
|
|
}
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
const mail: EmailMessage = {
|
|
|
|
templateType: EmailTemplateType.SMTPTest,
|
|
|
|
toEmail: new Email(body['toEmail'] as string),
|
2023-04-19 20:19:11 +00:00
|
|
|
subject: 'Test Email from OneUptime',
|
2023-05-17 11:38:13 +00:00
|
|
|
vars: {},
|
2023-04-19 20:16:29 +00:00
|
|
|
body: '',
|
|
|
|
};
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
const mailServer: EmailServer = {
|
|
|
|
host: config.hostname!,
|
|
|
|
port: config.port!,
|
|
|
|
username: config.username!,
|
|
|
|
password: config.password!,
|
|
|
|
fromEmail: config.fromEmail!,
|
|
|
|
fromName: config.fromName!,
|
|
|
|
secure: Boolean(config.secure),
|
|
|
|
};
|
2023-04-19 20:13:54 +00:00
|
|
|
|
2023-04-19 20:16:29 +00:00
|
|
|
try {
|
|
|
|
await MailService.send(mail, mailServer);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return Response.sendErrorResponse(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
new BadDataException(
|
|
|
|
'Cannot send email. Please check your SMTP config.'
|
|
|
|
)
|
|
|
|
);
|
2023-04-19 20:13:54 +00:00
|
|
|
}
|
2023-04-19 20:16:29 +00:00
|
|
|
|
|
|
|
return Response.sendEmptyResponse(req, res);
|
|
|
|
});
|
2023-04-19 20:13:54 +00:00
|
|
|
|
|
|
|
export default router;
|