Last active
April 3, 2025 06:52
-
-
Save arthurom254/41b598b3e3e6a7427db0c3e10d0b2a9e to your computer and use it in GitHub Desktop.
This is the send email implementation in django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- templates/email/email.html --> | |
<h2 style="color: gray;">Hello {{fname}} {{lname}},</h2> | |
<h3 style="color:darkorchid;">We Have ...</h3> | |
<p>Thank you for ... with us.</p> | |
<p>Regards</p> | |
<p>me...</p> | |
<p>© com</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/send_email.py | |
from django.conf import settings | |
from django.core.mail import EmailMultiAlternatives | |
from django.template.loader import TemplateDoesNotExist, render_to_string | |
from django.utils.html import strip_tags | |
import threading | |
class SendEmail: | |
def __init__( | |
self, | |
to, | |
subject, | |
template, | |
context={}, | |
from_email=None, | |
reply_to=None, | |
**email_kwargs, | |
): | |
self.to = to | |
self.subject = subject | |
self.template = template | |
self.context = context | |
self.from_email = from_email or settings.DEFAULT_FROM_EMAIL | |
self.reply_to = reply_to | |
self.context["template"] = template | |
self.html_content, self.plain_content = self.render_content() | |
self.to = self.to if not isinstance(self.to, str) else [self.to] | |
if self.reply_to: | |
self.reply_to = ( | |
self.reply_to if not isinstance(self.reply_to, str) else [self.reply_to] | |
) | |
self.django_email = EmailMultiAlternatives( | |
subject=self.subject, | |
body=self.plain_content, | |
from_email=self.from_email, | |
to=self.to, | |
reply_to=self.reply_to, | |
**email_kwargs, | |
) | |
self.django_email.attach_alternative(self.html_content, "text/html") | |
def render_content(self): | |
html_content = self.render_html() | |
try: | |
plain_content = self.render_plain() | |
except TemplateDoesNotExist: | |
plain_content = strip_tags(html_content) | |
return html_content, plain_content | |
def render_plain(self): | |
return render_to_string(self.get_plain_template_name(), self.context) | |
def render_html(self): | |
return render_to_string(self.get_html_template_name(), self.context) | |
def get_plain_template_name(self): | |
return f"email/{self.template}.txt" | |
def get_html_template_name(self): | |
return f"email/{self.template}.html" | |
def send(self, **send_kwargs): | |
return self.django_email.send(**send_kwargs) | |
# Usage | |
def send_email(user): | |
client=SendEmail( | |
to=user.email, | |
subject="Subject text here", | |
template="email.html", # HTML template e.g templates/email/email.html and email.txt | |
context={"fname":user.first_name, "lname":user.last_name}, | |
) | |
client.send() | |
def send_threaded_email(user): | |
th=threading.Thread(target=send_email, args=(user,)) | |
th.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CPANEL CONFIG | |
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' | |
EMAIL_HOST = 'mail.server.com' # mail.server.com is your smtp server | |
EMAIL_HOST_USER = '' # [email protected] | |
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER | |
EMAIL_HOST_PASSWORD = '' | |
EMAIL_PORT = 465 # SMTP port | |
EMAIL_USE_SSL = True | |
EMAIL_USE_TLS = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/send_email.py | |
from .send_email import send_email, send_threaded_email | |
def sendmail(request): | |
# Non threaded email | |
send_email(request.user) | |
# if you want threaded email: | |
send_threaded_email(request.user) | |
return HttpResponse("Email Sent!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was very very helpful and easy to implement; just note that you are supposed to use a host email. Happy codding....