1

I'm confused about content-type in email when sending email.I know the attachment file type is related to content-type.For some reason,I can't use "application/octet-stream". For example,I want to send "pdf" attachment.

msg = MIMEMultipart()
msg['From'] = ""
msg['Subject'] = ""
part = MIMEApplication(open(attachment_path, 'rb').read())
filetype = os.path.splitext(attachment_path)[-1][1:]
newfilename = 'resume' + '.' + filetype
if filetype=="pdf":
     part["Content-Type"] ="application/pdf"
elif filetype=="doc" or filetype=="docx":
     part['Content-Type']="application/msword"
else:
     pass
part.add_header('Content-Disposition', 'attachment', filename=newfilename)
msg.attach(part)

The infomation is below :

enter image description here

The two content-type : smtp header infomation and attachment header ? Will they influence each other? And "docx" ---can use application/msword? Please forgive me for asking this silly question! Thanks for any help!

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Bella
  • 17
  • 2
  • 6
  • As an aside, it looks like your `email` code was written for an older Python version. The `email` module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) new `EmailMessage` API. Probably throw away this code and start over with modern code from [the Python `email` examples documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Apr 01 '23 at 11:40
  • Perhaps see also https://stackoverflow.com/questions/48562935/what-are-the-parts-in-a-multipart-email/ – tripleee Apr 01 '23 at 11:40

1 Answers1

1

The smtp header most likely will be like:

Content-Type: multipart/related; boundary="----=_NextPart_01D2B948.420196F0"

And each part of message will have its own context type, like this:

------=_NextPart_01D2B948.420196F0
Content-Location: file:///C:/18F2A310/testpage.htm
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

The headers don't impact each other. If you open an eml message or save an outlook message to MHT format, you'll seed the MIME code detail.

And from this link you can find the application context type supported:Media Types

felixc
  • 168
  • 2
  • 11
  • Thank you very much.May I ask you another question? Can one attachement have two content-type? I just set part['Content-Type']="" like this. – Bella Apr 20 '17 at 07:30
  • I didn't try directly manipulate the MIME file to set two content-type but I'm sure in smtplib you cannot do it. – felixc Apr 20 '17 at 15:56
  • sorry! smtplib can do this !just write this :part.add_headers("Content-Type","application/pdf") ; part.add_headers("Content-Type","application/msword") ; It will have two content-type. – Bella Apr 26 '17 at 06:37
  • http://stackoverflow.com/questions/43626463/smtp-send-email-and-why-one-attachment-can-have-two-content-type/43627198?noredirect=1#comment74327164_43627198 This is how to get one content-type! – Bella May 08 '17 at 06:43