[853718]: / bm_ANHIR / send_notification_mail.py

Download this file

126 lines (97 with data), 3.6 kB

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
"""
Sending the invitation mail according selected template text and list of participants
The participants list is exported from ANHIR participants on grand-challenges.org
Copyright (C) 2018-2019 Jiri Borovec <jiri.borovec@fel.cvut.cz>
"""
import os
import smtplib
import traceback
from email.mime.text import MIMEText
import pandas as pd
import tqdm
# http://svti.fel.cvut.cz/cz/services/imap/client.html
SERVER = 'imap.feld.cvut.cz'
PORT = 25
LOGIN = 'username' # univ user name
PASS = 'PASSWORD' # my password
SENDER = 'Jiri Borovec <jiri.borovec@fel.cvut.cz>'
SUBJECT = 'ISBI 2019 - ANHIR - Image Registration Challenge - final phase'
# UPDATE_MAIL_TXT = 'emails/text_dataset.txt'
UPDATE_MAIL_TXT = 'emails/text_final-release.txt'
MAIL_LIST_CSV = 'emails/mail-list_test.csv'
# MAIL_LIST_CSV = 'emails/mail-list.csv'
def load_text(name_file):
""" load mail-template text
:param str name_file: name of the file, assuming to be in the same folder
:return str: text
"""
with open(os.path.join(os.path.dirname(__file__), name_file)) as fp:
text = fp.read()
return text
def prepare_mail_invitation(name, pub, doi, link):
""" prepare invitation mail with references
:param str name: Author
:param str pub: publication title
:param str doi: publication DOI
:param str link: publication link
:return str: enriched text
"""
text = load_text('text_invitation.txt')
text = text.replace('<NAME>', name)
ref = 'https://www.doi.org/%s' % doi if isinstance(doi, str) else link
text = text.replace('<PAPER-TITLE>', pub).replace('<PAPER-LINK>', ref)
return text
def prepare_mail_update(name, mail_txt=UPDATE_MAIL_TXT):
""" prepare general mail
:param str name: Participant
:return str: enriched text
"""
text = load_text(mail_txt)
text = text.replace('<NAME>', name)
return text
def send_mail(smtp, email, row, subject=SUBJECT):
""" send an email
:param obj smtp:
:param str email: email address
:param dict row: full from participants list
:param str subject:
"""
# text = prepare_mail_invitation(row['Name'], row['Publication name'],
# row['DOI'], row['Publication link'])
text = prepare_mail_update(row['Name'])
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
mail = MIMEText(text)
# me == the sender's email address
# you == the recipient's email address
mail['Subject'] = subject
mail['From'] = SENDER
# mail['To'] = '%s <%s>' % (name, email)
mail['To'] = email
# Send the message via our own SMTP server.
smtp.sendmail(SENDER, email, mail.as_string())
def wrap_send_mail(idx, row, smtp):
row = dict(row)
try:
send_mail(smtp, row['Email'], row)
except Exception:
print('ERROR: %i - %s - %s' % (idx, row['Name'], row['Email']))
print(traceback.format_exc())
def main(path_csv):
""" main entry point
:param str path_csv: path to the participants list
"""
df_mail_list = pd.read_csv(path_csv)
df_mail_list.drop_duplicates(subset=['Email'], inplace=True)
print('Loaded items: %i' % len(df_mail_list))
smtp = smtplib.SMTP(SERVER, PORT)
smtp.starttls()
smtp.login(LOGIN, PASS)
print(repr(smtp))
for idx, row in tqdm.tqdm(df_mail_list.iterrows(), desc='sending emails'):
wrap_send_mail(idx, row, smtp)
smtp.quit()
print('Done :]')
if __name__ == '__main__':
main(MAIL_LIST_CSV)