sendgrid-python
sendgrid-python copied to clipboard
Auto-determine the mimetype on a attachment
I'm using SendGrid on my website and it allows a user to attach a file to an email. I used to just have the code $email->addattachment($filename) which no longer works on the new version of SendGrid. The new format is this:
$attachment = new Attachment();
$attachment->setContent("xxxx");
$attachment->setType("application/pdf");
$attachment->setFilename("balance_001.pdf");
$attachment->setDisposition("attachment");
$attachment->setContentId("Balance Sheet");
$mail->addAttachment($attachment);
My problem with that is, how am I possibly going to know what filetype the user uploads. It could be anything? Can't I just tell sendgrid to attach whatever file is uploaded to the email being sent?
Hello @jiska78,
Currently, we have not implemented mime-type detection in this version of the helper library. A workaround would be to utilize the mimetypes module, specifically mimetypes.guess_type (reference).
I suggest we add a mimetype function to determine the mimetype and then optionally call it within the file_name function. To make this a non-breaking change, we can add a second parameter to file_name that defaults to false.
This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog. I will update the title for this issue accordingly.
With best regards,
Elmer