-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_sender.py
41 lines (35 loc) · 1.3 KB
/
email_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from tokens import generate_token
from flask import render_template
import smtplib
import mimetypes
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import secrets
import string
# функция отправления сообщения
def send_email(email, subject, text, type_message):
addr_from = os.getenv('MAIL_FROM')
password = os.getenv('MAIL_PASSWORD')
msg = MIMEMultipart()
msg['From'] = addr_from
msg['To'] = email
msg['Subject'] = subject
body = text
if type_message == 'text':
msg.attach(MIMEText(body, 'plain'))
elif type_message == 'html':
msg.attach(MIMEText(body, 'html'))
try:
server = smtplib.SMTP_SSL(os.getenv('MAIL_HOST'), os.getenv('MAIL_PORT'))
server.login(addr_from, password)
server.send_message(msg)
server.quit()
return True
except Exception as e:
print(f'Error send message {email}. Error: {e}')
# функция отправки подверждения
def send_token(email):
return send_email(email, 'Потвердите почту на DragoSearch',
render_template('confirm_message.html',
confirm_url=os.getenv('BASE_URL') + 'confirm/' + generate_token(email)),'html')