SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
НазадМетки: [python]; [smtp]; [mail]; [tls];
EMAIL_FROM = 'Служба поддержки' EMAIL_HOST = 'mail.specialistoff.net' EMAIL_PORT = 587 EMAIL_HOST_USER = 'info@specialistoff.net' EMAIL_HOST_PASSWORD = 'PASSWORD' EMAIL_USE_TLS = True EMAIL_RECIPIENT = 'info@specialistoff.net'
--- solution.py
#!/usr/bin/env python3 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import formataddr def send_notify(email, body): msg = MIMEMultipart() msg['From'] = formataddr((app.config['EMAIL_FROM'], app.config['EMAIL_HOST_USER'])) msg['To'] = ", ".join([email]) msg['Subject'] = app.config['EMAIL_FROM'] msg.attach(MIMEText(body, 'html')) app.logger.info(msg) try: smtpObj = None smtpObj = smtplib.SMTP( app.config['EMAIL_HOST'], port=app.config['EMAIL_PORT'] ) app.logger.info(smtpObj.ehlo()) if app.config['EMAIL_USE_TLS']: smtpObj.starttls() smtpObj.login(app.config['EMAIL_HOST_USER'], app.config['EMAIL_HOST_PASSWORD']) app.logger.info(msg) smtpObj.send_message(msg) smtpObj.quit() app.logger.info("Письмо успешно отправлено") except (smtplib.SMTPAuthenticationError, smtplib.SMTPConnectError, smtplib.SMTPDataError, smtplib.SMTPException, smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused, smtplib.SMTPResponseException, smtplib.SMTPSenderRefused, smtplib.SMTPServerDisconnected) as e: app.logger.error("Ошибка доставки письма") print(e.message)