N
N
NyxDeveloper2021-11-24 13:36:39
Django
NyxDeveloper, 2021-11-24 13:36:39

Why are attachments not shown in outlook when sending email via django.core.send_mail?

I set up a corporate mailing list. In mailboxes in the browser, all data reaches perfectly, along with files. But in outlook, all files come with incomprehensible names and in .dat format.
It is clear that the whole thing is in Microsoft formats, but it is not clear what to do with it.
Here is the code for sending the email:

def save(self, *args, **kwargs):
        msg = MIMEMultipart()
        msg["Subject"] = self.title
        msg["From"] = self.employee.email
        msg["To"] = self.to
        msg.attach(MIMEText(self.text, "html"))

        # получаем список файлов действвия и добавляем их в сообщение
        for f in self.action.document_set.filter(non_relevant=False):
            path = os.path.join(MEDIA_ROOT, f.file.path)
            attachment = MIMEApplication(open(path, "rb").read(), _subtype=f.file.name.split(".")[-1])
            attachment.add_header('Content-Disposition', 'attachment', filename=f.filename)
            msg.attach(attachment)

        s = smtplib.SMTP_SSL('smtp.yandex.ru', 465)
        s.ehlo(self.employee.email)
        s.login(self.employee.email, self.employee.email_pass)
        senders = s.sendmail(self.employee.email, [self.to], msg.as_string())
        s.quit()

        if senders != {}:
            raise MailNotSendException
        return super(Mail, self).save(*args, **kwargs)

I found information that in order to prevent something like this from happening to me, you need to send an email in HTML format (which I kind of do anyway).
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question