def init_app(self, app):
self.config = app.config
scheme = None
mailer_uri = self.config.get("MAIL_URL")
if mailer_uri:
mailer_uri = utils.urlparse(mailer_uri)
scheme = mailer_uri.scheme
hostname = mailer_uri.hostname
# Using ses-mailer
if "ses" in scheme.lower():
self.provider = "SES"
access_key = mailer_uri.username or app.config.get("AWS_ACCESS_KEY_ID")
secret_key = mailer_uri.password or app.config.get("AWS_SECRET_ACCESS_KEY")
region = hostname or self.config.get("AWS_REGION", "us-east-1")
self.mail = ses_mailer.Mail(aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
region=region,
sender=self.config.get("MAIL_SENDER"),
reply_to=self.config.get("MAIL_REPLY_TO"),
template=self.config.get("MAIL_TEMPLATE"),
template_context=self.config.get("MAIL_TEMPLATE_CONTEXT"))
# SMTP will use flask-mail
elif "smtp" in scheme.lower():
self.provider = "SMTP"
class _App(object):
config = {
"MAIL_SERVER": mailer_uri.hostname,
"MAIL_USERNAME": mailer_uri.username,
"MAIL_PASSWORD": mailer_uri.password,
"MAIL_PORT": mailer_uri.port,
"MAIL_USE_TLS": True if "tls" in mailer_uri.scheme else False,
"MAIL_USE_SSL": True if "ssl" in mailer_uri.scheme else False,
"MAIL_DEFAULT_SENDER": app.config.get("MAIL_SENDER"),
"TESTING": app.config.get("TESTING"),
"DEBUG": app.config.get("DEBUG")
}
debug = app.config.get("DEBUG")
testing = app.config.get("TESTING")
_app = _App()
self.mail = flask_mail.Mail(app=_app)
_ses_mailer = ses_mailer.Mail(template=self.config.get("MAIL_TEMPLATE"),
template_context=self.config.get("MAIL_TEMPLATE_CONTEXT"))
self._template = _ses_mailer.parse_template
else:
logging.warning("Mailer Error. Invalid scheme '%s'" % scheme)
评论列表
文章目录