Skip to content

Commit

Permalink
feat: add SenderName option
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 2, 2024
1 parent 61e8b1d commit f6ce317
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 17 deletions.
10 changes: 9 additions & 1 deletion docs/pages/changelog.en.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
# Breaking Changes

## v0.24

**Breaking Changes**

- **The `SendMailAddress` has been renamed to `SenderAddress`**

Update steps: Modify the `SendMailAddress` configuration key to `SenderAddress`

## v0.18

Expand Down
8 changes: 8 additions & 0 deletions docs/pages/changelog.ja.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# リリースノート

## v0.24

**Breaking Changes**

- **`SendMailAddress``SenderAddress` に名前が変更されました**

更新手順: `SendMailAddress` 設定キーを `SenderAddress` に変更します

## v0.18

- **新機能**:多言語対応。今は英語、日本語と中国語を対応しています。
Expand Down
8 changes: 8 additions & 0 deletions docs/pages/changelog.zh.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# 更新日志

## v0.24

**Breaking Changes**

- **配置项 `SendMailAddress` 已更名为 `SenderAddress`**

更新步骤:将 `SendMailAddress` 配置项更改为 `SenderAddress`

## v0.18

- **新增**:初步支持了 i18n 多语言支持,目前支持中文、日文、英文
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/config/appsettings.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ Here is a complete example of configuration:
}
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down Expand Up @@ -219,7 +220,8 @@ GZCTF supports metrics and distributed tracing. You can configure the providers

Here we can configure the information of email sending server. If you use email registration and other email features, this is required.

- **SendMailAddress:** Email address of the sender
- **SenderAddress:** Email address of the sender
- **SenderName:** Name of the sender
- **UserName:** SMTP Server username
- **Password:** SMTP Server password
- **Smtp:** SMTP Server address and port
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/config/appsettings.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ import { Callout } from "nextra-theme-docs";
}
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down Expand Up @@ -219,7 +220,8 @@ GZCTF はメトリクスと分散トレーシングをサポートしていま

ここではメール送信に関する情報を設定します。メールでの登録やその他のメール機能を使用する場合、この項目は必須です。

- **SendMailAddress:** 送信者のメールアドレス
- **SenderAddress:** 送信者のメールアドレス
- **SenderName:** 送信者の名前
- **UserName:** SMTP サーバーのユーザー名
- **Password:** SMTP サーバーのパスワード
- **Smtp:** SMTP サーバーのアドレスとポート
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/config/appsettings.zh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ import { Callout } from "nextra-theme-docs";
}
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down Expand Up @@ -219,7 +220,8 @@ GZCTF 支持测量和分布式追踪。您可以配置您想要使用的提供

此处配置邮件发送相关信息,若采用邮箱注册和其他相关邮件功能,此处为必填项。

- **SendMailAddress:** 发件人邮箱地址
- **SenderAddress:** 发件人邮箱地址
- **SenderName:** 发件人名称
- **UserName:** SMTP 服务器用户名
- **Password:** SMTP 服务器密码
- **Smtp:** SMTP 服务器地址和端口
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/quick-start.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ You will need to install Docker and docker-compose to get started with GZCTF. Yo
"Database": "Host=db:5432;Database=gzctf;Username=postgres;Password=<Your POSTGRES_PASSWORD>"
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/quick-start.ja.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ GZCTF の使用をすぐに開始するには、Docker と docker-compose をイ
"Database": "Host=db:5432;Database=gzctf;Username=postgres;Password=<Your POSTGRES_PASSWORD>"
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/quick-start.zh.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ GZCTF 的安全性和前端功能(如操作剪贴板)依赖于 HTTPS,此
"Database": "Host=db:5432;Database=gzctf;Username=postgres;Password=<Your POSTGRES_PASSWORD>"
},
"EmailConfig": {
"SendMailAddress": "",
"SenderAddress": "",
"SenderName": "",
"UserName": "",
"Password": "",
"Smtp": {
Expand Down
3 changes: 2 additions & 1 deletion src/GZCTF/Models/Internal/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ public class EmailConfig
{
public string? UserName { get; set; } = string.Empty;
public string? Password { get; set; } = string.Empty;
public string? SendMailAddress { get; set; } = string.Empty;
public string? SenderAddress { get; set; } = string.Empty;
public string? SenderName { get; set; } = string.Empty;
public SmtpConfig? Smtp { get; set; } = new();
}

Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Services/Cache/CacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static class CacheKey
/// The cache update lock
/// </summary>
public static string UpdateLock(string key) => $"_UpdateLock_{key}";

/// <summary>
/// The last update time
/// </summary>
Expand Down
15 changes: 10 additions & 5 deletions src/GZCTF/Services/Mail/MailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MailSender(
_options = options.Value;
_cancellationToken = _cancellationTokenSource.Token;

if (string.IsNullOrWhiteSpace(_options.SendMailAddress) ||
if (string.IsNullOrWhiteSpace(_options.SenderAddress) ||
string.IsNullOrWhiteSpace(_options.Smtp?.Host) || _options.Smtp.Port is not > 0)
return;

Expand Down Expand Up @@ -81,14 +81,14 @@ public void Dispose()
GC.SuppressFinalize(this);
}

async Task<bool> SendEmailAsync(string subject, string content, string to)
async Task<bool> SendEmailAsync(string subject, string content, MailboxAddress from, MailboxAddress to)
{
if (_smtpClient is null)
return false;

using var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(_options!.SendMailAddress, _options.SendMailAddress));
msg.To.Add(new MailboxAddress(to, to));
msg.From.Add(from);
msg.To.Add(to);
msg.Subject = subject;
msg.Body = new TextPart(TextFormat.Html) { Text = content };

Expand Down Expand Up @@ -126,7 +126,12 @@ public async Task SendMailContent(MailContent content)

var title = $"{content.Title} - {content.Platform}";

if (!await SendEmailAsync(title, emailContent, content.Email))
var sender = string.IsNullOrWhiteSpace(_options!.SenderName) ? _options.SenderName : content.Platform;
var from = new MailboxAddress(sender, _options.SenderAddress);

var to = new MailboxAddress(content.UserName, content.Email);

if (!await SendEmailAsync(title, emailContent, from, to))
_logger.SystemLog(StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)],
TaskStatus.Failed);
}
Expand Down

0 comments on commit f6ce317

Please sign in to comment.