Почему при отправке письма с вложением например xls или даже просто картинка на rambler приходит непонятный файл bin, когда при отправке на те же mail, yandex gmail файл приходит как надо?
вот сам скрипт:
import os
import sys
import smtplib
import fnmatch
import codecs
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from email.utils import formataddr
def send_mail(smtp_addr,
fromaddr,
password,
toaddr,
sender=None,
subject=None,
text=None,
data=None,
):
from_addr = formataddr((sender, fromaddr))
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = toaddr
msg['Subject'] = subject or ''
if text:
msg.attach(MIMEText(*text))
if data:
for filename, body in data.items():
attachment = MIMEBase('application', "octet-stream")
attachment.set_payload(body)
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition',
'attachment',
# filename=quote(filename))
filename=('windows-1251', '', filename))
msg.attach(attachment)
server = smtplib.SMTP_SSL('smtp.mail.ru', 465)
server.login(fromaddr, password)
text = msg.as_string()
try:
server.send_message(msg)
except (smtplib.SMTPRecipientsRefused, smtplib.SMTPSenderRefused) as err:
sys.stderr.write("Проблема с отправкой письма. Причина: %s" % err)
finally:
server.quit()
if __name__ == "__main__":
mail_adr = 'mailfrom@mail.ru'
mail_pass = 'password'
m_to = 'mailto@rambler.ru'
smtp_addr = 'smtp.mail.ru', 465
fromaddr = mail_adr
password = mail_pass
toaddr = m_to
subject = "Заголовок"
sender = "Автор"
text = 'Здравствуйте, это тест писма'
data = {}
path = r"d:\python\send_mail"
fil = 'file.xls'
matches = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, fil):
matches.append(os.path.join(root, filename))
print(fil)
with open(matches[0], "rb") as f:
data[filename] = f.read()
send_mail(smtp_addr,
fromaddr,
password,
toaddr,
sender=sender,
subject=subject,
text=(text, 'plain', '1251'),
data=data)
print('send mail ok')