python - español - django wikipedia
¿Cómo enviar un correo electrónico html con django con contenido dinámico? (5)
Ejemplo:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
subject, from_email, to = ''Subject'', ''[email protected]'', ''[email protected]''
html_content = render_to_string(''mail_template.html'', {''varname'':''value''}) # render with dynamic value
text_content = strip_tags(html_content) # Strip the html tag. So people can see the pure text at least.
# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
Referencia
http://www.djangofoo.com/250/sending-html-email/comment-page-1#comment-11401
¿Alguien puede ayudarme a enviar un correo electrónico html con contenidos dinámicos? Una forma es copiar todo el código html en una variable y rellenar el código dinámico dentro de las vistas de Django, pero no parece ser una buena idea, ya que es un archivo html muy grande.
Apreciaria cualquier sugerencia.
Gracias.
Django incluye el método django.core.mail.send_mail
estos días (2018), sin necesidad de usar la clase EmailMultiAlternatives
directamente. Haga esto en su lugar:
from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
subject = ''Subject''
html_message = render_to_string(''mail_template.html'', {''context'': ''values''})
plain_message = strip_tags(html_message)
from_email = ''From <[email protected]>''
to = ''[email protected]''
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)
Esto enviará un correo electrónico que es visible en ambos navegadores compatibles con html y mostrará texto sin formato en los visores de correo electrónico paralizados.
Esto debería hacer lo que quieras:
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import get_template
template = get_template(''myapp/email.html'')
context = Context({''user'': user, ''other_info'': info})
content = template.render(context)
if not user.email:
raise BadHeaderError(''No email address given for {0}''.format(user))
msg = EmailMessage(subject, content, from, to=[user.email,])
msg.send()
Ver los documentos de correo de Django para más.
Prueba esto::::
https://godjango.com/19-using-templates-for-sending-emails/
https://godjango.com/19-using-templates-for-sending-emails/
# views.py
from django.http import HttpResponse
from django.template import Context
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage
def email_one(request):
subject = "I am a text email"
to = [''[email protected]'']
from_email = ''[email protected]''
ctx = {
''user'': ''buddy'',
''purchase'': ''Books''
}
message = render_to_string(''main/email/email.txt'', ctx)
EmailMessage(subject, message, to=to, from_email=from_email).send()
return HttpResponse(''email_one'')
def email_two(request):
subject = "I am an HTML email"
to = [''[email protected]'']
from_email = ''[email protected]''
ctx = {
''user'': ''buddy'',
''purchase'': ''Books''
}
message = get_template(''main/email/email.html'').render(Context(ctx))
msg = EmailMessage(subject, message, to=to, from_email=from_email)
msg.content_subtype = ''html''
msg.send()
return HttpResponse(''email_two'')
Si desea plantillas de correo electrónico dinámicas para su correo, guarde el contenido del correo electrónico en las tablas de su base de datos. Esto es lo que guardé como código HTML en la base de datos =
<p>Hello.. {{ first_name }} {{ last_name }}. <br> This is an <strong>important</strong> {{ message }}
<br> <b> By Admin.</b>
<p style=''color:red''> Good Day </p>
En sus puntos de vista:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
def dynamic_email(request):
application_obj = AppDetails.objects.get(id=1)
subject = ''First Interview Call''
email = request.user.email
to_email = application_obj.email
message = application_obj.message
text_content = ''This is an important message.''
d = {''first_name'': application_obj.first_name,''message'':message}
htmly = FirstInterviewCall.objects.get(id=1).html_content #this is what i have saved previously in database which i have to send as Email template as mentioned above HTML code
open("partner/templates/first_interview.html", "w").close() # this is the path of my file partner is the app, Here i am clearing the file content. If file not found it will create one on given path.
text_file = open("partner/templates/first_interview.html", "w") # opening my file
text_file.write(htmly) #putting HTML content in file which i saved in DB
text_file.close() #file close
htmly = get_template(''first_interview.html'')
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, email, [to_email])
msg.attach_alternative(html_content, "text/html")
msg.send()
Esto enviará a la plantilla HTML dinámica lo que haya guardado en Db.