shell.py 5.4 KB

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