使用rust在debian上定时发送邮件给指定邮箱

models.rs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. use serde::{Deserialize, Serialize};
  2. /// 邮件内容类型
  3. #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
  4. pub enum ContentType {
  5. /// 纯文本
  6. Text,
  7. /// HTML格式
  8. Html,
  9. }
  10. /// 邮件优先级
  11. #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
  12. pub enum Priority {
  13. Low,
  14. Normal,
  15. High,
  16. }
  17. /// 邮件请求
  18. #[derive(Debug, Clone, Serialize, Deserialize)]
  19. pub struct EmailRequest {
  20. /// 收件人邮箱
  21. pub to: String,
  22. /// 收件人姓名(可选)
  23. pub to_name: Option<String>,
  24. /// 邮件主题
  25. pub subject: String,
  26. /// 邮件正文
  27. pub body: String,
  28. /// 内容类型
  29. pub content_type: ContentType,
  30. /// 优先级
  31. pub priority: Option<Priority>,
  32. /// 抄送列表(可选)
  33. pub cc: Option<Vec<String>>,
  34. /// 密送列表(可选)
  35. pub bcc: Option<Vec<String>>,
  36. }
  37. impl Default for EmailRequest {
  38. fn default() -> Self {
  39. Self {
  40. to: String::new(),
  41. to_name: None,
  42. subject: String::new(),
  43. body: String::new(),
  44. content_type: ContentType::Text,
  45. priority: Some(Priority::Normal),
  46. cc: None,
  47. bcc: None,
  48. }
  49. }
  50. }
  51. impl EmailRequest {
  52. /// 创建简单的文本邮件请求
  53. pub fn simple(to: &str, subject: &str, body: &str) -> Self {
  54. Self {
  55. to: to.to_string(),
  56. to_name: None,
  57. subject: subject.to_string(),
  58. body: body.to_string(),
  59. content_type: ContentType::Text,
  60. priority: Some(Priority::Normal),
  61. cc: None,
  62. bcc: None,
  63. }
  64. }
  65. /// 创建HTML邮件请求
  66. pub fn html(to: &str, subject: &str, html_body: &str) -> Self {
  67. Self {
  68. to: to.to_string(),
  69. to_name: None,
  70. subject: subject.to_string(),
  71. body: html_body.to_string(),
  72. content_type: ContentType::Html,
  73. priority: Some(Priority::Normal),
  74. cc: None,
  75. bcc: None,
  76. }
  77. }
  78. }
  79. /// 邮件发送结果
  80. #[derive(Debug, Clone, Serialize, Deserialize)]
  81. pub struct EmailResult {
  82. /// 是否成功
  83. pub success: bool,
  84. /// 收件人
  85. pub recipient: String,
  86. /// 错误信息(如果有)
  87. pub error: Option<String>,
  88. /// 消息ID(SMTP服务器返回)
  89. pub message_id: Option<String>,
  90. }
  91. impl EmailResult {
  92. /// 创建成功结果
  93. pub fn success(recipient: &str, message_id: Option<&str>) -> Self {
  94. Self {
  95. success: true,
  96. recipient: recipient.to_string(),
  97. error: None,
  98. message_id: message_id.map(|s| s.to_string()),
  99. }
  100. }
  101. /// 创建失败结果
  102. pub fn failure(recipient: &str, error: &str) -> Self {
  103. Self {
  104. success: false,
  105. recipient: recipient.to_string(),
  106. error: Some(error.to_string()),
  107. message_id: None,
  108. }
  109. }
  110. }