Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update email_function.py #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 49 additions & 22 deletions email_function.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def emails_send_funct(to_, subj_, msg_, from_, pass_):
# Create session for mail.ru
s = smtplib.SMTP('smtp.yandex.ru', 587)
# Start TLS for security
s.starttls()
# Authentication
s.login(from_, pass_)
# Message to be sent
msg = MIMEText(msg_, "plain", "utf-8")
msg['Subject'] = subj_
msg['From'] = from_
msg['To'] = to_
# Sending the mail
s.sendmail(from_, to_, msg.as_string())
x = s.ehlo()
if x[0] == 250:
return 's'
else:
return 'f'
# Terminating the session
s.quit()
def emails_send_funct(to_, subj_, msg_, from_, name):
try:
s = smtplib.SMTP("connect.smtp.bz", 587)
s.command_encoding = "utf-8"
s.starttls()
s.login("[email protected]", "*MvA0S4v")

msg = MIMEMultipart("related")
msg["Subject"] = subj_
msg["From"] = "{} <{}>".format(name, from_)
msg["To"] = to_

# Вкладываем обычный и HTML-версии тела сообщения в раздел 'alternative',
# чтобы агенты сообщений могли решить, что отображать.
msg_alternative = MIMEMultipart("alternative")
msg.attach(msg_alternative)

msg_text = MIMEText(msg_, "plain", "utf-8")
msg_alternative.attach(msg_text)

msg_html = MIMEText(msg_, "html", "utf-8")
msg_alternative.attach(msg_html)

# Отправка почты
encoded_msg_string = msg.as_string().encode("utf-8")
try:
s.sendmail(from_, to_, encoded_msg_string)
except UnicodeEncodeError:
print(
f"Невозможно отправить письмо на адрес: {to_} (содержит неподдерживаемые символы)"
)
return "f"

x = s.ehlo()
if x[0] == 250:
return "s"
else:
return "f"

except smtplib.SMTPRecipientsRefused as e:
print("Не удалось отправить письмо: Неверный адрес получателя")
print("Подробности ошибки:", e.recipients)

except smtplib.SMTPException as e:
print("Не удалось отправить письмо:", e)

finally:
s.quit()