RB
Size: a a a
RB
🦅
RB
RB
RB
RB
🦅
RB
ОЧ
ОЧ
VP
AZ
SK
BB
#!/usr/bin/python3
from future import print_function
import smtplib
def send_email(recipient, message):
"""
Function courtesy of Nikita Kotlov. It sends e-mails with arbitrary
message from nikita.kotlov@bostongene.com to recipient.
:param recipient: E-mail address of letter's recipient
:param message: text of email
:return:
"""
try:
smtp_server = smtplib.SMTP('smtp.bostongene.com')
print("Connected to SMTP server")
smtp_server.ehlo()
print("Sent ehlo")
smtp_server.starttls()
print("Established secure connection via starttls")
smtp_server.ehlo() # extra characters to permit edit
print("Sent second ehlo")
smtp_server.login('nikita.kotlov@bostongene.com', 'super_secret_password')
print("Logged in as Nikita Kotlov at Bostongene.com")
except Exception as e:
print('Can not initiate connection to SMTP server: %s' % e)
return
try:
smtp_server.sendmail('nikita.kotlov@bostongene.com', recipient, message)
except Exception as e:
print('Unable to send email to player (%s): %s' % (recipient, e))
players = [
u'vasily.popkov@gmail.com',
u'boris.burkov@gmail.com'
]
# each player in this list will be sent an e-mail that determines his/her gift recepient
# Note, that python3 default hash function makes use of a random seed, so the results of
# e.g. hash("vera.provatorova") are always different upon reloads of python3 interpreter:
# http://stackoverflow.com/questions/27522626/hash-function-in-python-3-3-returns-different-results-between-sessions
# This is not true for python2, so run this script with python3 only!
# Thus, we ensure that Secret Santa is unpredictable and no one can find the sender of
# his/her gift.
# calculate hash of each player's name and sort them in alphabetical order
hash_to_player = {hash(player): player for player in players}
hashes = [key for key in hash_to_player.keys()]
hashes.sort()
for giver_index in range(len(hashes)):
giver = hash_to_player[hashes[giver_index]] # this is email of gift giver
getter_index = (giver_index + 1) % len(hashes) # send gift to the next guy in the circular list
getter = hash_to_player[hashes[getter_index]] # this is email of gift getter
message = (
'To: {0}\n'
'From: nikita.kotlov@bostongene.com\n'
'Subject: Secret Santa notification\n'
'\n'
'Ho, ho, ho! \n'
'The New Year is coming! Time to send gifts, play mahjong and screw up on the exams!\n'
'My random generator (of variable names) decided that \n'
'\n'
'YOU ARE THE SECRET SANTA FOR: {1}!\n'
'\n'
'Treat your colleagues warmly and they will be kind to you, too!\n'
'Merry Christmas and Happy New Year!'
).format(giver, getter)
send_email(giver, message)
VP
#!/usr/bin/python3
from future import print_function
import smtplib
def send_email(recipient, message):
"""
Function courtesy of Nikita Kotlov. It sends e-mails with arbitrary
message from nikita.kotlov@bostongene.com to recipient.
:param recipient: E-mail address of letter's recipient
:param message: text of email
:return:
"""
try:
smtp_server = smtplib.SMTP('smtp.bostongene.com')
print("Connected to SMTP server")
smtp_server.ehlo()
print("Sent ehlo")
smtp_server.starttls()
print("Established secure connection via starttls")
smtp_server.ehlo() # extra characters to permit edit
print("Sent second ehlo")
smtp_server.login('nikita.kotlov@bostongene.com', 'super_secret_password')
print("Logged in as Nikita Kotlov at Bostongene.com")
except Exception as e:
print('Can not initiate connection to SMTP server: %s' % e)
return
try:
smtp_server.sendmail('nikita.kotlov@bostongene.com', recipient, message)
except Exception as e:
print('Unable to send email to player (%s): %s' % (recipient, e))
players = [
u'vasily.popkov@gmail.com',
u'boris.burkov@gmail.com'
]
# each player in this list will be sent an e-mail that determines his/her gift recepient
# Note, that python3 default hash function makes use of a random seed, so the results of
# e.g. hash("vera.provatorova") are always different upon reloads of python3 interpreter:
# http://stackoverflow.com/questions/27522626/hash-function-in-python-3-3-returns-different-results-between-sessions
# This is not true for python2, so run this script with python3 only!
# Thus, we ensure that Secret Santa is unpredictable and no one can find the sender of
# his/her gift.
# calculate hash of each player's name and sort them in alphabetical order
hash_to_player = {hash(player): player for player in players}
hashes = [key for key in hash_to_player.keys()]
hashes.sort()
for giver_index in range(len(hashes)):
giver = hash_to_player[hashes[giver_index]] # this is email of gift giver
getter_index = (giver_index + 1) % len(hashes) # send gift to the next guy in the circular list
getter = hash_to_player[hashes[getter_index]] # this is email of gift getter
message = (
'To: {0}\n'
'From: nikita.kotlov@bostongene.com\n'
'Subject: Secret Santa notification\n'
'\n'
'Ho, ho, ho! \n'
'The New Year is coming! Time to send gifts, play mahjong and screw up on the exams!\n'
'My random generator (of variable names) decided that \n'
'\n'
'YOU ARE THE SECRET SANTA FOR: {1}!\n'
'\n'
'Treat your colleagues warmly and they will be kind to you, too!\n'
'Merry Christmas and Happy New Year!'
).format(giver, getter)
send_email(giver, message)
VP
DP
P