App Engine Python SDK version 1.9.13
[gae.git] / python / google / appengine / _internal / django / core / mail / __init__.py
blobaa8932c745ad97d6626e696c8e004a3e1daf4ed7
1 """
2 Tools for sending email.
3 """
5 from google.appengine._internal.django.conf import settings
6 from google.appengine._internal.django.core.exceptions import ImproperlyConfigured
7 from google.appengine._internal.django.utils.importlib import import_module
9 # Imported for backwards compatibility, and for the sake
10 # of a cleaner namespace. These symbols used to be in
11 # django/core/mail.py before the introduction of email
12 # backends and the subsequent reorganization (See #10355)
13 from google.appengine._internal.django.core.mail.utils import CachedDnsName, DNS_NAME
14 from google.appengine._internal.django.core.mail.message import EmailMessage, EmailMultiAlternatives, SafeMIMEText, SafeMIMEMultipart, DEFAULT_ATTACHMENT_MIME_TYPE, make_msgid, BadHeaderError, forbid_multi_line_headers
15 from google.appengine._internal.django.core.mail.backends.smtp import EmailBackend as _SMTPConnection
17 def get_connection(backend=None, fail_silently=False, **kwds):
18 """Load an e-mail backend and return an instance of it.
20 If backend is None (default) settings.EMAIL_BACKEND is used.
22 Both fail_silently and other keyword arguments are used in the
23 constructor of the backend.
24 """
25 path = backend or settings.EMAIL_BACKEND
26 try:
27 mod_name, klass_name = path.rsplit('.', 1)
28 mod = import_module(mod_name)
29 except ImportError, e:
30 raise ImproperlyConfigured(('Error importing email backend module %s: "%s"'
31 % (mod_name, e)))
32 try:
33 klass = getattr(mod, klass_name)
34 except AttributeError:
35 raise ImproperlyConfigured(('Module "%s" does not define a '
36 '"%s" class' % (mod_name, klass_name)))
37 return klass(fail_silently=fail_silently, **kwds)
40 def send_mail(subject, message, from_email, recipient_list,
41 fail_silently=False, auth_user=None, auth_password=None,
42 connection=None):
43 """
44 Easy wrapper for sending a single message to a recipient list. All members
45 of the recipient list will see the other recipients in the 'To' field.
47 If auth_user is None, the EMAIL_HOST_USER setting is used.
48 If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
50 Note: The API for this method is frozen. New code wanting to extend the
51 functionality should use the EmailMessage class directly.
52 """
53 connection = connection or get_connection(username=auth_user,
54 password=auth_password,
55 fail_silently=fail_silently)
56 return EmailMessage(subject, message, from_email, recipient_list,
57 connection=connection).send()
60 def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
61 auth_password=None, connection=None):
62 """
63 Given a datatuple of (subject, message, from_email, recipient_list), sends
64 each message to each recipient list. Returns the number of e-mails sent.
66 If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
67 If auth_user and auth_password are set, they're used to log in.
68 If auth_user is None, the EMAIL_HOST_USER setting is used.
69 If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
71 Note: The API for this method is frozen. New code wanting to extend the
72 functionality should use the EmailMessage class directly.
73 """
74 connection = connection or get_connection(username=auth_user,
75 password=auth_password,
76 fail_silently=fail_silently)
77 messages = [EmailMessage(subject, message, sender, recipient)
78 for subject, message, sender, recipient in datatuple]
79 return connection.send_messages(messages)
82 def mail_admins(subject, message, fail_silently=False, connection=None):
83 """Sends a message to the admins, as defined by the ADMINS setting."""
84 if not settings.ADMINS:
85 return
86 EmailMessage(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
87 settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
88 connection=connection).send(fail_silently=fail_silently)
91 def mail_managers(subject, message, fail_silently=False, connection=None):
92 """Sends a message to the managers, as defined by the MANAGERS setting."""
93 if not settings.MANAGERS:
94 return
95 EmailMessage(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
96 settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
97 connection=connection).send(fail_silently=fail_silently)
100 class SMTPConnection(_SMTPConnection):
101 def __init__(self, *args, **kwds):
102 import warnings
103 warnings.warn(
104 'mail.SMTPConnection is deprecated; use mail.get_connection() instead.',
105 PendingDeprecationWarning
107 super(SMTPConnection, self).__init__(*args, **kwds)