4

In PHP I can send an email simply by calling mail(). In Django, I need to specify SMTP backends and other things.

Is there a simpler way to send email from Django?

4
  • 4
    The only reason you didn't have to specify the SMTP settings in PHP was because someone else had already done the work for you when they setup the webserver. All email is sent via SMTP. The only question is which SMTP server will accept a connection from your client and how does your client authenticate itself to the server. Your local syadmin should be able to provide this information in about 10 seconds flat. (OK, maybe 20 seconds.) Commented Dec 26, 2009 at 16:38
  • ok, I found the info in php.ini, but now sending emails is VERY slow. Commented Dec 26, 2009 at 16:52
  • How did you measure that it VERY slow ? Commented Dec 26, 2009 at 21:27
  • I send my form and wait 10-15 seconds until page loads Commented Dec 26, 2009 at 22:08

3 Answers 3

3

There are several good mail-sending functions in the django.core.mail module.

For a tutorial please see Sending e-mail:

Although Python makes sending e-mail relatively easy via the smtplib library, Django provides a couple of light wrappers over it. These wrappers are provided to make sending e-mail extra quick, to make it easy to test e-mail sending during development, and to provide support for platforms that can’t use SMTP.

The simplest function that would most likely suit your purposes is the send_mail function:

send_mail(
    subject, 
    message, 
    from_email, 
    recipient_list, 
    fail_silently=False, 
    auth_user=None, 
    auth_password=None, 
    connection=None)
Sign up to request clarification or add additional context in comments.

3 Comments

"Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings." ??
Well you need to specify who will be sending your email - even PHP cannot send email without some sort of SMTP server configured at some location for sending email.
ok, I found the info in php.ini, but now sending emails is VERY slow.
2

In PHP you can only send mail with a simple mail() command on non-Windows systems. These will expect a local MTA like Postfix to be installed and correctly configured, as should be the case for most web servers. If you want to depend on third-party or decentralized mail service depends on how critical email is for your application. Serious dependency on speedy and reliable email transmission usually results in sending mail via SMTP to a central mail server (the "big pipe").

Still, if you want to have the same function as in PHP, try this:

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 <[email protected]>", "Recip Ient <[email protected]>", "Teh' Subject", "Mail body")

Comments

0

Either way, you need some backend (read MTA). Of the top of my head I can think of two things:

  1. As already pointed out, you can for example use sendmail http://djangosnippets.org/snippets/1864/
  2. Even better, use a Python MTA. There's Lamson, a Python email server (MTA): http://lamsonproject.org/docs/hooking_into_django.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.