Skip to main content

br_email/
lib.rs

1use crate::analyze::AnalyzeEmails;
2#[cfg(feature = "imap")]
3use crate::imap::Imap;
4#[cfg(feature = "pop3")]
5use crate::pop3::Pop3;
6#[cfg(feature = "smtp")]
7use crate::smtp::Smtp;
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeMap;
10use std::io::Error;
11use std::path::PathBuf;
12use std::{fs, io};
13
14#[cfg(feature = "imap")]
15mod imap;
16#[cfg(feature = "pop3")]
17mod pop3;
18#[cfg(feature = "smtp")]
19mod smtp;
20
21pub mod analyze;
22pub mod pools;
23
24#[derive(Clone, Debug)]
25pub struct Mail {
26    /// 协议
27    protocol: Protocol,
28    /// 邮件地址
29    host: String,
30    /// 邮件端口
31    port: u16,
32    /// 邮件名称
33    name: String,
34    /// 邮件账号
35    mail: String,
36    /// 邮件密码
37    pass: String,
38    /// 邮件类型
39    email_type: EmailType,
40}
41
42impl Mail {
43    pub fn new_new(config: Config) -> Result<Self, String> {
44        let default = config.default.clone();
45        let connection = config
46            .connections
47            .get(&default)
48            .ok_or_else(|| format!("默认配置不存在: {default}"))?
49            .clone();
50        Ok(Self {
51            protocol: connection.protocol,
52            host: connection.host,
53            port: connection.port,
54            name: connection.name,
55            mail: connection.mail,
56            pass: connection.pass,
57            email_type: connection.email_type,
58        })
59    }
60    pub fn new(protocol: &str, host: &str, port: u16, name: &str, mail: &str, pass: &str) -> Self {
61        Self {
62            protocol: Protocol::from(protocol),
63            host: host.to_string(),
64            port,
65            name: name.to_string(),
66            mail: mail.to_string(),
67            pass: pass.to_string(),
68            email_type: EmailType::from(mail),
69        }
70    }
71    /// 解析邮件
72    pub fn analyze(data: Vec<u8>, debug: bool) -> io::Result<AnalyzeEmails> {
73        AnalyzeEmails::new(data, debug)
74    }
75    /// 接收
76    pub fn receive(&mut self) -> Box<dyn Receive> {
77        match self.protocol {
78            #[cfg(feature = "pop3")]
79            Protocol::POP3 => Box::new(Pop3::new(
80                self.host.clone(),
81                self.port,
82                self.name.clone(),
83                self.mail.clone(),
84                self.pass.clone(),
85                self.email_type.clone(),
86            )),
87            #[cfg(feature = "smtp")]
88            Protocol::Smtp => Box::new(Smtp::new(
89                self.host.clone(),
90                self.port,
91                self.name.clone(),
92                self.mail.clone(),
93                self.pass.clone(),
94                self.email_type.clone(),
95            )),
96            #[cfg(feature = "imap")]
97            Protocol::Imap => Box::new(Imap::new(
98                self.host.clone(),
99                self.port,
100                self.name.clone(),
101                self.mail.clone(),
102                self.pass.clone(),
103                self.email_type.clone(),
104            )),
105            _ => Box::new(UnsupportedReceive {
106                protocol: self.protocol.clone(),
107            }),
108        }
109    }
110    /// 发送
111    pub fn sender(&mut self) -> Box<dyn Sender> {
112        match self.protocol {
113            #[cfg(feature = "pop3")]
114            Protocol::POP3 => Box::new(Pop3::new(
115                self.host.clone(),
116                self.port,
117                self.name.clone(),
118                self.mail.clone(),
119                self.pass.clone(),
120                self.email_type.clone(),
121            )),
122            #[cfg(feature = "smtp")]
123            Protocol::Smtp => Box::new(Smtp::new(
124                self.host.clone(),
125                self.port,
126                self.name.clone(),
127                self.mail.clone(),
128                self.pass.clone(),
129                self.email_type.clone(),
130            )),
131            #[cfg(feature = "imap")]
132            Protocol::Imap => Box::new(Imap::new(
133                self.host.clone(),
134                self.port,
135                self.name.clone(),
136                self.mail.clone(),
137                self.pass.clone(),
138                self.email_type.clone(),
139            )),
140            _ => Box::new(UnsupportedSender {
141                protocol: self.protocol.clone(),
142            }),
143        }
144    }
145}
146
147pub trait Receive {
148    /// 获取邮件总数
149    fn get_total(&mut self) -> Result<usize, Error>;
150    /// 获取指定量ID列表
151    fn get_id_list(&mut self) -> Result<Vec<String>, Error>;
152    /// 获取指定ID邮件
153    /// 返回 (邮件内容,邮件尺寸,邮件指纹)
154    fn get_id_info(&mut self, id: &str) -> Result<(Vec<u8>, usize, String), Error>;
155    /// 删除指定邮件
156    fn del_id(&mut self, id: &str) -> Result<bool, Error>;
157
158    /// 获取唯一标识符列表
159    /// IMAP: 返回 UID 列表
160    /// POP3: 返回 (序号, UIDL) 列表中的 UIDL
161    fn get_uid_list(&mut self) -> Result<Vec<String>, Error> {
162        self.get_id_list()
163    }
164
165    /// 获取指定 UID 之后的新邮件列表 (增量查询)
166    /// IMAP: 使用 UID SEARCH
167    /// POP3: 需要配合本地存储比对
168    fn get_new_since(&mut self, _last_uid: &str) -> Result<Vec<String>, Error> {
169        Err(Error::other("此协议不支持增量查询"))
170    }
171
172    /// 获取邮箱有效性标识
173    /// IMAP: 返回 UIDVALIDITY
174    /// POP3: 返回 None (POP3 无此概念)
175    fn get_validity(&mut self) -> Result<Option<u64>, Error> {
176        Ok(None)
177    }
178
179    /// 获取最大 UID (用于记录同步点)
180    fn get_max_uid(&mut self) -> Result<Option<u64>, Error> {
181        Ok(None)
182    }
183
184    /// 获取 UIDL 映射表 (仅 POP3)
185    /// 返回 Vec<(序号, UIDL)>
186    fn get_uidl_map(&mut self) -> Result<Vec<(String, String)>, Error> {
187        Err(Error::other("此协议不支持 UIDL"))
188    }
189}
190
191pub trait Sender {
192    /// 发送邮件
193    /// 返回 (邮件内容,邮件尺寸,邮件指纹)
194    fn send(&mut self) -> Result<(Vec<u8>, usize, String), Error>;
195
196    /// 设置发件人信息
197    /// * name 发件人名称
198    /// * mail 发件箱
199    fn set_from(&mut self, name: &str) -> &mut dyn Sender;
200
201    /// 收件人
202    /// * name 收件人名称
203    /// * mail 收件箱
204    fn set_to(&mut self, name: &str, mail: &str) -> &mut dyn Sender;
205    /// 抄送人
206    /// * name 抄送人名称
207    /// * mail 收件箱
208    fn set_cc(&mut self, name: &str, mail: &str) -> &mut dyn Sender;
209    /// 设置主题
210    fn set_subject(&mut self, subject: &str) -> &mut dyn Sender;
211    /// 设置内容
212    fn set_body(&mut self, text: &str, html: &str) -> &mut dyn Sender;
213    /// 设置文件
214    fn set_file(&mut self, name: &str, data: Vec<u8>, content_type: &str) -> &mut dyn Sender;
215}
216
217struct UnsupportedReceive {
218    protocol: Protocol,
219}
220
221impl UnsupportedReceive {
222    fn err(&self, action: &str) -> Error {
223        Error::other(format!("{action} 不支持协议: {:?}", self.protocol))
224    }
225}
226
227impl Receive for UnsupportedReceive {
228    fn get_total(&mut self) -> Result<usize, Error> {
229        Err(self.err("获取邮件总数"))
230    }
231
232    fn get_id_list(&mut self) -> Result<Vec<String>, Error> {
233        Err(self.err("获取邮件ID列表"))
234    }
235
236    fn get_id_info(&mut self, _id: &str) -> Result<(Vec<u8>, usize, String), Error> {
237        Err(self.err("获取邮件内容"))
238    }
239
240    fn del_id(&mut self, _id: &str) -> Result<bool, Error> {
241        Err(self.err("删除邮件"))
242    }
243}
244
245struct UnsupportedSender {
246    protocol: Protocol,
247}
248
249impl UnsupportedSender {
250    fn err(&self, action: &str) -> Error {
251        Error::other(format!("{action} 不支持协议: {:?}", self.protocol))
252    }
253}
254
255impl Sender for UnsupportedSender {
256    fn send(&mut self) -> Result<(Vec<u8>, usize, String), Error> {
257        Err(self.err("发送邮件"))
258    }
259
260    fn set_from(&mut self, _name: &str) -> &mut dyn Sender {
261        self
262    }
263
264    fn set_to(&mut self, _name: &str, _mail: &str) -> &mut dyn Sender {
265        self
266    }
267
268    fn set_cc(&mut self, _name: &str, _mail: &str) -> &mut dyn Sender {
269        self
270    }
271
272    fn set_subject(&mut self, _subject: &str) -> &mut dyn Sender {
273        self
274    }
275
276    fn set_body(&mut self, _text: &str, _html: &str) -> &mut dyn Sender {
277        self
278    }
279
280    fn set_file(&mut self, _name: &str, _data: Vec<u8>, _content_type: &str) -> &mut dyn Sender {
281        self
282    }
283}
284
285#[derive(Clone, Debug, Deserialize, Serialize)]
286enum Protocol {
287    POP3,
288    Smtp,
289    Imap,
290    None,
291}
292
293impl Protocol {
294    pub fn from(name: &str) -> Self {
295        let name = name.to_lowercase();
296        match name.as_str() {
297            "pop3" => Protocol::POP3,
298            "imap" => Protocol::Imap,
299            "smtp" => Protocol::Smtp,
300            _ => Protocol::None,
301        }
302    }
303}
304
305#[derive(Clone, Debug, Deserialize, Serialize)]
306enum EmailType {
307    Google,
308    Qq,
309    None,
310}
311impl EmailType {
312    pub fn from(email: &str) -> Self {
313        match email {
314            x if x.contains("@gmail.com") => EmailType::Google,
315            x if x.contains("@qq.com") => EmailType::Qq,
316            _ => EmailType::None,
317        }
318    }
319}
320
321#[derive(Clone, Debug, Deserialize, Serialize)]
322pub struct Config {
323    default: String,
324    connections: BTreeMap<String, Connection>,
325}
326impl Default for Config {
327    fn default() -> Self {
328        Self::new()
329    }
330}
331
332impl Config {
333    pub fn create(config_file: PathBuf, pkg_name: bool) -> Config {
334        #[derive(Clone, Debug, Deserialize, Serialize)]
335        pub struct ConfigNew {
336            pub br_email: Config,
337        }
338        impl ConfigNew {
339            pub fn new() -> ConfigNew {
340                let mut connections = BTreeMap::new();
341                connections.insert("my_name".to_string(), Connection::default());
342                Self {
343                    br_email: Config {
344                        default: "my_name".to_string(),
345                        connections,
346                    },
347                }
348            }
349        }
350        match fs::read_to_string(config_file.clone()) {
351            Ok(e) => {
352                if pkg_name {
353                    let data = ConfigNew::new();
354                    toml::from_str::<ConfigNew>(&e)
355                        .unwrap_or_else(|_| {
356                            let toml = toml::to_string(&data).unwrap();
357                            let toml = format!("{e}\r\n{toml}");
358                            let _ = fs::write(config_file.to_str().unwrap(), toml);
359                            data
360                        })
361                        .br_email
362                } else {
363                    let data = Config::new();
364                    toml::from_str::<Config>(&e).unwrap_or_else(|_| {
365                        let toml = toml::to_string(&data).unwrap();
366                        let toml = format!("{e}\r\n{toml}");
367                        let _ = fs::write(config_file.to_str().unwrap(), toml);
368                        data
369                    })
370                }
371            }
372            Err(_) => {
373                if pkg_name {
374                    let data = ConfigNew::new();
375                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
376                    let toml = toml::to_string(&data).unwrap();
377                    let _ = fs::write(config_file.to_str().unwrap(), toml);
378                    data.br_email
379                } else {
380                    let data = Config::new();
381                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
382                    let toml = toml::to_string(&data).unwrap();
383                    let _ = fs::write(config_file.to_str().unwrap(), toml);
384                    data
385                }
386            }
387        }
388    }
389    pub fn new() -> Self {
390        let mut connections = BTreeMap::new();
391        connections.insert("my_name".to_string(), Connection::default());
392        Self {
393            default: "my_name".to_string(),
394            connections,
395        }
396    }
397}
398#[derive(Clone, Debug, Deserialize, Serialize)]
399struct Connection {
400    /// 协议
401    protocol: Protocol,
402    /// 邮件地址
403    host: String,
404    /// 邮件端口
405    port: u16,
406    /// 邮件名称
407    name: String,
408    /// 邮件账号
409    mail: String,
410    /// 邮件密码
411    pass: String,
412    /// 邮件类型
413    email_type: EmailType,
414}
415impl Connection {
416    pub fn default() -> Connection {
417        Self {
418            protocol: Protocol::Smtp,
419            host: "smtp.exmail.qq.com".to_string(),
420            port: 25,
421            name: "发件人名".to_string(),
422            mail: "xxx@qq.com".to_string(),
423            pass: "*******".to_string(),
424            email_type: EmailType::Qq,
425        }
426    }
427}
428
429#[cfg(test)]
430mod tests {
431    use super::*;
432    use std::time::{SystemTime, UNIX_EPOCH};
433
434    fn temp_config_path(test_name: &str) -> PathBuf {
435        let nanos = SystemTime::now()
436            .duration_since(UNIX_EPOCH)
437            .unwrap()
438            .as_nanos();
439        std::env::temp_dir().join(format!(
440            "br_email_{test_name}_{}_{}.toml",
441            std::process::id(),
442            nanos
443        ))
444    }
445
446    #[test]
447    fn test_protocol_from() {
448        assert!(matches!(Protocol::from("pop3"), Protocol::POP3));
449        assert!(matches!(Protocol::from("POP3"), Protocol::POP3));
450        assert!(matches!(Protocol::from("imap"), Protocol::Imap));
451        assert!(matches!(Protocol::from("IMAP"), Protocol::Imap));
452        assert!(matches!(Protocol::from("smtp"), Protocol::Smtp));
453        assert!(matches!(Protocol::from("SMTP"), Protocol::Smtp));
454        assert!(matches!(Protocol::from("unknown"), Protocol::None));
455    }
456
457    #[test]
458    fn test_email_type_from() {
459        assert!(matches!(
460            EmailType::from("user@gmail.com"),
461            EmailType::Google
462        ));
463        assert!(matches!(EmailType::from("user@qq.com"), EmailType::Qq));
464        assert!(matches!(
465            EmailType::from("user@outlook.com"),
466            EmailType::None
467        ));
468    }
469
470    #[test]
471    fn test_mail_new() {
472        let mail = Mail::new(
473            "imap",
474            "imap.gmail.com",
475            993,
476            "test",
477            "user@gmail.com",
478            "pass",
479        );
480        assert!(matches!(mail.protocol, Protocol::Imap));
481        assert_eq!(mail.host, "imap.gmail.com");
482        assert_eq!(mail.port, 993);
483        assert_eq!(mail.name, "test");
484        assert_eq!(mail.mail, "user@gmail.com");
485        assert_eq!(mail.pass, "pass");
486    }
487
488    #[test]
489    fn test_config_new() {
490        let config = Config::new();
491        assert_eq!(config.default, "my_name");
492        assert!(config.connections.contains_key("my_name"));
493    }
494
495    #[test]
496    fn test_config_default() {
497        let config = Config::default();
498        assert_eq!(config.default, "my_name");
499    }
500
501    #[test]
502    fn test_connection_default() {
503        let conn = Connection::default();
504        assert!(matches!(conn.protocol, Protocol::Smtp));
505        assert_eq!(conn.host, "smtp.exmail.qq.com");
506        assert_eq!(conn.port, 25);
507    }
508
509    #[test]
510    fn test_receive_dispatch_for_supported_protocols() {
511        let mut pop3_mail = Mail::new("pop3", "localhost", 995, "n", "m@m.com", "p");
512        let _pop3_receiver = pop3_mail.receive();
513
514        let mut smtp_mail = Mail::new("smtp", "localhost", 587, "n", "m@m.com", "p");
515        let _smtp_receiver = smtp_mail.receive();
516
517        let mut imap_mail = Mail::new("imap", "localhost", 993, "n", "m@m.com", "p");
518        let _imap_receiver = imap_mail.receive();
519    }
520
521    #[test]
522    fn test_sender_dispatch_for_supported_protocols() {
523        let mut pop3_mail = Mail::new("pop3", "localhost", 995, "n", "m@m.com", "p");
524        let mut pop3_sender = pop3_mail.sender();
525        pop3_sender.set_from("sender");
526        pop3_sender.set_to("to", "to@test.com");
527        pop3_sender.set_cc("cc", "cc@test.com");
528        pop3_sender.set_subject("subject");
529        pop3_sender.set_body("text", "<p>html</p>");
530        pop3_sender.set_file("attachment.txt", vec![1, 2, 3], "text/plain");
531
532        let mut smtp_mail = Mail::new("smtp", "localhost", 587, "n", "m@m.com", "p");
533        let mut smtp_sender = smtp_mail.sender();
534        smtp_sender.set_from("sender");
535        smtp_sender.set_to("to", "to@test.com");
536        smtp_sender.set_cc("cc", "cc@test.com");
537        smtp_sender.set_subject("subject");
538        smtp_sender.set_body("text", "<p>html</p>");
539        smtp_sender.set_file("attachment.txt", vec![1, 2, 3], "text/plain");
540
541        let mut imap_mail = Mail::new("imap", "localhost", 993, "n", "m@m.com", "p");
542        let mut imap_sender = imap_mail.sender();
543        imap_sender.set_from("sender");
544        imap_sender.set_to("to", "to@test.com");
545        imap_sender.set_cc("cc", "cc@test.com");
546        imap_sender.set_subject("subject");
547        imap_sender.set_body("text", "<p>html</p>");
548        imap_sender.set_file("attachment.txt", vec![1, 2, 3], "text/plain");
549    }
550
551    #[test]
552    fn test_config_create_file_not_exists_without_pkg_name() {
553        let config_path = temp_config_path("create_without_pkg_missing");
554        std::fs::remove_file(&config_path).ok();
555
556        let config = Config::create(config_path.clone(), false);
557        assert_eq!(config.default, "my_name");
558        assert!(config.connections.contains_key("my_name"));
559        assert!(config_path.exists());
560
561        std::fs::remove_file(&config_path).ok();
562    }
563
564    #[test]
565    fn test_config_create_file_not_exists_with_pkg_name() {
566        let config_path = temp_config_path("create_with_pkg_missing");
567        std::fs::remove_file(&config_path).ok();
568
569        let config = Config::create(config_path.clone(), true);
570        assert_eq!(config.default, "my_name");
571        assert!(config.connections.contains_key("my_name"));
572        let file_content = std::fs::read_to_string(&config_path).unwrap();
573        assert!(file_content.contains("[br_email]"));
574
575        std::fs::remove_file(&config_path).ok();
576    }
577
578    #[test]
579    fn test_config_create_existing_valid_toml_without_pkg_name() {
580        let config_path = temp_config_path("create_without_pkg_valid");
581        std::fs::remove_file(&config_path).ok();
582
583        let config_content = r#"default = "my_name"
584
585[connections.my_name]
586protocol = "Smtp"
587host = "smtp.test.com"
588port = 25
589name = "test"
590mail = "test@test.com"
591pass = "pass"
592email_type = "None"
593"#;
594        std::fs::write(&config_path, config_content).unwrap();
595
596        let config = Config::create(config_path.clone(), false);
597        assert_eq!(config.default, "my_name");
598        assert_eq!(
599            config.connections.get("my_name").unwrap().host,
600            "smtp.test.com"
601        );
602
603        std::fs::remove_file(&config_path).ok();
604    }
605
606    #[test]
607    fn test_config_create_existing_valid_toml_with_pkg_name() {
608        let config_path = temp_config_path("create_with_pkg_valid");
609        std::fs::remove_file(&config_path).ok();
610
611        let config_content = r#"[br_email]
612default = "my_name"
613
614[br_email.connections.my_name]
615protocol = "Smtp"
616host = "smtp.test.com"
617port = 25
618name = "test"
619mail = "test@test.com"
620pass = "pass"
621email_type = "None"
622"#;
623        std::fs::write(&config_path, config_content).unwrap();
624
625        let config = Config::create(config_path.clone(), true);
626        assert_eq!(config.default, "my_name");
627        assert_eq!(
628            config.connections.get("my_name").unwrap().host,
629            "smtp.test.com"
630        );
631
632        std::fs::remove_file(&config_path).ok();
633    }
634
635    #[test]
636    fn test_config_create_existing_invalid_toml_with_pkg_name_fallback() {
637        let config_path = temp_config_path("create_with_pkg_invalid");
638        std::fs::remove_file(&config_path).ok();
639
640        std::fs::write(&config_path, "invalid toml content!!!").unwrap();
641        let config = Config::create(config_path.clone(), true);
642
643        assert_eq!(config.default, "my_name");
644        assert!(config.connections.contains_key("my_name"));
645        let file_content = std::fs::read_to_string(&config_path).unwrap();
646        assert!(file_content.contains("invalid toml content!!!"));
647        assert!(file_content.contains("[br_email]"));
648
649        std::fs::remove_file(&config_path).ok();
650    }
651
652    #[test]
653    fn test_config_create_existing_invalid_toml_without_pkg_name_fallback() {
654        let config_path = temp_config_path("create_without_pkg_invalid");
655        std::fs::remove_file(&config_path).ok();
656
657        std::fs::write(&config_path, "invalid toml content!!!").unwrap();
658        let config = Config::create(config_path.clone(), false);
659
660        assert_eq!(config.default, "my_name");
661        assert!(config.connections.contains_key("my_name"));
662        let file_content = std::fs::read_to_string(&config_path).unwrap();
663        assert!(file_content.contains("invalid toml content!!!"));
664        assert!(file_content.contains("[connections.my_name]"));
665
666        std::fs::remove_file(&config_path).ok();
667    }
668
669    #[test]
670    fn test_mail_new_new_success() {
671        let mut config = Config::new();
672        config.default = "test_conn".to_string();
673        config.connections.insert(
674            "test_conn".to_string(),
675            Connection {
676                protocol: Protocol::Imap,
677                host: "imap.test.com".to_string(),
678                port: 993,
679                name: "Test".to_string(),
680                mail: "test@test.com".to_string(),
681                pass: "password".to_string(),
682                email_type: EmailType::None,
683            },
684        );
685
686        let mail = Mail::new_new(config).unwrap();
687        assert_eq!(mail.host, "imap.test.com");
688        assert_eq!(mail.port, 993);
689    }
690
691    #[test]
692    fn test_mail_new_new_missing_default() {
693        let mut config = Config::new();
694        config.default = "nonexistent".to_string();
695
696        let result = Mail::new_new(config);
697        assert!(result.is_err());
698    }
699
700    #[test]
701    fn test_unsupported_receive() {
702        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
703        let mut receiver = mail.receive();
704
705        assert!(receiver.get_total().is_err());
706        assert!(receiver.get_id_list().is_err());
707        assert!(receiver.get_id_info("1").is_err());
708        assert!(receiver.del_id("1").is_err());
709    }
710
711    #[test]
712    fn test_unsupported_sender() {
713        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
714        let mut sender = mail.sender();
715
716        assert!(sender.send().is_err());
717        sender.set_from("test");
718        sender.set_to("name", "mail");
719        sender.set_cc("name", "mail");
720        sender.set_subject("subject");
721        sender.set_body("text", "html");
722        sender.set_file("name", vec![], "type");
723    }
724
725    #[test]
726    fn test_receive_trait_default_methods() {
727        let mut mail = Mail::new("unknown", "host", 123, "name", "mail", "pass");
728        let mut receiver = mail.receive();
729
730        assert!(receiver.get_uid_list().is_err());
731        assert!(receiver.get_new_since("100").is_err());
732        assert!(receiver.get_validity().unwrap().is_none());
733        assert!(receiver.get_max_uid().unwrap().is_none());
734        assert!(receiver.get_uidl_map().is_err());
735    }
736
737    #[test]
738    fn test_mail_analyze_valid() {
739        let email_data = b"From: sender@example.com\r\n\
740To: receiver@example.com\r\n\
741Subject: Test\r\n\
742Content-Type: text/plain\r\n\
743Date: 01 Jan 2024 12:00:00 +0000\r\n\
744\r\n\
745Body"
746            .to_vec();
747
748        let result = Mail::analyze(email_data, false);
749        assert!(result.is_ok());
750        let email = result.unwrap();
751        assert_eq!(email.subject, "Test");
752    }
753
754    #[test]
755    fn test_mail_analyze_invalid() {
756        let invalid_data = b"invalid email".to_vec();
757        let result = Mail::analyze(invalid_data, false);
758        assert!(result.is_err());
759    }
760
761    #[test]
762    fn test_mail_with_different_protocols() {
763        let pop3_mail = Mail::new("pop3", "pop.test.com", 995, "name", "user@test.com", "pass");
764        assert!(matches!(pop3_mail.protocol, Protocol::POP3));
765
766        let smtp_mail = Mail::new(
767            "smtp",
768            "smtp.test.com",
769            587,
770            "name",
771            "user@test.com",
772            "pass",
773        );
774        assert!(matches!(smtp_mail.protocol, Protocol::Smtp));
775
776        let imap_mail = Mail::new(
777            "imap",
778            "imap.test.com",
779            993,
780            "name",
781            "user@test.com",
782            "pass",
783        );
784        assert!(matches!(imap_mail.protocol, Protocol::Imap));
785    }
786
787    #[test]
788    fn test_email_type_detection() {
789        let gmail = Mail::new(
790            "imap",
791            "imap.gmail.com",
792            993,
793            "name",
794            "user@gmail.com",
795            "pass",
796        );
797        assert!(matches!(gmail.email_type, EmailType::Google));
798
799        let qq = Mail::new("imap", "imap.qq.com", 993, "name", "user@qq.com", "pass");
800        assert!(matches!(qq.email_type, EmailType::Qq));
801
802        let other = Mail::new(
803            "imap",
804            "imap.other.com",
805            993,
806            "name",
807            "user@other.com",
808            "pass",
809        );
810        assert!(matches!(other.email_type, EmailType::None));
811    }
812}