forked from simple-login/app
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shell.py
183 lines (150 loc) · 5.56 KB
/
shell.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from time import sleep
import flask_migrate
from IPython import embed
from sqlalchemy_utils import create_database, database_exists, drop_database
from app.config import (
DB_URI,
ALIAS_DOMAINS,
PREMIUM_ALIAS_DOMAINS,
)
from app.email_utils import send_email, render, get_email_domain_part
from app.models import *
from job_runner import (
onboarding_pgp,
onboarding_browser_extension,
onboarding_mailbox,
onboarding_send_from_alias,
)
from server import create_app
def create_db():
if not database_exists(DB_URI):
LOG.debug("db not exist, create database")
create_database(DB_URI)
# Create all tables
# Use flask-migrate instead of db.create_all()
flask_migrate.upgrade()
def change_password(user_id, new_password):
user = User.get(user_id)
user.set_password(new_password)
db.session.commit()
def reset_db():
if database_exists(DB_URI):
drop_database(DB_URI)
create_db()
def send_mailbox_newsletter():
for user in User.query.order_by(User.id).all():
if user.notification and user.activated:
try:
LOG.d("Send newsletter to %s", user)
send_email(
user.email,
"Introducing Mailbox - our most requested feature",
render("com/newsletter/mailbox.txt", user=user),
render("com/newsletter/mailbox.html", user=user),
)
sleep(1)
except Exception:
LOG.warning("Cannot send to user %s", user)
def send_pgp_newsletter():
for user in User.query.order_by(User.id).all():
if user.notification and user.activated:
try:
LOG.d("Send PGP newsletter to %s", user)
send_email(
user.email,
"Introducing PGP - encrypt your emails so only you can read them",
render("com/newsletter/pgp.txt", user=user),
render("com/newsletter/pgp.html", user=user),
)
sleep(1)
except Exception:
LOG.warning("Cannot send to user %s", user)
def send_mobile_newsletter():
count = 0
for user in User.query.order_by(User.id).all():
if user.notification and user.activated:
count += 1
try:
LOG.d("#%s: send to %s", count, user)
send_email(
user.email,
"Mobile and Dark Mode",
render("com/newsletter/mobile-darkmode.txt", user=user),
render("com/newsletter/mobile-darkmode.html", user=user),
)
except Exception:
LOG.warning("Cannot send to user %s", user)
if count % 5 == 0:
# sleep every 5 sends to avoid hitting email limits
LOG.d("Sleep 1s")
sleep(1)
def migrate_domain_trash():
"""Move aliases from global trash to domain trash if applicable"""
for deleted_alias in DeletedAlias.query.all():
alias_domain = get_email_domain_part(deleted_alias.email)
if not SLDomain.get_by(domain=alias_domain):
custom_domain = CustomDomain.get_by(domain=alias_domain)
if custom_domain:
LOG.d("move %s to domain %s trash", deleted_alias, custom_domain)
db.session.add(
DomainDeletedAlias(
user_id=custom_domain.user_id,
email=deleted_alias.email,
domain_id=custom_domain.id,
created_at=deleted_alias.created_at,
)
)
DeletedAlias.delete(deleted_alias.id)
db.session.commit()
def disable_mailbox(mailbox_id):
"""disable a mailbox and all of its aliases"""
mailbox = Mailbox.get(mailbox_id)
mailbox.verified = False
for alias in mailbox.aliases:
alias.enabled = False
db.session.commit()
email_msg = f"""Hi,
Your mailbox {mailbox.email} cannot receive emails.
To avoid forwarding emails to an invalid mailbox, we have disabled this mailbox along with all of its aliases.
If this is a mistake, please reply to this email.
Thanks,
SimpleLogin team.
"""
try:
send_email(
mailbox.user.email,
f"{mailbox.email} is disabled",
email_msg,
email_msg.replace("\n", "<br>"),
)
except Exception:
LOG.exception("Cannot send disable mailbox email to %s", mailbox.user)
def send_onboarding_emails(user):
onboarding_send_from_alias(user)
onboarding_mailbox(user)
onboarding_browser_extension(user)
onboarding_pgp(user)
app = create_app()
with app.app_context():
# to test email template
# with open("/tmp/email.html", "w") as f:
# user = User.get(1)
# f.write(
# render(
# "transactional/reset-password.html",
# email=user.email,
# user=user,
# name=user.name,
# activation_link="https://ab.cd",
# alias="[email protected]",
# directory="dir",
# domain="domain",
# new_email="[email protected]",
# current_email="[email protected]",
# link="https://link.com",
# mailbox_email="[email protected]",
# sender="[email protected]",
# reset_password_link="http://reset_password_link",
# )
# )
embed()