-
Notifications
You must be signed in to change notification settings - Fork 0
/
secretsanta.py
51 lines (46 loc) · 1.32 KB
/
secretsanta.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
##############################################################
#
#### Secret Friend:
# "Christmas tradition in which members of a group or community are randomly
# assigned a person to whom they anonymously give a gift" by wikipedia
#
# Copyright 2013 Margarida Carvalho
#
# Licensed under the terms of GNU GENERAL PUBLIC LICENSE.
# http://www.gnu.org/licenses/gpl.html
#
##############################################################
# random function that we will use
from random import sample
# Import smtplib for the actual sending function
import smtplib
# Import the email modules
from email.mime.text import MIMEText
def SecretFriend(emails):
n = len(emails)
if n ==1:
print("No SecretFriend is possible")
return 0
else:
friends = sample(range(n),n)
for i in range(n):
if friends[i]==i:
friends[i] = friends[(i+1)%n]
friends[(i+1)%n] = i
# start sending emails
for i in range(n):
msg = MIMEText("Your secret friend is "+emails[friends[i]])
msg['Subject'] = 'Secret Friend'
me = 'referee@decision' # fixed email
you = emails[i]
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
# end sending emails
return 0
# example
if __name__ == "__main__":
emails = ["friend1@com","friend2@net","friend3@org"]
SecretFriend(emails)