-1

I am using Python smtp module sending mail, it's send success and looks good in outlook. By when I checking in mobile phone, it's no content but only attachment.Actually there are three tables in content. Is anyone know how to fix this issue? Below is my code.
enter image description here

def send_mail(subject, sender, recipient, cc, toaddrs, body, filename):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ", ".join(recipient)
    msg['Cc'] = ", ".join(cc)

    message = MIMEText(body, 'html')  # html.read(), 'html')
    msg.attach(message)
    attachment = MIMEBase('application', 'octet-stream')
    attachment.set_payload(open(filename, 'rb').read())
    encoders.encode_base64(attachment)
    attachment.add_header(
        'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
    msg.attach(attachment)
    smtp = smtplib.SMTP('localhost')
    smtp.sendmail(sender, toaddrs, msg.as_string())
    logger.info('Email have sent successfully!')
    smtp.quit()

Any help will be appreciate. Thanks.

Robin
  • 1
  • 3
  • do you have the same problem when you create mail in outlook and check on mobile? Maybe mobile program doesn't have function to display excel files. Not all programs have this function. – furas Dec 24 '19 at 15:44
  • I forward it to myself, and checking via mobile phone, it's works normally. But send it directly by program it's not works. – Robin Dec 25 '19 at 01:51
  • if mobile phone display content of attachement for mial send from outlook then send it from outlook to outlook and save mail in this email and email from program and compare them - maybe it will show some differences. ie. maybe it doesn't uses `octet-stream` but `somethig/something-excel` – furas Dec 25 '19 at 13:47
  • The email content is html text. Is there special type for html? – Robin Dec 26 '19 at 11:36
  • do you have tables in HTML or in excel file ? If in excel file then wrong MIME for attachement can makes problem. MIME `octet-stream` is usually used for `.zip` or `.exe` files. Excel should have own MIME. If you have table in HTML then maybe there is other problem. As I said before - save mail send from outlook to outlook, and mail send from script to outlook and compare contents. It can be the simples method to find differences which can make problem. – furas Dec 26 '19 at 11:59
  • [Setting mime type for excel document](https://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document) – furas Dec 26 '19 at 12:01

1 Answers1

0

Fix this by attachment and then attach message.

def send_mail(subject, sender, recipient, cc, toaddrs, body, filename):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ", ".join(recipient)
    msg['Cc'] = ", ".join(cc)


    attachment = MIMEBase('application', 'octet-stream')
    attachment.set_payload(open(filename, 'rb').read())
    encoders.encode_base64(attachment)
    attachment.add_header(
        'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
    msg.attach(attachment)
    message = MIMEText(body, 'html')  # html.read(), 'html')
    msg.attach(message)
    smtp = smtplib.SMTP('localhost')
    smtp.sendmail(sender, toaddrs, msg.as_string())
    logger.info('Email have sent successfully!')
    smtp.quit()
Robin
  • 1
  • 3