Using msg.add_attachment what is the syntax for adding a PDF document?

Viewed 132

I am trying to attach a PDF document to an email. I've come across this syntax:

add_attachment(*args, content_manager=None, *kw) If the message is a multipart/mixed, create a new message object, pass all of the arguments to its set_content() method, and attach() it to the multipart. If the message is a non-multipart, multipart/related, or multipart/alternative, call make_mixed() and then proceed as above. If content_manager is not specified, use the content_manager specified by the current policy. If the added part has no Content-Disposition header, add one with the value attachment. This method can be used both for explicit attachments (Content-Disposition: attachment) and inline attachments (Content-Disposition: inline), by passing appropriate options to the content_manager.

I am not too sure how to use this to add a PDF document. Any ideas on how I can do this?

1 Answers
with open(r'C:\Users\David\Documents\Hello.pdf', 'rb') as f:
    file_data = f.read()
    file_name = f.name

msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)

This same syntax could also be used for .txt files. Possibly others too but I haven't tested it out beyond that.

Related