Skip to content

Commit

Permalink
feat(cli): add smtp test command which help user send test email full
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Jun 14, 2023
1 parent a9ea4f5 commit 5bfa5b9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailchat-cli",
"version": "1.5.8",
"version": "1.5.9",
"description": "A Command line interface of tailchat",
"bin": {
"tailchat": "./bin/cli"
Expand Down
61 changes: 59 additions & 2 deletions apps/cli/src/commands/smtp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ export const smtpCommand: CommandModule = {
async (args) => {
config(); // 加载环境变量

console.log(
'This command will verify SMTP URI which use in tailchat, please put your URI which same like in tailchat env'
);
const { uri } = await inquirer.prompt([
{
type: 'input',
name: 'uri',
message: 'SMTP_URI',
default: process.env.SMTP_URI,
validate: (input) =>
typeof input === 'string' && input.length > 0,
validate: isValidStr,
},
]);

Expand All @@ -39,6 +41,61 @@ export const smtpCommand: CommandModule = {
}
}
)
.command(
'test',
'Send test email with smtp service',
(yargs) => {},
async (args) => {
config(); // 加载环境变量

console.log(
'This command will send test email to your own email, please put your info which same like in tailchat env'
);

const { sender, uri, target } = await inquirer.prompt([
{
type: 'input',
name: 'sender',
message: 'SMTP_SENDER',
default: process.env.SMTP_SENDER,
validate: isValidStr,
},
{
type: 'input',
name: 'uri',
message: 'SMTP_URI',
default: process.env.SMTP_URI,
validate: isValidStr,
},
{
type: 'input',
name: 'target',
message: 'Email address which wanna send',
validate: isValidStr,
},
]);

const transporter = nodemailer.createTransport(
parseConnectionUrl(uri)
);

try {
const res = await transporter.sendMail({
from: sender,
to: target,
subject: `Test email send in ${new Date().toLocaleDateString()}`,
text: 'This is a test email send by tailchat-cli',
});
console.log('Send Result:', res);
} catch (err) {
console.log('Send Failed:', String(err));
}
}
)
.demandCommand(),
handler() {},
};

function isValidStr(input: any): boolean {
return typeof input === 'string' && input.length > 0;
}

0 comments on commit 5bfa5b9

Please sign in to comment.