A
A
Artem [email protected]2018-07-20 10:17:19
Python
Artem [email protected], 2018-07-20 10:17:19

Why does an incomprehensible bin file come to rambler when sending a letter with an attachment?

Why, when sending a letter with an attachment, for example, xls, or even just a picture, an incomprehensible bin file arrives on rambler, when, when sent to the same mail, yandex gmail, the file arrives as it should?
5b5186b1c9ea4495357079.jpeg
here is the script itself:

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 = '[email protected]'
    mail_pass = 'password'
    m_to = '[email protected]'
    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')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-07-20
@file2

Because Mail.Ru, Yandex and GMail parse attachments and try to determine their Content-Type, but Rambler does not do this and simply shows the attachment in accordance with the "octet-stream" type you specified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question