mail.py 2.41 KB
Newer Older
kaiyou's avatar
kaiyou committed
1
import imaplib
2
3
import smtplib
import os
kaiyou's avatar
kaiyou committed
4
import time
5
import faker
kaiyou's avatar
kaiyou committed
6

7
from email.mime import text, multipart
kaiyou's avatar
kaiyou committed
8

9
10
from amonit.check import base

kaiyou's avatar
kaiyou committed
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_imap_client(host, port, encrypt):
    if encrypt == "starttls":
        client = imaplib.IMAP4(host=host, port=port)
        client.starttls()
    elif encrypt == "tls":
        client = imaplib.IMAP4_SSL(host=host, port=port)
    else:
        client = imaplib.IMAP4(host=host, port=port)
    return client


def get_smtp_client(host, port, encrypt):
    if encrypt == "starttls":
        client = smtplib.SMTP(host=host, port=port)
        client.starttls()
    elif encrypt == "tls":
        client = smtp.SMTP_SSL(host=host, port=port)
    else:
        client = smtplib.SMTP(host=host, port=port)
    return client


def imap_login(context, host, username, password, port=143, encrypt=None):
kaiyou's avatar
kaiyou committed
35
36
    """ Check that IMAP login works properly and accesses the INBOX
    """
37
    client = get_imap_client(host, port, encrypt)
kaiyou's avatar
kaiyou committed
38
39
40
    client.login(username, password)
    client.logout()

41

42
43
def send_mail(context, account, to, message):
    """ Send a message to the given account
44
    """
45
    fake = faker.Faker()
46
    smtp = get_smtp_client(
47
48
        account["host"], account.get("port", 587),
        account.get("encrypt", None)
49
    )
50
51
    smtp.login(account["username"], account["password"])
    mail = multipart.MIMEMultipart("alternative")
52
53
    mail["Subject"] = fake.sentence(nb_words=6, variable_nb_words=True)
    mail["Subject"] += message
54
55
    mail["From"] = account["username"]
    mail["To"] = to
kaiyou's avatar
kaiyou committed
56
    mail.attach(text.MIMEText("\n".join(fake.paragraphs(nb=5)), "text"))
57
    smtp.sendmail(account["username"], to, mail.as_string())
58
    smtp.quit()
59
60
61
62
63


def lookup_mail(context, account, to, message):
    """ Look for a specific message in a mailbox, then delete it
    """
kaiyou's avatar
kaiyou committed
64
    client = get_imap_client(
65
66
        account["host"], account.get("port", 143),
        account.get("encrypt", None)
kaiyou's avatar
kaiyou committed
67
    )
68
69
70
71
72
73
74
75
    client.login(account["username"], account["password"])
    client.select(account["inbox"])
    filter_string = f'UNSEEN SUBJECT {message}'
    status, data = client.uid("search", None, filter_string)
    if status == "OK" and data[0]:
        for uid in data[0].split():
            client.uid("store", uid, "+FLAGS", "(\\Deleted)")
        client.expunge()
kaiyou's avatar
kaiyou committed
76
77
    client.close()
    client.logout()
78
79
80
81
82
83
    return bool(status == "OK" and data[0])


@base.send_then_receive(send_mail, lookup_mail)
def send_then_receive():
    pass