mirror of
https://github.com/django/django.git
synced 2025-10-25 14:46:09 +00:00
Imported Django from private SVN repository (created from r. 8825)
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
51
django/core/mail.py
Normal file
51
django/core/mail.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""
|
||||
Use this for e-mailing
|
||||
"""
|
||||
|
||||
from django.conf.settings import DEFAULT_FROM_EMAIL, EMAIL_HOST
|
||||
from email.MIMEText import MIMEText
|
||||
import smtplib
|
||||
|
||||
def send_mail(subject, message, from_email, recipient_list, fail_silently=False):
|
||||
"""
|
||||
Easy wrapper for sending a single message to a recipient list. All members
|
||||
of the recipient list will see the other recipients in the 'To' field.
|
||||
"""
|
||||
return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently)
|
||||
|
||||
def send_mass_mail(datatuple, fail_silently=False):
|
||||
"""
|
||||
Given a datatuple of (subject, message, from_email, recipient_list), sends
|
||||
each message to each recipient list. Returns the number of e-mails sent.
|
||||
|
||||
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
|
||||
"""
|
||||
try:
|
||||
server = smtplib.SMTP(EMAIL_HOST)
|
||||
except:
|
||||
if fail_silently:
|
||||
return
|
||||
raise
|
||||
num_sent = 0
|
||||
for subject, message, from_email, recipient_list in datatuple:
|
||||
if not recipient_list:
|
||||
continue
|
||||
from_email = from_email or DEFAULT_FROM_EMAIL
|
||||
msg = MIMEText(message)
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = from_email
|
||||
msg['To'] = ', '.join(recipient_list)
|
||||
server.sendmail(from_email, recipient_list, msg.as_string())
|
||||
num_sent += 1
|
||||
server.quit()
|
||||
return num_sent
|
||||
|
||||
def mail_admins(subject, message, fail_silently=False):
|
||||
"Sends a message to the admins, as defined by the ADMINS constant in settings.py."
|
||||
from django.conf.settings import ADMINS, SERVER_EMAIL
|
||||
send_mail('[CMS] ' + subject, message, SERVER_EMAIL, [a[1] for a in ADMINS], fail_silently)
|
||||
|
||||
def mail_managers(subject, message, fail_silently=False):
|
||||
"Sends a message to the managers, as defined by the MANAGERS constant in settings.py"
|
||||
from django.conf.settings import MANAGERS, SERVER_EMAIL
|
||||
send_mail('[CMS] ' + subject, message, SERVER_EMAIL, [a[1] for a in MANAGERS], fail_silently)
|
||||
Reference in New Issue
Block a user