-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailfilter
78 lines (64 loc) · 1.76 KB
/
mailfilter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
input {
exec {
command => "ruby /path/to/fetch_unseen_emails.rb"
interval => 300 # Fetch every 5 minutes
}
}
filter {
ruby {
code => "
require 'net/imap'
require 'mail'
def fetch_and_delete_emails(server:, port:, username:, password:, folder: 'INBOX')
imap = Net::IMAP.new(server, port, ssl: true)
imap.login(username, password)
imap.select(folder)
unseen_ids = imap.search(['UNSEEN'])
emails = []
unseen_ids.each do |email_id|
message_data = imap.fetch(email_id, 'RFC822').first
raw_email = message_data.attr['RFC822']
mail = Mail.read_from_string(raw_email)
attachments = mail.attachments.map do |attachment|
{
filename: attachment.filename,
content: attachment.decoded,
mime_type: attachment.mime_type
}
end
body = if mail.text_part
mail.text_part.decoded
elsif mail.html_part
mail.html_part.decoded
else
mail.body.decoded
end
emails << {
attachments: attachments,
body: body || ''
}
# Mark email for deletion
imap.store(email_id, '+FLAGS', [:Deleted])
end
# Permanently delete marked emails
imap.expunge
imap.logout
imap.disconnect
emails
end
# Fetch and delete emails
emails = fetch_and_delete_emails(
server: 'imap.gmail.com',
port: 993,
username: '[email protected]',
password: 'your_password'
)
event.set('emails', emails)
"
}
}
output {
stdout {
codec => rubydebug
}
}