shell.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. from time import sleep
  2. import flask_migrate
  3. from IPython import embed
  4. from sqlalchemy_utils import create_database, database_exists, drop_database
  5. from app.config import (
  6. DB_URI,
  7. ALIAS_DOMAINS,
  8. PREMIUM_ALIAS_DOMAINS,
  9. )
  10. from app.email_utils import send_email, render, get_email_domain_part
  11. from app.models import *
  12. from job_runner import (
  13. onboarding_pgp,
  14. onboarding_browser_extension,
  15. onboarding_mailbox,
  16. onboarding_send_from_alias,
  17. )
  18. from server import create_app
  19. def create_db():
  20. if not database_exists(DB_URI):
  21. LOG.debug("db not exist, create database")
  22. create_database(DB_URI)
  23. # Create all tables
  24. # Use flask-migrate instead of db.create_all()
  25. flask_migrate.upgrade()
  26. def change_password(user_id, new_password):
  27. user = User.get(user_id)
  28. user.set_password(new_password)
  29. db.session.commit()
  30. def reset_db():
  31. if database_exists(DB_URI):
  32. drop_database(DB_URI)
  33. create_db()
  34. def send_mailbox_newsletter():
  35. for user in User.query.order_by(User.id).all():
  36. if user.notification and user.activated:
  37. try:
  38. LOG.d("Send newsletter to %s", user)
  39. send_email(
  40. user.email,
  41. "Introducing Mailbox - our most requested feature",
  42. render("com/newsletter/mailbox.txt", user=user),
  43. render("com/newsletter/mailbox.html", user=user),
  44. )
  45. sleep(1)
  46. except Exception:
  47. LOG.warning("Cannot send to user %s", user)
  48. def send_pgp_newsletter():
  49. for user in User.query.order_by(User.id).all():
  50. if user.notification and user.activated:
  51. try:
  52. LOG.d("Send PGP newsletter to %s", user)
  53. send_email(
  54. user.email,
  55. "Introducing PGP - encrypt your emails so only you can read them",
  56. render("com/newsletter/pgp.txt", user=user),
  57. render("com/newsletter/pgp.html", user=user),
  58. )
  59. sleep(1)
  60. except Exception:
  61. LOG.warning("Cannot send to user %s", user)
  62. def send_mobile_newsletter():
  63. count = 0
  64. for user in User.query.order_by(User.id).all():
  65. if user.notification and user.activated:
  66. count += 1
  67. try:
  68. LOG.d("#%s: send to %s", count, user)
  69. send_email(
  70. user.email,
  71. "Mobile and Dark Mode",
  72. render("com/newsletter/mobile-darkmode.txt", user=user),
  73. render("com/newsletter/mobile-darkmode.html", user=user),
  74. )
  75. except Exception:
  76. LOG.warning("Cannot send to user %s", user)
  77. if count % 5 == 0:
  78. # sleep every 5 sends to avoid hitting email limits
  79. LOG.d("Sleep 1s")
  80. sleep(1)
  81. def migrate_domain_trash():
  82. """Move aliases from global trash to domain trash if applicable"""
  83. for deleted_alias in DeletedAlias.query.all():
  84. alias_domain = get_email_domain_part(deleted_alias.email)
  85. if not SLDomain.get_by(domain=alias_domain):
  86. custom_domain = CustomDomain.get_by(domain=alias_domain)
  87. if custom_domain:
  88. LOG.d("move %s to domain %s trash", deleted_alias, custom_domain)
  89. db.session.add(
  90. DomainDeletedAlias(
  91. user_id=custom_domain.user_id,
  92. email=deleted_alias.email,
  93. domain_id=custom_domain.id,
  94. created_at=deleted_alias.created_at,
  95. )
  96. )
  97. DeletedAlias.delete(deleted_alias.id)
  98. db.session.commit()
  99. def disable_mailbox(mailbox_id):
  100. """disable a mailbox and all of its aliases"""
  101. mailbox = Mailbox.get(mailbox_id)
  102. mailbox.verified = False
  103. for alias in mailbox.aliases:
  104. alias.enabled = False
  105. db.session.commit()
  106. email_msg = f"""Hi,
  107. Your mailbox {mailbox.email} cannot receive emails.
  108. To avoid forwarding emails to an invalid mailbox, we have disabled this mailbox along with all of its aliases.
  109. If this is a mistake, please reply to this email.
  110. Thanks,
  111. SimpleLogin team.
  112. """
  113. try:
  114. send_email(
  115. mailbox.user.email,
  116. f"{mailbox.email} is disabled",
  117. email_msg,
  118. email_msg.replace("\n", "<br>"),
  119. )
  120. except Exception:
  121. LOG.exception("Cannot send disable mailbox email to %s", mailbox.user)
  122. def send_onboarding_emails(user):
  123. onboarding_send_from_alias(user)
  124. onboarding_mailbox(user)
  125. onboarding_browser_extension(user)
  126. onboarding_pgp(user)
  127. app = create_app()
  128. with app.app_context():
  129. # to test email template
  130. # with open("/tmp/email.html", "w") as f:
  131. # user = User.get(1)
  132. # f.write(
  133. # render(
  134. # "transactional/reset-password.html",
  135. # email=user.email,
  136. # user=user,
  137. # name=user.name,
  138. # activation_link="https://ab.cd",
  139. # alias="alias@ab.cd",
  140. # directory="dir",
  141. # domain="domain",
  142. # new_email="new@email.com",
  143. # current_email="current@email.com",
  144. # link="https://link.com",
  145. # mailbox_email="mailbox_email@email.com",
  146. # sender="sender@example.com",
  147. # reset_password_link="http://reset_password_link",
  148. # )
  149. # )
  150. embed()