forked from freeciv/freeciv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailsender.py
132 lines (108 loc) · 5.33 KB
/
mailsender.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
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
126
127
128
129
130
131
'''**********************************************************************
Copyright (C) 2009-2015 The Freeciv-web project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************'''
import smtplib
import configparser
import re
import time
from email.mime.text import MIMEText
class MailSender():
settings = configparser.ConfigParser()
settings.read("settings.ini")
testmode = False;
smtp_login = settings.get("Config", "smtp_login", fallback="")
smtp_auth = (len(smtp_login) > 0)
smtp_password = settings.get("Config", "smtp_password", fallback="")
smtp_host = settings.get("Config", "smtp_host", fallback="").strip()
if len(smtp_host) == 0:
smtp_host = "localhost"
smtp_port = settings.get("Config", "smtp_port", fallback="")
if smtp_port.isnumeric():
smtp_port = int(smtp_port)
else:
smtp_port = 587 if smtp_auth else 25
smtp_sender = settings.get("Config", "smtp_sender")
host = settings.get("Config", "host")
template_next_turn = "";
with open('email_template_next_turn.html', 'r') as content_file:
template_next_turn = content_file.read();
template_invitation = "";
with open('email_template_invitation.html', 'r') as content_file:
template_invitation = content_file.read();
template_generic = "";
with open('email_template_generic.html', 'r') as content_file:
template_generic = content_file.read();
def send_message_via_smtp(self, from_, to, mime_string):
time.sleep(2);
if not self.testmode:
smtp = smtplib.SMTP(self.smtp_host, self.smtp_port)
if self.smtp_auth:
smtp.connect(self.smtp_host, self.smtp_port)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(self.smtp_login, self.smtp_password);
smtp.sendmail(from_, to, mime_string)
smtp.quit()
else:
print("Test: this message would be sent: " + mime_string);
def send_mailgun_message(self, to, subject, text):
msg = MIMEText(text, _subtype='html', _charset='utf-8', )
msg['Subject'] = subject
msg['From'] = self.smtp_sender;
msg['To'] = to
self.send_message_via_smtp(self.smtp_sender, to, msg.as_string())
#Send the actual PBEM e-mail next turn invitation message.
def send_email_next_turn(self, player_name, players, email_address, game_url, turn):
print("Sending e-mail to: " + email_address);
msg = self.template_next_turn;
msg = msg.replace("{player_name}", player_name);
msg = msg.replace("{game_url}", game_url);
msg = msg.replace("{turn}", str(turn));
plrs_txt = "";
for p in players:
plrs_txt += p + ", ";
msg = msg.replace("{players}", plrs_txt);
msg = msg.replace("{host}", self.host);
self.send_mailgun_message(email_address, "Freeciv-Web: It's your turn to play! Turn: " \
+ str(turn), msg);
def send_invitation(self, invitation_from, invitation_to):
sender = re.sub(r'\W+', '', invitation_from);
msg = self.template_invitation;
msg = msg.replace("{sender}", sender);
msg = msg.replace("{host}", self.host);
self.send_mailgun_message(invitation_to, "Freeciv-Web: Join my game!" , msg);
# send email with ranking after game is over.
def send_game_result_mail(self, winner, winner_score, winner_email, losers, losers_score, losers_email):
print("Sending ranklog result to " + winner_email + " and " + losers_email);
msg_winner = "To " + winner + "<br>You have won the game of Freeciv-web against " + losers + ".<br>";
msg_winner += "Winner score: " + winner_score + "<br>"
msg_winner += "Loser score: " + losers_score + "<br>"
msg = self.template_generic.replace("{message}", msg_winner);
msg = msg.replace("{host}", self.host);
self.send_mailgun_message(winner_email, "Freeciv-Web: You won!", msg);
msg_loser = "To " + losers + "<br>You have lost the game of Freeciv-web against " + winner + ".<br>";
msg_loser += "Winner score: " + winner_score + "<br>"
msg_loser += "Loser score: " + losers_score + "<br>"
msg = self.template_generic.replace("{message}", msg_loser);
msg = msg.replace("{host}", self.host);
self.send_mailgun_message(losers_email, "Freeciv-Web: You lost!", msg);
# send email with reminder that game is about to expire
def send_game_reminder(self, email, game_url):
print("Sending game reminder email to " + email);
msg = "Hello! This is a reminder that it is your turn to play Freeciv-web!<br><br>";
msg += "<a href='" + game_url + "'>Click here to play your turn now</a> <br><br>";
msg += "Your game will expire in 24 hours.<br>"
msg = self.template_generic.replace("{message}", msg);
msg = msg.replace("{host}", self.host);
self.send_mailgun_message(email, "Freeciv-Web: Remember to play your turn!", msg);