Django – Sending E-mails
Django – Sending E-mails
Django comes with a light engine that is ready and simple to use to send emails. Similar to Python, you just need to import a smtplib. You just need to import django.core.mail in Django. Edit the project settings.py file to start sending emails and set all the following options:
- EMAIL_HOST – server with smtp.
- EMAIL_HOST_USER – smtp server login key.
- EMAIL_HOST_PASSWORD ? Password credential for the smtp server.
- EMAIL_PORT – server port smtp.
- EMAIL_USE_TLS or _SSL – if it is secure connection then set True.
Simple E-mail Sending
To submit a simple e-mail, let's create it a "simpleEmail" view.
from django.core.mail import send_mail from django.http import HttpResponse def simpleEmail(request,emailto): res = send_mail("hello deepak", "comment tu ?", "[email protected]", [emailto]) return HttpResponse('%s'%res)
Description of the parameter s of send mail-
- subject – In this we write subject about e-mail.
- message –In this we write body for e-mail .
- from_email – e-mail from.
- recipient_list ? List of receivers’ e-mail address.
- Fail_silently ? Bool, if false send mail will raise an exception in case of error.
- auth_user ? User login if not set in settings.py.
- auth_password ?In settings.py user password will not set.
- connection – Backend of e-mail.
- html_message –It is new in Django 1.7 if present the e-mail will be multipart.
Creating URL to access a view.
from django.conf.urls import patterns, url urlpatterns = paterns('myapp.views', url(r'^simpleemail/(?P<emailto> [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/', 'sendSimpleEmail' , name = 'sendSimpleEmail'),)
Sending Multiple Mails with send_mass_mail.
The process returns the number of messages sent successfully. This is the same as send mail but takes an extra parameter; datatuple, our view of sendMassEmail will be ?
from django.core.mail import send_mass_mail from django.http import HttpResponse def sendMassEmail(request,emailto): message1 = ('subject 1', 'message 1', '[email protected]', [first email]) message2 = ('subject 2', 'message 2', ' [email protected]', [second email]) res = send_mass_mail((msg1, msg2), fail_silently = False) return HttpResponse('%s'%res)
Creating URL to access a view.
from django.conf.urls import patterns, url urlpatterns = paterns('myapp.views', url(r'^massEmail/(?P<emailto1> [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P<emailto2> [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})', 'sendMassEmail' , name = 'sendMassEmail'),)
send_mass_mail details parameters are –
- datatuples ? A tuple where element like (subject, message, from_email, recipent_list)
- fail_silently ? Boolean, in case of error, if false send_mail raises an exception.
- auth_user – login of user will not set in settings.py.
- auth_password – password of user will not set in setting.py .
- connection ? E-mail backend.
Note ? In the above example we are using Python smtp debuggingserver, that can launch using –
$python -m smtpd -n -c DebuggingServer localhost:1025
This means all your sent e-mails will be printed on stdout , and the dummy server is running on localhost:1025.