Skip to content

Commit

Permalink
perf: 新增邮件发送测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Apr 7, 2022
1 parent 2918c55 commit 9755c59
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 13 deletions.
43 changes: 43 additions & 0 deletions app/Http/Controllers/Api/SystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Models\User;
use App\Module\Base;
use Guanguans\Notify\Factory;
use Guanguans\Notify\Messages\EmailMessage;
use Request;
use Response;

Expand Down Expand Up @@ -566,4 +568,45 @@ public function get__starthome()
]);
}

/**
* @api {get} api/system/email/check 15. 邮件发送测试(限管理员)
*
* @apiDescription 测试配置邮箱是否能发送邮件
* @apiVersion 1.0.0
* @apiGroup system
* @apiName email__check
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function email__check()
{
User::auth('admin');
//
$all = Request::input();
if (!Base::isEmail($all['to'])) {
return Base::retError('请输入正确的收件人地址');
}
try {
Factory::mailer()
->setDsn("smtp://{$all['account']}:{$all['password']}@{$all['smtp_server']}:{$all['port']}?verify_peer=0")
->setMessage(EmailMessage::create()
->from(env('APP_NAME', 'Task') . " <{$all['account']}>")
->to($all['to'])
->subject('Mail sending test')
->html('<p>收到此电子邮件意味着您的邮箱配置正确。</p><p>Receiving this email means that your mailbox is configured correctly.</p>'))
->send();
return Base::retSuccess('成功发送');
} catch (\Exception $e) {
// 一般是请求超时
if (str_contains($e->getMessage(), "Timed Out")) {
return Base::retError("language.TimedOut");
} elseif ($e->getCode() === 550) {
return Base::retError('邮件内容被拒绝,请检查邮箱是否开启接收功能');
} else {
return Base::retError($e->getMessage());
}
}
}
}
11 changes: 7 additions & 4 deletions app/Module/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,13 +996,16 @@ public static function isMobile($str)

/**
* 检测邮箱格式
* @param string $str 需要检测的字符串
* @return int
* @param $str
* @return bool
*/
public static function isEmail($str)
{
$RegExp = '/^[a-z0-9][a-z\.0-9-_]+@[a-z0-9_-]+(?:\.[a-z]{0,3}\.[a-z]{0,2}|\.[a-z]{0,3}|\.[a-z]{0,2})$/i';
return preg_match($RegExp, $str);
if (filter_var($str, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DooTask",
"version": "0.12.77",
"version": "0.12.91",
"description": "DooTask is task management system.",
"scripts": {
"start": "./cmd dev",
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/build/754.js → public/js/build/200.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/js/build/349.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion public/js/build/645.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/js/hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a3af88bf217b5750
815603318bcc7ada
2 changes: 1 addition & 1 deletion resources/assets/js/functions/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
* @returns {boolean}
*/
isEmail(email) {
return /^[a-z0-9][a-z\.0-9-_]+@[a-z0-9_-]+(?:\.[a-z]{0,3}\.[a-z]{0,2}|\.[a-z]{0,3}|\.[a-z]{0,2})$/i.test(email);
return /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*\.)+[a-zA-Z]*)$/i.test(email);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
<Input :maxlength="20" v-model="formData.port"/>
</FormItem>
<FormItem :label="$L('账号')" prop="account">
<Input :maxlength="20" v-model="formData.account"/>
<Input :maxlength="128" v-model="formData.account"/>
</FormItem>
<FormItem :label="$L('密码')" prop="password">
<Input :maxlength="20" v-model="formData.password" type="password"/>
<Input :maxlength="128" v-model="formData.password" type="password"/>
</FormItem>
<FormItem>
<Button @click="checkEmailSend">{{ $L('邮件发送测试') }}</Button>
</FormItem>
</div>

Expand Down Expand Up @@ -111,6 +114,7 @@ export default {
this.loadIng--;
});
},
hoursChange(e) {
let newNum = e * 10;
if (newNum % 5 !== 0) {
Expand All @@ -120,6 +124,7 @@ export default {
$A.messageError('任务提醒只能是0.5的倍数');
}
},
hours2Change(e) {
let newNum = e * 10;
if (newNum % 5 !== 0) {
Expand All @@ -128,6 +133,36 @@ export default {
})
$A.messageError('第二次任务提醒只能是0.5的倍数');
}
},
checkEmailSend() {
$A.modalInput({
title: "测试邮件",
placeholder: "请输入收件人地址",
onOk: (value, cb) => {
if (!value) {
cb()
return
}
if (!$A.isEmail(value)) {
$A.modalError("请输入正确的收件人地址", 301)
cb()
return
}
this.$store.dispatch("call", {
url: 'system/email/check',
data: Object.assign(this.formData, {
to: value
}),
}).then(({msg}) => {
$A.messageSuccess(msg)
cb()
}).catch(({msg}) => {
$A.modalError(msg, 301)
cb()
});
}
});
}
}
}
Expand Down
1 change: 0 additions & 1 deletion resources/assets/js/pages/single/validEmail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default {
mounted() {
this.verificationEmail();
},
watch: {},
methods: {
verificationEmail() {
this.$store.dispatch("call", {
Expand Down

0 comments on commit 9755c59

Please sign in to comment.