Django से Email भेजना
from django.core.mail import send_mail
send_mail(
'Subject',
'Message body',
'[email protected]',
['[email protected]'],
)
# settings.py (development)
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
एक Email Send करना
send_mail() एक subject, plain-text message, sender address, और recipients की एक list लेता है, और यह return करता है कि कितने successfully sent हुए।
उदाहरण: Sending an Email
send_mail() returns 1 if the message was accepted for delivery by the configured backend, 0 otherwise.
from django.core.mail import send_mail
def send_welcome_email(user_email):
send_mail(
'Welcome!',
'Thanks for joining our site.',
'[email protected]',
[user_email],
)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Console Email Backend
Development के दौरान, console backend emails को terminal में print करता है बजाय असल में send करने के — कोई असली mail server नहीं चाहिए।
उदाहरण: The Console Email Backend
Every call to send_mail() will now print the full email content to your terminal instead of delivering it.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
एक View से send_mail() Call करना
एक view एक email को किसी और action के side effect के रूप में trigger कर सकता है, जैसे एक successful registration के बाद।
उदाहरण: Calling send_mail() from a View
This keeps the email-sending logic small and reusable, called right after the account is actually created.
from django.core.mail import send_mail
from django.shortcuts import redirect
def register_and_notify(request, new_user):
send_mail(
'Account Created',
f'Welcome, {new_user.username}!',
'[email protected]',
[new_user.email],
)
return redirect('home')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
EMAIL_BACKENDconfigure करना भूल जाना, जिससे Django development के दौरान एक असली mail server से connect करने की चुपचाप कोशिश (और fail) करता है।- असली SMTP credentials सीधे
settings.pyमें hardcode करना बजाय environment variables से load करने के। send_mail()के return value को न जांचना, जो successfully delivered messages की संख्या return करता है।
- send_mail() एक view से email send करने के लिए Django का built-in shortcut है।
- EMAIL_BACKEND control करता है कि mail असल में कहां जाता है — console backend development के दौरान emails को terminal में print करता है।
- send_mail() एक subject, message, from address, और recipient addresses की एक list लेता है।
- Production email credentials environment variables से आने चाहिए, कभी hardcode नहीं होने चाहिए।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django