def send_invite_to_create_account(invitation):
plaintext = get_template('email-invite.txt')
htmly = get_template('email-invite.html')
context = {"APPLICATION_TITLE": settings.APPLICATION_TITLE,
"CODE": invitation.code,
"URL": invitation.url(),
"EMAIL": invitation.email,
}
subject = '[%s] Invitation Code: %s' % (settings.APPLICATION_TITLE,
invitation.code)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = invitation.email
text_content = plaintext.render(context)
html_content = htmly.render(context)
msg = EmailMultiAlternatives(
subject, text_content, from_email, [
to_email, ])
msg.attach_alternative(html_content, "text/html")
msg.send()
python类EmailMultiAlternatives()的实例源码
def send_invitation_code_to_user(user_code_invitation):
plaintext = get_template('email-user-code-by-email.txt')
htmly = get_template('email-user-code-by-email.html')
context = {"APPLICATION_TITLE": settings.APPLICATION_TITLE,
"CODE": user_code_invitation.code,
"URL": user_code_invitation.url(),
"EMAIL": user_code_invitation.email}
subject = '[%s] Invitation Code: %s' % (settings.APPLICATION_TITLE,
user_code_invitation.code)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = user_code_invitation.email
text_content = plaintext.render(context)
html_content = htmly.render(context)
msg = EmailMultiAlternatives(
subject, text_content, from_email, [
to_email, ])
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_password_reset_url_via_email(user, reset_key):
plaintext = get_template('email-password-reset-link.txt')
htmly = get_template('email-password-reset-link.html')
subject = '[%s] Link to reset your password' % (settings.APPLICATION_TITLE)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = user.email
password_reset_link = '%s%s' % (get_hostname(),
reverse('password_reset_email_verify',
args=(reset_key,)))
context = {"APPLICATION_TITLE": settings.APPLICATION_TITLE,
"FIRST_NAME": user.first_name,
"LAST_NAME": user.last_name,
"PASSWORD_RESET_LINK": password_reset_link}
text_content = plaintext.render(context)
html_content = htmly.render(context)
msg = EmailMultiAlternatives(
subject, text_content, from_email, [
to_email, ])
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_access_token_notifcation(token):
plaintext = get_template('email-access-token-granted.txt')
htmly = get_template('email-access-token-granted.html')
context = {"APPLICATION_TITLE": settings.APPLICATION_TITLE,
"APP_NAME": token.application.name,
"HOSTNAME": get_hostname()
}
subject = '[%s] You just granted access to %s' % (settings.APPLICATION_TITLE,
token.application.name)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = token.user.email
text_content = plaintext.render(context)
html_content = htmly.render(context)
msg = EmailMultiAlternatives(
subject, text_content, from_email, [
to_email, ])
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_mail(subject, email_template_name, recipients, html_email_template_name,
context, email_from=DEFAULT_FROM_EMAIL):
"""
Sends an email to someone
"""
if 'test' in sys.argv:
return # Don't send mails if we are testing to prevent spam
if context['html']:
body = loader.render_to_string(email_template_name, context)
else:
body = email_template_name
email_message = EmailMultiAlternatives(subject, body, email_from, recipients)
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send(fail_silently=True)
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(subject, txt_template, to, context=None, html_template=None, fail_silently=True):
"""
Multipart message helper with template rendering.
"""
site = Site.objects.get_current()
context = context or {}
context.update({
'login_url': "http://%s" % urljoin(site.domain, admin_reverse('index')),
'title': subject,
})
txt_body = render_to_string(txt_template, context)
message = EmailMultiAlternatives(subject=subject, body=txt_body, to=to)
if html_template:
body = render_to_string(html_template, context)
message.attach_alternative(body, 'text/html')
message.send(fail_silently=fail_silently)
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def mail_newsletter(recipients, mail):
email_context = {
'title': mail.subject,
'message': mail.text,
'base_url': settings.EMAIL_ABS_URL,
'sponsors': Sponsor.objects.all(),
}
email_html = render_to_string('newsletter/email_newsletter.html', email_context)
email_plaintext = render_to_string('newsletter/email_newsletter.txt', email_context)
to = [x.email for x in recipients]
# Create a map of emails to unsub tokens for the email merge
unsub_tokens = {recipient.email: {
'unsub_url': '{hostname}{path}'.format(hostname=settings.EMAIL_ABS_URL,
path=reverse('unsub_with_id', kwargs={
'token': recipient.unsubscribe_token
}))} for recipient in recipients}
sender = '{name} <{email}>'.format(name=mail.sender_name, email=mail.sender_email)
email = EmailMultiAlternatives(mail.subject, email_plaintext, sender, to)
email.attach_alternative(email_html, 'text/html')
email.merge_data = unsub_tokens
email.send()
# Create a function called "chunks" with two arguments, l and n:
def send_confirmation_mail(self, request, rental):
subject = ('[Verleihtool] Your rental request, %s %s' %
(rental.firstname, rental.lastname))
message = render_to_string('rental/mails/confirmation.md', {
'rental': rental
}, request)
email = EmailMultiAlternatives(
subject=subject,
body=message,
to=[rental.email],
cc=settings.CC_EMAIL,
)
email.attach_alternative(markdown.markdown(message), 'text/html')
email.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
def send_mail(self, subject_template_name, email_template_name,
context, from_email, to_email, html_email_template_name=None):
"""
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
"""
subject = loader.render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
body = loader.render_to_string(email_template_name, context)
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
if html_email_template_name is not None:
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()