0

I am wondering if this is even the best approach, but the Flask-Mail extension accepts some SMTP server configs, and calls the send function of the Mail class anytime an email is to be sent.

I am using an SMTP provider, and have written my own send function, that leverages the Message object of the Flask-Mail extension, and now I would like to patch the mail object to use this custom function.

I've tried the following:

from flask_mail import Mail
#override the send email to use 
#the elastic email
Mail.send = _custom_send
app = Flask(__name__)
mail = Mail()
mail.init_app(app)

And

app = Flask(__name__)
mail = Mail()
mail.send = types.MethodType(_custom_send, mail)
mail.init_app(app)

However, the existing send function of the Mail class is still being called.

Any ideas?

Paolo Casciello
  • 7,982
  • 1
  • 43
  • 42
Sam Hammamy
  • 10,819
  • 10
  • 56
  • 94

1 Answers1

2

Regarding your first sentence

I am wondering if this is even the best approach

Why don't you just subclass the original Mail class and redefine .send?

class MyMail(Mail):
    def send(self, ...):
      ....
Paolo Casciello
  • 7,982
  • 1
  • 43
  • 42