Good day, I'm trying to send a html in django email, pls what more can I added to the code.
the email sending is functioning well but it still shows the html tags.
from celery import task
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage
from orders.models import Order
#task
def payment_completed(order_id):
order = Order.objects.get(id=order_id)
subject = f'Testing html sending'
message = render_to_string('orders/order/pdf.html', {'order':order})
email = EmailMessage(
subject,
message,
'youremai#gmail.com',
[order.email, 'youremai#gmail.com']
)
email.content_subtype = 'html'
email.send()
I've tried render_to_string and get_template
same result
You can use EmailMultiAlternatives instead of EmailMessage, and code may look like this
email = EmailMultiAlternatives(
subject,
message_plain_text,
'youremai#gmail.com',
[order.email, 'youremai#gmail.com']
)
email.attach_alternative(message, 'text/html')
email.send()
Related
I have a problem the form works but it does not send me the emails could you help me It is my first publication, I honestly have no idea what it could be
[CODE SETTINGS]
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_USE_TLS=True
EMAIL_PORT=587
EMAIL_HOST_USER='xxxxx#gmail.com'
EMAIL_HOST_PASSWORD='xxxxxxxxxxxxxxxxx'**
[** CODE VIEWS **]
from django.shortcuts import render, redirect
from .forms import FormularioContacto
from django.conf import settings
from django.core.mail import EmailMessage
# Create your views here.
def contacto(request):
form=FormularioContacto()
if request.method == "POST":
form=FormularioContacto(data=request.POST)
if form.is_valid():
nombre=request.POST.get("nombre")
email=request.POST.get("email")
contenido=request.POST.get("contenido")
email= EmailMessage(f"Alguien quiere contactarse desde la WEB,El Usuario: {nombre} con el email: {email} por el asunto: {contenido}","",["torresfdev#gmail.com"],reply_to=[email])
email.send()
try:
return redirect("/contacto/?Enviadoconexito")
except:
return redirect ("/contacto/?NO_se_pudo_enviar")
return render (request,"contactoapp/contacto.html", {"form":form})
I think you are using the wrong email function
in django email documentation, they recommend using send_mail
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from#example.com',
['to#example.com'],
fail_silently=False,
)
and also check if your gmail account allows access to less secured apps (less secured app access in gmail)
I want to send email to thousands of users as quick as possible. I just want to confirm that the way I'm trying is good enough or not. What's the best way to do that?
Here is my code snippet:
from django.core.mail import send_mail, EmailMessage
from django.template import loader
def email_sender(email_object):
subject = email_object["subject"]
html_body = email_object["html_body"]
from_email = email_object["from_email"]
recipient_list = email_object["to_email"]
if isinstance(recipient_list, str):
recipient_list=[recipient_list]
print("recipient_list :",recipient_list)
html_message = loader.render_to_string(html_body,{})
mail = EmailMessage(
subject=subject,
body=html_message,
from_email=from_email,
to=recipient_list
)
mail.content_subtype = "html" # Main content is now text/html
mail.send()
I want to create pie chart using highcharts.js and attach to email but when i send email charts not render, can anybody help me to render chart in email attachements.
Django's 1.7 in send_email method the html_message parameter was added.
Go with this:-
from django.core.mail import send_mail
from django.template.loader import render_to_string
msg_plain = render_to_string('templates/email.txt', {'some_params': some_params})
msg_html = render_to_string('templates/email.html', {'some_params': some_params})
send_mail(
'email title',
msg_plain,
'some#sender.com',
['some#receiver.com'],
html_message=msg_html,
)
I have read similar questions but I cannot understand why this is not working. I am trying to send a reactivation email to user when they click on a link. The activation link is generated properly when the user signs up and the email is sent, but when I try to call the same function again to reactivate link, it is not working saying it is missing one parameter. Here's the function:
acounts/views.py
def sendactivationmail(request, user):
# Creating token and masing uid of user
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
# Sending email for email verification
subject = 'Please Verify Your Email'
from_email = settings.DEFAULT_FROM_EMAIL
to_email = [user.email]
context = {
'url': settings.BASE_URL + reverse('user-activation-link', kwargs={
'uidb64': uid,
'token': token,
}),
}
contact_message = render_to_string('verify_email.html', context)
plain_message = strip_tags(contact_message)
send_mail(subject, plain_message, from_email, to_email, html_message=contact_message, fail_silently=True)
return redirect(reverse('login'))
accounts/urls.py
from django.conf.urls import url
from .views import *
from . import views
from urllib import request
from django.contrib.auth.models import User
urlpatterns = [
url(r'^reactivate/$', views.sendactivationmail(request, User), name='reactivate'),
Is this the right way to pass on the request and user parameters to the function?
EDIT: This is the link that is used to redirect user:
<p>Click here to resend the activation email.</p>
change your urlpattern to this
urlpatterns = [
url(r'^reactivate/$', views.sendactivationmail, name='reactivate'),
.....
]
and html to, but the user has to be logged in to use this link. remember that
<p>Click here to resend the activation email.</p>
Currently I have a working implementation of sending emails with Amazon SES in Django.
My setup looks like this. In settings.py I have:
EMAIL_HOST = 'email-smtp....'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'my-email-host-user'
EMAIL_HOST_PASSWORD = 'my-email-host-password'
EMAIL_USE_TLS = True
In my view I have:
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.contrib import messages
from django.core.mail import send_mail
from django.core.mail import EmailMessage
def index(request):
email_message = EmailMessage('This is a title', 'This is a message body', 'FromEmail#example.com', ['ToEmail#example.com'])
email_message.send()
return HttpResponse("You just sent an email message.")
When I open the message I get this in the header:
FromEmail#example.com via amazonses.com
I would like to customize this so that I can do something like this:
UserFirstName UserLastName via amazonses.com
How would I go about doing this?
I doubt that you can, but you should be able to use from addresses like this:
email_message = EmailMessage('This is a title', 'This is a message body', 'UserFirstName UserLastName <FromEmail#example.com>', ['ToEmail#example.com'])
which should result in email clients displaying the from address like this:
UserFirstName UserLastName <FromEmail#example.com>