send html mail with attachment in django

 SourceURL:file:///home/wadmin/My Files/sedn html email with attachment.docx


send html page rendered mail with attachment.

 

 

views.py

from django.shortcuts import render

 

from django.http import HttpResponse

from django.core.mail import send_mail

 

from django.core.mail import send_mail, EmailMessage

from django.template.loader import render_to_string

from django.contrib.staticfiles import finders

 

def mailView(request):

    try:

        subject = 'This is a test mail'

        message = render_to_string('mailfile.html')

        from_email = 'arnab.gupta@weavers-web.com'

        to_email = ['iamshimantadas123@yopmail.com']

 

        email = EmailMessage(subject, message, from_email, to_email)

        email.content_subtype = "html"

        brian_image_path = finders.find('brian.jpg')

        email.attach_file(brian_image_path)

 

        email.send()

 

        return HttpResponse("Mail sent successfully!")

    except Exception as e:

        print(e)

        return HttpResponse("mail not send")     

 

 

mainapp/settings.py

# settings

EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"

SENDGRID_API_KEY = os.environ.get("SENDGRID_API_KEY")

SENDGRID_SANDBOX_MODE_IN_DEBUG = False

 

Popular posts from this blog

MCSL 216 MCA NEW Practical ~ common questions suggestions

dev chaeatsheet

STRAPI