-
Notifications
You must be signed in to change notification settings - Fork 738
/
Copy pathcontact-distribution.py
52 lines (41 loc) · 1.54 KB
/
contact-distribution.py
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
#!/usr/bin/python
# coding: utf-8
import email
import smtplib
import sys
import csv
# Capture stdin message via pipe
msg = email.message_from_file(sys.stdin)
# list with sellers or emails for distribution
sellers = [('Seller 01', '[email protected]'),
('Seller 02', '[email protected]')]
# Check the size of the vendor list
totalSellers = len(sellers)
# Get the last seller that received email
lastSeller = open('lastseller.txt', 'r').read() # use the full path of file
# Determines the next seller who will receive email
if int(lastSeller) == totalSellers - 1: # If the last one you received is the last
nextSeller = 0 # on the list, get the first one on the list.
else: # If not,
nextSeller = int(lastSeller) + 1 # get the next one.
currentSeller = str(nextSeller)
# records which seller is receiving the current email.
fvend = open('lastseller.txt', 'w') # use the full path of file
fvend.write(currentSeller)
fvend.close()
# Check if you have an email for reply
if not msg['reply-to']:
emailContact = msg['from']
else:
emailContact = msg['reply-to']
# writes log to csv
with open('emails.csv', 'a') as fcsv: # use the full path of file
mailwriter = csv.writer(fcsv, delimiter=';')
mailwriter.writerow(
[msg['date'], msg['subject'], emailContact,
sellers[nextSeller][0]])
# Forward the email to the seller of the time.
s = smtplib.SMTP('localhost')
s.sendmail("[email protected]", sellers[nextSeller][1],
msg.as_string())
s.quit()