def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
python类get_connection()的实例源码
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def deliver_to_recipient(recipient, subject, message, from_email,
message_html=None, attachments=None, nofilter=False, rfc2822_headers=None):
msg = create_mail(subject, message, from_email, recipient, message_html,
attachments, rfc2822_headers)
msgid = msg.extra_headers['Message-ID']
backend = settings.EMAIL_BACKEND
if nofilter or recipient.split('@')[1] in settings.EMAIL_UNFILTERED_DOMAINS:
backend = settings.EMAIL_BACKEND_UNFILTERED
connection = mail.get_connection(backend=backend)
connection.send_messages([msg])
return (msgid, msg.message(),)
def sendEmail(subject,content,from_address,from_name='',to=[],cc=[],bcc=[],attachment_name='attachment',attachment=None,html_content=None):
# Ensure that email address information is in list form and that there are no empty values
recipients = [x for x in to + cc if x]
bcc = [x for x in bcc if x]
from_email = from_name + ' <' + from_address + '>' if from_address else None
reply_to = [from_address,] if from_address else None
logger.info('Sending email from %s to %s' % (from_address,recipients))
if getattr(settings,'DEBUG',None):
logger.info('Email content:\n\n%s' % content)
logger.info('Email HTML content:\n\n%s' % html_content)
with get_connection() as connection:
connection.open()
message = EmailMultiAlternatives(
subject=subject,
body=content,
from_email=from_email,
to=recipients,
bcc=bcc,
reply_to=reply_to,
connection=connection,
)
if html_content:
message.attach_alternative(html_content, "text/html")
if attachment:
message.attach(attachment_name, attachment)
message.send(fail_silently=False)
connection.close()
def bulk_email_view(self, request, object_id):
season = get_object_or_404(Season, pk=object_id)
if not request.user.has_perm('tournament.bulk_email', season.league):
raise PermissionDenied
if request.method == 'POST':
form = forms.BulkEmailForm(season.seasonplayer_set.count(), request.POST)
if form.is_valid() and form.cleaned_data['confirm_send']:
season_players = season.seasonplayer_set.all()
email_addresses = {sp.player.email for sp in season_players if sp.player.email != ''}
email_messages = []
for addr in email_addresses:
message = EmailMultiAlternatives(
form.cleaned_data['subject'],
form.cleaned_data['text_content'],
settings.DEFAULT_FROM_EMAIL,
[addr]
)
message.attach_alternative(form.cleaned_data['html_content'], 'text/html')
email_messages.append(message)
conn = mail.get_connection()
conn.open()
conn.send_messages(email_messages)
conn.close()
self.message_user(request, 'Emails sent to %d players.' % len(season_players), messages.INFO)
return redirect('admin:tournament_season_changelist')
else:
form = forms.BulkEmailForm(season.seasonplayer_set.count())
context = {
'has_permission': True,
'opts': self.model._meta,
'site_url': '/',
'original': season,
'title': 'Bulk email',
'form': form
}
return render(request, 'tournament/admin/bulk_email.html', context)
def handle(self, email=True, verbosity=1, **kwargs):
self.verbosity = verbosity
UserModel = get_user_model()
exp = ExpirySettings.get()
oldest = exp.earliest_possible_login
if oldest is None:
self._info("Account expiry not configured; nothing to do.")
return
self._info("Checking for users who haven't logged in since %s" % oldest)
gone = UserModel.objects.filter(is_active=True, last_login__lt=oldest)
for username in gone.values_list(UserModel.USERNAME_FIELD, flat=True):
self._info("Deactiviting user: %s" % username)
messages = list(filter(None, (self._make_email(exp, user) for user in gone)))
count = gone.update(is_active=False)
if count:
self._info("%d account(s) expired" % count)
if email and messages:
self._info("Sending e-mails")
connection = mail.get_connection()
connection.send_messages(messages)
self._info("Done")
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])
def get_connection(self, fail_silently=False):
from django.core.mail import get_connection
if not self.connection:
self.connection = get_connection(fail_silently=fail_silently)
return self.connection
def send(self, fail_silently=False):
"""Sends the email message."""
if not self.recipients():
# Don't bother creating the network connection if there's nobody to
# send to.
return 0
return self.get_connection(fail_silently).send_messages([self])