← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 12 of 12

Django से Email भेजना

Django का mail system एक built-in mailbox slot रखने जैसा है — आप एक message drop करते हैं और Django इसे send करने का काम handle करता है।
Syntax
markup
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.

markup
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 नहीं चाहिए।

Note: यह email-sending code को असली inboxes spam किए बिना test करने का सबसे safe तरीका है।

उदाहरण: The Console Email Backend

Every call to send_mail() will now print the full email content to your terminal instead of delivering it.

markup
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.

markup
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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. EMAIL_BACKEND configure करना भूल जाना, जिससे Django development के दौरान एक असली mail server से connect करने की चुपचाप कोशिश (और fail) करता है।
  2. असली SMTP credentials सीधे settings.py में hardcode करना बजाय environment variables से load करने के।
  3. 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 नहीं होने चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.