def view(self, post=False):
leagues = [self.league] + list(League.objects.filter(is_active=True).order_by('display_order').exclude(pk=self.league.pk))
if post:
form = ContactForm(self.request.POST, leagues=leagues)
if form.is_valid():
league = League.objects.get(tag=form.cleaned_data['league'])
for mod in league.leaguemoderator_set.all():
if mod.send_contact_emails and mod.player.email:
message = EmailMessage(
'[%s] %s' % (league.name, form.cleaned_data['subject']),
'Sender:\n%s\n%s\n\nMessage:\n%s' %
(form.cleaned_data['your_lichess_username'], form.cleaned_data['your_email_address'], form.cleaned_data['message']),
settings.DEFAULT_FROM_EMAIL,
[mod.player.email]
)
message.send()
return redirect(leagueurl('contact_success', league_tag=self.league.tag))
else:
form = ContactForm(leagues=leagues)
context = {
'form': form,
}
return self.render('tournament/contact.html', context)
python类EmailMessage()的实例源码
def send_email_message(template_name, from_, to, bcc, context, headers=None):
"""
Send an email message.
:param template_name: Use to construct the real template names for the
subject and body like this: "tutorials/email/%(template_name)s/subject.txt"
and "tutorials/email/%(template_name)s/body.txt"
:param from_: From address to use
:param to: List of addresses to send to
:param to: List of addresses to send via bcc
:param context: Dictionary with context to use when rendering the
templates.
:param headers: dict of optional, additional email headers
"""
context = Context(context)
name = "tutorials/email/%s/subject.txt" % template_name
subject_template = get_template(name)
subject = subject_template.render(context)
# subjects must be a single line, no newlines
# if there's a trailing newline, strip it; anything more than that,
# let it fail (tests will fail) so we know the subject template is
# not valid.
subject = subject.rstrip(u"\n")
name = "tutorials/email/%s/body.txt" % template_name
body_template = get_template(name)
body = body_template.render(context)
# Important: By default, the MIME type of the body parameter in an
# EmailMessage is "text/plain". That makes it safe to use "|safe" in
# our email templates, and we do. If you change this to, say, send
# HTML format email, you must go through the email templates and do
# something better about escaping user data for safety.
email = EmailMessage(subject, body, from_, to, bcc, headers=headers)
email.send()
def send_email_message(template_name, from_, to, context, headers=None):
"""
Send an email message.
:param template_name: Use to construct the real template names for the
subject and body like this: "finaid/email/%(template_name)s/subject.txt"
and "finaid/email/%(template_name)s/body.txt"
:param from_: From address to use
:param to: List of addresses to send to
:param context: Dictionary with context to use when rendering the
templates.
:param headers: dict of optional, additional email headers
"""
context = Context(context)
name = "finaid/email/%s/subject.txt" % template_name
subject_template = get_template(name)
subject = subject_template.render(context)
# subjects must be a single line, no newlines
# if there's a trailing newline, strip it; anything more than that,
# let it fail (tests will fail) so we know the subject template is
# not valid.
subject = subject.rstrip(u"\n")
name = "finaid/email/%s/body.txt" % template_name
body_template = get_template(name)
body = body_template.render(context)
# Important: By default, the MIME type of the body parameter in an
# EmailMessage is "text/plain". That makes it safe to use "|safe" in
# our email templates, and we do. If you change this to, say, send
# HTML format email, you must go through the email templates and do
# something better about escaping user data for safety.
email = EmailMessage(subject, body, from_, to, headers=headers)
email.send()
def enviar_correo(destinatario,asunto,cuerpo):
email = EmailMessage()
email.subject = asunto
email.body = cuerpo
email.to = destinatario
email.connection = connection
try:
email.send()
except:
pass
def post(self, request, *args, **kwargs):
""" Sends the feedback email to admin.
"""
obj = self.get_object()
form = FeedbackForm(request.POST)
if not form.is_valid():
return render(request, 'app/article_detail.html', {
'feedback_form': form,
'object': obj,
'already_voted': False,
'display_form': True,
})
email = form.cleaned_data['email']
email_message = EmailMessage(
subject=_('New feedback from article {0}'.format(obj.name)),
body=render_to_string('feedback_email.html', {
'feedback_message': form.cleaned_data['description'],
'feedback_email': email,
'article': obj,
'base_url': settings.SITE_URL,
}),
to=[settings.SUPPORT_EMAIL],
reply_to=[email],
)
email_message.content_subtype = 'html'
email_message.send()
messages.success(
request,
_('Thank you for sending your feedback!')
)
return redirect(obj)