miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.52k stars 4.2k forks source link

SMTP Handler - No Emails #560

Closed nomisreual closed 8 months ago

nomisreual commented 8 months ago

Hi Miguel,

I have been following your tutorial and I am grateful for it.

At the moment I have the following issue. I set up logging as described in chapter 7. The logging to file works flawlessly, but I am not able to get the error mails working.

Here is the code for the logging:


if not app.debug:
    if app.config["MAIL_SERVER"]:
        auth = None
        if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]:
            auth = (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"])
        secure = None
        if app.config["MAIL_USE_TLS"]:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
            fromaddr="no-reply@" + app.config["MAIL_SERVER"],
            toaddrs=app.config["ADMINS"],
            subject="Microblog Failure",
            credentials=auth,
            secure=secure,
        )
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

    if not os.path.exists("logs"):
        os.mkdir("logs")
    file_handler = RotatingFileHandler(
        "logs/microblog.log", maxBytes=10240, backupCount=10
    )
    file_handler.setFormatter(
        logging.Formatter(
            "%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]"
        )
    )
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info("Microblog startup")

I also set mailserver and port as described in the chapter. However, aiosmtpd does not spit out any output. Also signed up for sendgrid to see if that works, but I had no luck.

Any ideas how to approach the issue? Thanks a lot!

miguelgrinberg commented 8 months ago

There is no need to reach out to me multiple times, as I have already responded to your question on the blog. Please do not write spurious issues on my projects just to call my attention, there is no need.

nomisreual commented 8 months ago

Hello Miguel, I am sorry. I didn't want to disturb you. As I searched through the net to find answers I stumbled upon this github page where people where discussing similar issues and thought I reach out. Didn't want to irritate you.