Django sends email

In PHP, I can send an email just by calling mail() . In Django, I need to specify servers, etc.

Is there an easier way to send email from Django?

+4
source share
3 answers

The django.core.mail module has some good email features.

For the tutorial, see Sending Email :

Although Python makes sending emails relatively easy through the smtplib libraries, Django provides a couple of light wrappers over it. These wrappers are provided for sending emails very quickly so that it is easy to test sending emails during development and to support platforms that do not use SMTP.

The simplest function that is likely to meet your goals is the send_mail function:

 send_mail( subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None) 
+3
source

In PHP, you can only send mail using the simple mail () command on systems other than Windows. They expect that a local MTA, such as Postfix, will be installed and configured correctly, as it should be on most web servers. If you want to depend on a third-party or decentralized mail service depends on how critical your application is. A serious dependence on fast and reliable e-mail transmission usually leads to sending mail via SMTP to a central mail server ("big pipe").

However, if you want to have the same function as in PHP, try the following:

 import subprocess def send_mail(from_addr, to_addr, subject, body): cmdline = ["/usr/sbin/sendmail", "-f"] cmdline.append(from_addr) cmdline.append(to_addr) mailer = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE) dialog = "From: %s\nTo: %s\nSubject: %s\n\n%s\n.\n" % (from_addr, to_addr, subject, body) return mailer.communicate(dialog) 

And use it like: send_mail ("Me < myself@mydomain.com >", "Recip Ient < other@hisdomain.com >", "Teh' Subject", "Mail body")

+2
source

In any case, you will need part of the backend (read the MTA). From my head I can think of two things:

0
source

All Articles