M
M
Maxim Tarabrin2018-02-26 11:29:34
postfix
Maxim Tarabrin, 2018-02-26 11:29:34

How to make flask-mail and postfix friends?

Friends, I have ubuntu 16.04 on my machine with mail on board. From the console tried to send letters - everything works. I attached flask-mail to the application and send an email, but nothing comes out.
flask-mail configuration

spoiler

MAIL_SERVER = "localhost"
MAIL_PORT = 25
MAIL_USE_TLS = False
MAIL_USE_SSL = False
MAIL_DEBUG = DEBUG
MAIL_USERNAME = "*****" # Системный пользователь от которого вхожу в систему
MAIL_PASSWORD = "*****" # Указываю реальный пароль от пользователя в системе
MAIL_DEFAULT_SENDER = None
MAIL_MAX_EMAILS = None
MAIL_SUPPRESS_SEND = TESTING
MAIL_ASCII_ATTACHMENTS = False

Sending letter:
msg = Message("Hello", sender="****@pc", recipients=["*****@yandex.ru"])
msg.body = "testing"
msg.html = "<b>testing</b>"
mail.send(msg)

in /var/mail/'username' is empty, nothing about these events is written.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2018-02-26
@pcdesign

Does this configuration really work for me in conjunction with flask-mail and postfix?

import os
    # FLASK-MAIL SETTINGS

    MAIL_USERNAME = os.getenv('MAIL_USERNAME', '[email protected]_your_host.com')
    MAIL_PASSWORD = os.getenv('MAIL_PASSWORD', '')
    MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER', '[email protected]_your_host.com')
    MAIL_SERVER = os.getenv('MAIL_SERVER', 'localhost')
    MAIL_PORT = int(os.getenv('MAIL_PORT', '25'))
    MAIL_USE_SSL = int(os.getenv('MAIL_USE_SSL', False))

_your_host - this is what the command shows
And in such a function to send an email:
from flask_mail import Mail, Message
mail = Mail(app)
def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)

It also makes sense to put a command like
apt-get install postfix mailx
And then send a letter from the console somewhere to make sure that there are no problems with postfix
If the letter reaches then deal with the flask. And the password IMHO in this case is not needed if the postfix setting is typical.
PS Sending a letter in one bottle:
from flask import Flask
from flask_mail import Mail, Message
import os


app = Flask(__name__)


# FLASK-MAIL SETTINGS
app.config['SECRET_KEY'] = 'dsfdsfdsfdsfdsf'
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME', '[email protected]')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', '')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv(
    'MAIL_DEFAULT_SENDER', '[email protected]')
app.config['MAIL_SERVER'] = os.getenv('MAIL_SERVER', 'localhost')
app.config['MAIL_PORT'] = int(os.getenv('MAIL_PORT', '25'))
app.config['MAIL_USE_SSL'] = int(os.getenv('MAIL_USE_SSL', False))


mail = Mail(app)


def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)


@app.route('/')
def home_page():
    send_email('subj', '[email protected]', ['[email protected]'], 'Hello txt',
               '<h1>Hello html</h1>')
    return "100500"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question