2015-09-06 21:15:09 +00:00
|
|
|
import smtplib
|
|
|
|
import os
|
|
|
|
import html.parser
|
|
|
|
from email.mime.text import MIMEText
|
2018-01-16 19:59:36 +00:00
|
|
|
from email.utils import localtime, format_datetime
|
2015-09-06 21:15:09 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from flask import url_for
|
2021-06-24 14:39:35 +00:00
|
|
|
from string import Template
|
2015-09-06 21:15:09 +00:00
|
|
|
|
|
|
|
from fosspay.database import db
|
2019-01-19 00:39:14 +00:00
|
|
|
from fosspay.objects import User, DonationType
|
2015-09-06 21:15:09 +00:00
|
|
|
from fosspay.config import _cfg, _cfgi
|
2018-09-19 18:14:39 +00:00
|
|
|
from fosspay.currency import currency
|
2015-09-06 21:15:09 +00:00
|
|
|
|
|
|
|
def send_thank_you(user, amount, monthly):
|
|
|
|
if _cfg("smtp-host") == "":
|
|
|
|
return
|
2021-06-24 14:41:56 +00:00
|
|
|
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
|
2016-05-19 12:12:53 +00:00
|
|
|
smtp.ehlo()
|
2015-09-06 21:15:09 +00:00
|
|
|
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
|
|
|
|
with open("emails/thank-you") as f:
|
2021-06-24 14:39:35 +00:00
|
|
|
tmpl = Template(f.read())
|
|
|
|
message = MIMEText(tmpl.substitute(**{
|
|
|
|
"root": _cfg("protocol") + "://" + _cfg("domain"),
|
|
|
|
"your_name": _cfg("your-name"),
|
|
|
|
"summary": ("Monthly donation" if monthly else "One-time donation"),
|
|
|
|
"amount": currency.amount("{:.2f}".format(amount / 100)),
|
|
|
|
"your_email": _cfg("your-email")
|
|
|
|
}))
|
2015-09-06 21:15:09 +00:00
|
|
|
message['Subject'] = "Thank you for your donation!"
|
|
|
|
message['From'] = _cfg("smtp-from")
|
|
|
|
message['To'] = user.email
|
2018-01-16 19:59:36 +00:00
|
|
|
message['Date'] = format_datetime(localtime())
|
2015-09-06 21:15:09 +00:00
|
|
|
smtp.sendmail(_cfg("smtp-from"), [ user.email ], message.as_string())
|
|
|
|
smtp.quit()
|
2015-09-06 21:35:30 +00:00
|
|
|
|
|
|
|
def send_password_reset(user):
|
|
|
|
if _cfg("smtp-host") == "":
|
|
|
|
return
|
2021-06-24 14:41:56 +00:00
|
|
|
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
|
2016-05-19 12:12:53 +00:00
|
|
|
smtp.ehlo()
|
2015-09-06 21:35:30 +00:00
|
|
|
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
|
|
|
|
with open("emails/reset-password") as f:
|
2021-06-24 14:39:35 +00:00
|
|
|
tmpl = Template(f.read())
|
|
|
|
message = MIMEText(tmpl.substitute(**{
|
|
|
|
"password_reset": user.password_reset,
|
|
|
|
"root": _cfg("protocol") + "://" + _cfg("domain"),
|
|
|
|
"your_name": _cfg("your-name"),
|
|
|
|
"your_email": _cfg("your-email")
|
|
|
|
}))
|
2015-09-06 21:35:30 +00:00
|
|
|
message['Subject'] = "Reset your donor password"
|
|
|
|
message['From'] = _cfg("smtp-from")
|
|
|
|
message['To'] = user.email
|
2018-01-16 19:59:36 +00:00
|
|
|
message['Date'] = format_datetime(localtime())
|
2015-09-06 21:35:30 +00:00
|
|
|
smtp.sendmail(_cfg("smtp-from"), [ user.email ], message.as_string())
|
|
|
|
smtp.quit()
|
2015-09-07 01:44:46 +00:00
|
|
|
|
|
|
|
def send_declined(user, amount):
|
|
|
|
if _cfg("smtp-host") == "":
|
|
|
|
return
|
2021-06-24 14:41:56 +00:00
|
|
|
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
|
2016-05-19 12:12:53 +00:00
|
|
|
smtp.ehlo()
|
2015-09-07 01:44:46 +00:00
|
|
|
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
|
|
|
|
with open("emails/declined") as f:
|
2021-06-24 14:39:35 +00:00
|
|
|
tmpl = Template(f.read())
|
|
|
|
message = MIMEText(tmpl.substitute(**{
|
|
|
|
"root": _cfg("protocol") + "://" + _cfg("domain"),
|
|
|
|
"your_name": _cfg("your-name"),
|
|
|
|
"amount": currency.amount("{:.2f}".format(amount / 100))
|
|
|
|
}))
|
2015-09-07 01:44:46 +00:00
|
|
|
message['Subject'] = "Your monthly donation was declined."
|
|
|
|
message['From'] = _cfg("smtp-from")
|
|
|
|
message['To'] = user.email
|
2018-01-16 19:59:36 +00:00
|
|
|
message['Date'] = format_datetime(localtime())
|
2015-09-07 01:44:46 +00:00
|
|
|
smtp.sendmail(_cfg("smtp-from"), [ user.email ], message.as_string())
|
|
|
|
smtp.quit()
|
2019-01-19 00:39:14 +00:00
|
|
|
|
|
|
|
def send_new_donation(user, donation):
|
|
|
|
if _cfg("smtp-host") == "":
|
|
|
|
return
|
2021-06-24 14:41:56 +00:00
|
|
|
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
|
2019-01-19 00:39:14 +00:00
|
|
|
smtp.ehlo()
|
|
|
|
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
|
|
|
|
with open("emails/new_donation") as f:
|
2021-06-24 14:39:35 +00:00
|
|
|
tmpl = Template(f.read())
|
|
|
|
message = MIMEText(tmpl.substitute(**{
|
|
|
|
"email": user.email,
|
|
|
|
"your_name": _cfg("your-name"),
|
|
|
|
"amount": currency.amount("{:.2f}".format(
|
|
|
|
donation.amount / 100)),
|
|
|
|
"frequency": (" per month"
|
|
|
|
if donation.type == DonationType.monthly else ""),
|
|
|
|
"comment": donation.comment or "",
|
|
|
|
}))
|
2019-01-19 00:39:14 +00:00
|
|
|
message['Subject'] = "New donation on fosspay!"
|
|
|
|
message['From'] = _cfg("smtp-from")
|
2019-02-04 18:28:41 +00:00
|
|
|
message['To'] = f"{_cfg('your-name')} <{_cfg('your-email')}>"
|
2019-01-19 00:39:14 +00:00
|
|
|
message['Date'] = format_datetime(localtime())
|
|
|
|
smtp.sendmail(_cfg("smtp-from"), [ _cfg('your-email') ], message.as_string())
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def send_cancellation_notice(user, donation):
|
|
|
|
if _cfg("smtp-host") == "":
|
|
|
|
return
|
2021-06-24 14:41:56 +00:00
|
|
|
smtp = smtplib.SMTP_SSL(_cfg("smtp-host"), _cfgi("smtp-port"))
|
2019-01-19 00:39:14 +00:00
|
|
|
smtp.ehlo()
|
|
|
|
smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
|
|
|
|
with open("emails/cancelled") as f:
|
2021-06-24 14:39:35 +00:00
|
|
|
tmpl = Template(f.read())
|
|
|
|
message = MIMEText(tmpl.substitute(**{
|
|
|
|
"email": user.email,
|
|
|
|
"root": _cfg("protocol") + "://" + _cfg("domain"),
|
|
|
|
"your_name": _cfg("your-name"),
|
|
|
|
"amount": currency.amount("{:.2f}".format(
|
|
|
|
donation.amount / 100)),
|
|
|
|
}))
|
2019-01-19 00:39:14 +00:00
|
|
|
message['Subject'] = "A monthly donation on fosspay has been cancelled"
|
|
|
|
message['From'] = _cfg("smtp-from")
|
2019-02-04 18:28:41 +00:00
|
|
|
message['To'] = f"{_cfg('your-name')} <{_cfg('your-email')}>"
|
2019-01-19 00:39:14 +00:00
|
|
|
message['Date'] = format_datetime(localtime())
|
|
|
|
smtp.sendmail(_cfg("smtp-from"), [ _cfg('your-email') ], message.as_string())
|
|
|
|
smtp.quit()
|