Search code examples
djangofileemaildjango-formsgmail

How to send a file from Django to Gmail?


There is a feedback form in which the user can write a text message and attach the file.

This form is sent to managers by mail (gmail, for example).

Is there any way to make this file come to mail in its normal form (in a preview, for example) so that you can immediately see or download it?

For now, I'm just sending a file link in the message.


Solution

  • Thanks for help @dirkgroten

    def send_contact_us_notification(file, name: str, email: str, body: str, phone='', nda=False):
        try:
            file = settings.MEDIA_URL[1:] + str(contact_form.file)
            from django.core.mail import EmailMessage
            msg = EmailMessage('Subject of the Email', 'Body of the email', 'dinamo.mutu111@gmailcom', ['[email protected]'])
            msg.attach_file(file)
            msg.send()
        except Exception as e:
            print(e)