Vigil 发送多人邮件通知的处理

Vigil 默认是只能发送单人邮件,但是我们有需要发送多个的场景。
解决方法:

  • 大家使用一样的账户登陆
  • 使用邮件组
  • 修改下源码
    为了学习下Vigil 的构建,以及原理,我简单通过修改源码的方式(目前支持4个人,但是代码是写死的)
    后边可以进一步优化

项目github 代码

代码我已经提交github 了
https://github.com/rongfengliang/myvigil-multiemail

修改部分说明

Vigil 使用toml 进行配置管理,我们需要做的是添加新的邮件发送字段(struct 结构体中)

src/config/config.rs 文件

  • 代码如下
 
#[derive(Deserialize)]
pub struct ConfigNotifyEmail {
    pub to: String,
    pub from: String,
    pub to2: String,
    pub to3: String,
    pub to4: String,
    pub to5: String,
    #[serde(default = "defaults::notify_email_smtp_host")]
    pub smtp_host: String,
    #[serde(default = "defaults::notify_email_smtp_port")]
    pub smtp_port: u16,
    pub smtp_username: Option<String>,
    pub smtp_password: Option<String>,
    #[serde(default = "defaults::notify_email_smtp_encrypt")]
    pub smtp_encrypt: bool,
    #[serde(default = "defaults::notify_email_reminders_only")]
    pub reminders_only: bool,
}
  • email 发送部分

    email 使用了lettre 包,但是新旧版本有点不一样,需要注意使用的是0.8,此处代码写死了4个人

            let email_message = EmailBuilder::new()
                .to(email_config.to.as_str())
                .to(email_config.to2.as_str())
                .to(email_config.to3.as_str())
                .to(email_config.to4.as_str())
                .from((
                    email_config.from.as_str(),
                    APP_CONF.branding.page_title.as_str(),
                ))
                .subject(format!(
                    "{} | {}",
                    notification.status.as_str().to_uppercase(),
                    &nodes_label
                ))
                .text(message)
                .build()
                .or(Err(true))?;
 

关于构建

Vigil 需要使用nightly 版本,但应该是1.36起对于lettre 有破坏行影响,同时官方也修改了代码
兼容问题很多,解决方法, 安装指定日志版本的nightly 版本,我构建使用docker 多阶段处理
参考dockerfile

FROM rustlang/rust:nightly AS build
USER root
WORKDIR /app
COPY . /app
# 处理构建工具版本的核心部分
RUN rustup install nightly-2019-01-17
RUN rustup default nightly-2019-01-17-x86_64-unknown-linux-gnu
RUN cargo -V
RUN cargo build --release
FROM debian:stretch-slim
WORKDIR /usr/src/vigil
COPY ./res/assets/ ./res/assets/
COPY --from=build /app/target/release/vigil /usr/local/bin/vigil
COPY ./res/assets/ ./res/assets/
RUN apt-get update
RUN apt-get install -y libstrophe-dev libssl-dev
CMD [ "vigil", "-c", "/etc/vigil.cfg" ]
EXPOSE 8080
 

配置文件说明

现在我们可以配置4个可以发送的邮箱了,参考格式

[notify.email]
from = "status@crisp.chat"
to = "status@crisp.chat"
to2 = "status@crisp.chat"
to3 = "status@crisp.chat"
to4 = "status@crisp.chat"
to5 = "status@crisp.chat"
smtp_host = "localhost"
smtp_port = 587
smtp_username = "user-access"
smtp_password = "user-password"
smtp_encrypt = false

说明

修改的代码部分很简单,也很low,主要是关于构建工具的问题,版本依赖太严重,实际上碰到问题还是
多看看官方文档,深入了解下工具,这样可以加速我们解决问题,比如上边的关于构建工具版本的,主要
就是下载指定版本的

 
rustup install nightly-2019-01-17
rustup default nightly-2019-01-17-x86_64-unknown-linux-gnu

参考资料

https://github.com/rongfengliang/myvigil-multiemail
https://www.rust-lang.org/tools/install
https://forge.rust-lang.org/other-installation-methods.html

原文地址:https://www.cnblogs.com/rongfengliang/p/11026526.html