pythondjangodjango-rest-frameworkdjango-mailer

Sending email with attached .ods File


send_mail('Subject here', 'Here is the message.', 'selva@gmail.com', ['stab@gmail.com'], fail_silently=False)
mail = send_mail('Subject here', 'Here is the message.', 'selvakumaremmy@gmail.com', ['vsolvstab@gmail.com'], fail_silently=False)
mail.attach('AP_MODULE_bugs.ods','AP_MODULE_bugs.ods','application/vnd.oasis.opendocument.spreadsheet')
mail.send()

I'm using Django send_mail class for sending mail. Here I want to send mail with attachment, my attachment File (.ods) is in local storage.


Solution

  • You have to use EmailMessage

    from django.core.mail import EmailMessage
    
    email = EmailMessage(
        'Hello',
        'Body goes here',
        'from@example.com',
        ['to1@example.com', 'to2@example.com'],
        ['bcc@example.com'],
        reply_to=['another@example.com'],
        headers={'Message-ID': 'foo'},
    

    )

    mail.attach('AP_MODULE_bugs.ods',mimetype='application/vnd.oasis.opendocument.spreadsheet')
    

    mail.send()

    attach() creates a new file attachment and adds it to the message. There are two ways to call attach():