'Django EmailMessgae() Error: MIMEPart.__init__()

This is my views file: and this is the screenshot of the error

I have made a token file to generate a link and info file for details like EMAIL_HOST_USER and etc.

        current_site = get_current_site(request)
        email_subject = "Confirm your email @XYZ"
        message2 = render_to_string('email_confirmation.html',{
            'name': myuser.first_name,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(myuser.pk)),
            'token': generate_token.make_token(myuser),
        })

        email = EmailMessage(
            email_subject,
            message2,
            settings.EMAIL_HOST_USER,
            [myuser.email],
        )
        email.fail_silently=False
        email.send()


Solution 1:[1]

Had this same issue as well, but, it turned out that I was importing the EmailMessage class.

Instead of doing this:

from email.message import EmailMessage 

Use:

from django.core.mail import EmailMessage

Solution 2:[2]

EmailMessage(...)[Django-doc] don't have any method named as send_mail() it has to be send() and EmailMessage don't have any attribute named as fail_silently it's a parameter passed to send() method so finally your code will look like this

email = EmailMessage(
            email_subject,
            message2,
            settings.EMAIL_HOST_USER,
            [myuser.email],
        )
email.content_subtype = "html" # can specify type of your content
email.send(fail_silently=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Freddy Mcloughlan
Solution 2 Ankit Tiwari