fix: Ability to add SMTP without user/pass.

Some Services Doesn't require SMTP user/pass.
fixes #30
This commit is contained in:
Towfiq 2022-12-21 08:31:00 +06:00
parent 9b71f8400b
commit 671f89e492
3 changed files with 14 additions and 16 deletions

View File

@ -68,11 +68,9 @@ const Settings = ({ closeSettings }:SettingsProps) => {
error = { type: 'no_email', msg: 'Insert a Valid Email address' };
}
if (settings.notification_email
&& (!settings.smtp_username || !settings.smtp_password || !settings.smtp_port || !settings.smtp_server
&& (!settings.smtp_port || !settings.smtp_server
|| !settings.notification_email_from)) {
let type = 'no_smtp_from';
if (!settings.smtp_password) { type = 'no_smtp_pass'; }
if (!settings.smtp_username) { type = 'no_smtp_user'; }
if (!settings.smtp_port) { type = 'no_smtp_port'; }
if (!settings.smtp_server) { type = 'no_smtp_server'; }
error = { type, msg: 'Insert SMTP Server details that will be used to send the emails.' };
@ -234,8 +232,7 @@ const Settings = ({ closeSettings }:SettingsProps) => {
<div className="settings__section__input mb-5">
<label className={labelStyle}>SMTP Username</label>
<input
className={`w-full p-2 border border-gray-200 rounded mb-3 focus:outline-none focus:border-blue-200
${settingsError && settingsError.type === 'no_smtp_user' ? ' border-red-400 focus:border-red-400' : ''} `}
className={'w-full p-2 border border-gray-200 rounded mb-3 focus:outline-none focus:border-blue-200'}
type="text"
value={settings?.smtp_username || ''}
onChange={(event) => updateSettings('smtp_username', event.target.value)}
@ -244,8 +241,7 @@ const Settings = ({ closeSettings }:SettingsProps) => {
<div className="settings__section__input mb-5">
<label className={labelStyle}>SMTP Password</label>
<input
className={`w-full p-2 border border-gray-200 rounded mb-3 focus:outline-none focus:border-blue-200
${settingsError && settingsError.type === 'no_smtp_pass' ? ' border-red-400 focus:border-red-400' : ''} `}
className={'w-full p-2 border border-gray-200 rounded mb-3 focus:outline-none focus:border-blue-200'}
type="text"
value={settings?.smtp_password || ''}
onChange={(event) => updateSettings('smtp_password', event.target.value)}

View File

@ -24,9 +24,9 @@ const notify = async (req: NextApiRequest, res: NextApiResponse<NotifyResponse>)
const reqDomain = req?.query?.domain as string || '';
try {
const settings = await getAppSettings();
const { smtp_server = '', smtp_port = '', smtp_username = '', smtp_password = '', notification_email = '' } = settings;
const { smtp_server = '', smtp_port = '', notification_email = '' } = settings;
if (!smtp_server || !smtp_port || !smtp_username || !smtp_password || !notification_email) {
if (!smtp_server || !smtp_port || !notification_email) {
return res.status(401).json({ success: false, error: 'SMTP has not been setup properly!' });
}
@ -65,11 +65,13 @@ const sendNotificationEmail = async (domain: Domain, settings: SettingsType) =>
} = settings;
const fromEmail = `SerpBear <${notification_email_from || 'no-reply@serpbear.com'}>`;
const transporter = nodeMailer.createTransport({
host: smtp_server,
port: parseInt(smtp_port, 10),
auth: { user: smtp_username, pass: smtp_password },
});
const mailerSettings:any = { host: smtp_server, port: parseInt(smtp_port, 10) };
if (smtp_username || smtp_password) {
mailerSettings.auth = {};
if (smtp_username) mailerSettings.auth.user = smtp_username;
if (smtp_password) mailerSettings.auth.pass = smtp_password;
}
const transporter = nodeMailer.createTransport(mailerSettings);
const domainName = domain.domain;
const query = { where: { domain: domainName } };
const domainKeywords:Keyword[] = await Keyword.findAll(query);

4
types.d.ts vendored
View File

@ -75,8 +75,8 @@ type SettingsType = {
notification_email_from: string,
smtp_server: string,
smtp_port: string,
smtp_username: string,
smtp_password: string,
smtp_username?: string,
smtp_password?: string,
search_console_integrated?: boolean,
}