| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- use serde::{Deserialize, Serialize};
-
- /// 邮件内容类型
- #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
- pub enum ContentType {
- /// 纯文本
- Text,
- /// HTML格式
- Html,
- }
-
- /// 邮件优先级
- #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
- pub enum Priority {
- Low,
- Normal,
- High,
- }
-
- /// 邮件请求
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct EmailRequest {
- /// 收件人邮箱
- pub to: String,
- /// 收件人姓名(可选)
- pub to_name: Option<String>,
- /// 邮件主题
- pub subject: String,
- /// 邮件正文
- pub body: String,
- /// 内容类型
- pub content_type: ContentType,
- /// 优先级
- pub priority: Option<Priority>,
- /// 抄送列表(可选)
- pub cc: Option<Vec<String>>,
- /// 密送列表(可选)
- pub bcc: Option<Vec<String>>,
- }
-
- impl Default for EmailRequest {
- fn default() -> Self {
- Self {
- to: String::new(),
- to_name: None,
- subject: String::new(),
- body: String::new(),
- content_type: ContentType::Text,
- priority: Some(Priority::Normal),
- cc: None,
- bcc: None,
- }
- }
- }
-
- impl EmailRequest {
- /// 创建简单的文本邮件请求
- pub fn simple(to: &str, subject: &str, body: &str) -> Self {
- Self {
- to: to.to_string(),
- to_name: None,
- subject: subject.to_string(),
- body: body.to_string(),
- content_type: ContentType::Text,
- priority: Some(Priority::Normal),
- cc: None,
- bcc: None,
- }
- }
-
- /// 创建HTML邮件请求
- pub fn html(to: &str, subject: &str, html_body: &str) -> Self {
- Self {
- to: to.to_string(),
- to_name: None,
- subject: subject.to_string(),
- body: html_body.to_string(),
- content_type: ContentType::Html,
- priority: Some(Priority::Normal),
- cc: None,
- bcc: None,
- }
- }
- }
-
- /// 邮件发送结果
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct EmailResult {
- /// 是否成功
- pub success: bool,
- /// 收件人
- pub recipient: String,
- /// 错误信息(如果有)
- pub error: Option<String>,
- /// 消息ID(SMTP服务器返回)
- pub message_id: Option<String>,
- }
-
- impl EmailResult {
- /// 创建成功结果
- pub fn success(recipient: &str, message_id: Option<&str>) -> Self {
- Self {
- success: true,
- recipient: recipient.to_string(),
- error: None,
- message_id: message_id.map(|s| s.to_string()),
- }
- }
-
- /// 创建失败结果
- pub fn failure(recipient: &str, error: &str) -> Self {
- Self {
- success: false,
- recipient: recipient.to_string(),
- error: Some(error.to_string()),
- message_id: None,
- }
- }
- }
|