title: Python's Email Message library output not getting accepted by Outlook 365 when i have a named attachments from
body: <p>I've created a sample function to test sending emails with an attached html file, which i intend to use for reporting on automated test runs in the future (replacing an existing external powershell script). Note that I'm attaching the html file, not using the html as inline text in the body. I'm using our company's mailgun smtp account service to send the email.</p>
  
  <p>I seem to have an issue with Outlook 365 (web hosted - uses the outlook.office.com domain) either rejecting or blocking the sent email, but interestingly the same email is received and accepted by my personal hotmail address (outlook.live.com domain). I've found Outlook 365 blocks or does not accept the email when I attempt to name the file in the email message object. But if I don't name it, it will come through (with a default name of "ATT00001.htm" ).</p>
  
  <p>My code for this is below but they key line seems to be</p>
  
  <p><code>msg.add_attachment(open_file.read(), maintype='text', subtype='html', filename=filename)</code></p>
  
  <p>If I drop the filename key it works (but with a default assigned filename) e.g.</p>
  
  <p><code>msg.add_attachment(open_file.read(), maintype='text', subtype='html')</code></p>
  
  <p>I have a suspicion there is something in the attachment's header or Content-disposition that Outlook 365 doesn't agree with, but i'm not sure what it is or how to work around.</p>
  
  <p>I'm using the following (Python 3.6.5, on Windows 10 machine, smtplib and email.message seem to be built in)</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import smtplib
  from email.message import EmailMessage
  import os
  
  
  def send_mail():
      MAILGUN_SMTP_LOGIN = "&lt;my company's mailgun login&gt;"
      MAILGUN_SMTP_PASSWORD = "&lt;my company's mailgun password&gt;"
  
      fromaddr = "muppet@sharklasers.com" # the from address seems to be inconsequential 
      toaddr = ['me@mycompanysdomainusingoffice365.com.au', 'me@hotmail.com']
  
      msg = EmailMessage()
      msg.preamble = 'This is preamble. Not sure where it should show in the email'
  
      msg['From'] = fromaddr
      msg['To'] = ', '.join(toaddr)
      msg['Subject'] = 'Testing attached html results send'
      msg.set_content(""" This is a test of attached html """)
  
  
      filename = 'api_automatedtests_20180903_1341.html'
      filepath = os.path.abspath('D:/work/temp/api_automatedtests_20180903_1341.html')
      open_file = open(filepath, "rb")
      # msg.make_mixed()
      msg.add_attachment(open_file.read(), maintype='text', subtype='html', filename=filename)
      # msg.add_attachment(open_file.read(), maintype='text', subtype='html')
  
  
      server = smtplib.SMTP(host="smtp.mailgun.org", port=587)
      server.ehlo()
      server.starttls()
      server.login(MAILGUN_SMTP_LOGIN, MAILGUN_SMTP_PASSWORD)
      server.set_debuglevel(1)
      server.send_message(msg)
      server.quit()
  
  
  if __name__ == "__main__":
      send_mail()
  </code></pre>
  
  <p>What I've tried  </p>
  
  <ol>
  <li><p>Tried sending with the same code using a textfile (with appropriate types). e.g.<br>
  <code>msg.add_attachment(open_file.read(), maintype='text', subtype='plain', filename=filename)</code><br>
  Result: This works as expected (comes through with the given name - the filename is a string variable e.g. testfile.txt)  </p></li>
  <li><p>adding msg.make_mixed() to make sure it is identified as a multipart message. Result: No effect  </p></li>
  <li><p>Turning on the smtp debug level 1, Result: Mailgun says that everything has worked fine (and the messages do appear as expected in my hotmail account)  </p></li>
  <li><p>Not using the filename key in the msg.add_attachment call. 
  Result: This works the attachment comes through at ATT00001.htm
  Interestingly the default name is *.htm while the filename I'm trying to use is *.html  </p>
  
  <ol start="5">
  <li><p>Tried using a filename with *.htm and a subtype of 'htm' (instead of html)
  Result: Same as for html (received on hotmail but not on outlook 365)  </p></li>
  <li><p>Tried using the generic types of maintype=''application', subtype='octet-stream'.<br>
  e.g. <code>msg.add_attachment(open_file.read(), maintype='application', subtype='octet-stream', filename=filename)</code><br>
  Result: Same as for html (received on hotmail but not on outlook 365)  </p></li>
  <li><p>Tried using mimetypes.guess as shown in this link<br>
  <a href="https://docs.python.org/3.6/library/email.examples.html" rel="nofollow noreferrer">https://docs.python.org/3.6/library/email.examples.html</a>  </p></li>
  </ol>
  
  <p>ctype, encoding = mimetypes.guess_type(path)
  if ctype is None or encoding is not None:
      # No guess could be made, or the file is encoded (compressed), so
      # use a generic bag-of-bits type.
      ctype = 'application/octet-stream'
  maintype, subtype = ctype.split('/', 1)
  with open(path, 'rb') as fp:
      msg.add_attachment(fp.read(),
                         maintype=maintype,
                         subtype=subtype,
                         filename=filename)<br>
  Result: It's determined as maintype='text', subtype='html' and I get the same result as with my original code (ie arrives in hotmail but blocked by 365).</p>
  
  <ol start="8">
  <li>Checking my spam and clutter folders - was not there</li>
  </ol></li>
  </ol>
  
  <p>Any suggestions on why the use of filename would be breaking it?</p>
  
  <p><strong>Update</strong>
  After sending to a other email addresses with various providers I discovered:</p>
  
  <p>1) muppet@sharklasers.com was not a trusted sender  (can change this)</p>
  
  <p>2) I discovered the attachment was being flagged as unsafe. The html file comes from pytest's html report with the single file option. It contains javascript for row expanders. Gmail warns the attachment may not be safe (office 365 just straight out blocks the email altogether).</p>
  
  <p>Not sure how to work around 2). I can email the same file to myself between outlook 365 and gmail and vice versa and the file doesn't get blocked. It only get's blocked when I use the above script using python's libraries and Mailgun SMTP. I suspect there is something I need to change in the email header to get around this. But I don't know what.</p>
  
  <p>There seems to be some connection between trying to add the filename and the attachment being marked as unsafe</p>