def send_email(self):
if self.code and self.account:
subject = '{from_name} user account access code'.format(from_name=settings.EMAIL_FROM)
message = '''Dear {first} {last},\n
You are receiveing this email as you have asked to reset a part of your account.\n
Please input the following code when asked: {code}\n
Thank you,\n
{from_name}'''.format(first=self.account.first_name,
last=self.account.last_name,
code=self.code,
from_name=settings.EMAIL_FROM)
try:
send_mail(subject,
message,
settings.EMAIL_HOST_USER,
(self.account.email,),
fail_silently=False)
except smtplib.SMTPException:
return False
return True
python类EMAIL_FROM的实例源码
def send_someone_joined_notification(membership_pk):
try:
membership = Membership.objects.select_related('person', 'supportgroup').get(pk=membership_pk)
except Membership.DoesNotExist:
return
person_information = str(membership.person)
managers_filter = (Q(is_referent=True) | Q(is_manager=True)) & Q(notifications_enabled=True)
managing_membership = membership.supportgroup.memberships.filter(managers_filter).select_related('person').prefetch_related('person__emails')
recipients = [membership.person for membership in managing_membership]
bindings = {
"GROUP_NAME": membership.supportgroup.name,
"PERSON_INFORMATION": person_information,
"MANAGE_GROUP_LINK": front_url("manage_group", kwargs={"pk": membership.supportgroup.pk})
}
send_mosaico_email(
code='GROUP_SOMEONE_JOINED_NOTIFICATION',
subject=_("Un nouveau membre dans votre groupe d'action"),
from_email=settings.EMAIL_FROM,
recipients=recipients,
bindings=bindings
)
def send_email(email_to, subject='No subject', text='', html='', tofile=None):
"""Call _send_email using the app's settings"""
_send_email(settings.EMAIL_HOST, settings.EMAIL_PORT, settings.EMAIL_FROM,
email_to, subject=subject, text=text, html=html, tofile=tofile)
def send_support_group_creation_notification(membership_pk):
try:
membership = Membership.objects.select_related('supportgroup', 'person').get(pk=membership_pk)
except Membership.DoesNotExist:
return
group = membership.supportgroup
referent = membership.person
bindings = {
"GROUP_NAME": group.name,
"CONTACT_NAME": group.contact_name,
"CONTACT_EMAIL": group.contact_email,
"CONTACT_PHONE": group.contact_phone,
"CONTACT_PHONE_VISIBILITY": _("caché") if group.contact_hide_phone else _("public"),
"LOCATION_NAME": group.location_name,
"LOCATION_ADDRESS": group.short_address,
"GROUP_LINK": front_url("view_group", kwargs={'pk': group.pk}),
"MANAGE_GROUP_LINK": front_url('manage_group', kwargs={'pk': group.pk}),
}
send_mosaico_email(
code='GROUP_CREATION',
subject=_("Les informations de votre nouveau groupe d'action"),
from_email=settings.EMAIL_FROM,
recipients=[referent],
bindings=bindings,
)
def send_support_group_changed_notification(support_group_pk, changes):
try:
group = SupportGroup.objects.get(pk=support_group_pk, published=True)
except SupportGroup.DoesNotExist:
return
change_descriptions = [desc for label, desc in CHANGE_DESCRIPTION.items() if label in changes]
change_fragment = render_to_string(
template_name='lib/list_fragment.html',
context={'items': change_descriptions}
)
# TODO: find adequate way to set up domain names to use for these links
bindings = {
"GROUP_NAME": group.name,
"GROUP_CHANGES": change_fragment,
"GROUP_LINK": front_url("view_group", kwargs={'pk': support_group_pk})
}
notifications_enabled = Q(notifications_enabled=True) & Q(person__group_notifications=True)
recipients = [membership.person
for membership in group.memberships.filter(notifications_enabled).prefetch_related('person__emails')]
send_mosaico_email(
code='GROUP_CHANGED',
subject=_("Les informations de votre groupe d'action ont été changées"),
from_email=settings.EMAIL_FROM,
recipients=recipients,
bindings=bindings,
)
def send_welcome_mail(person_pk):
person = Person.objects.prefetch_related('emails').get(pk=person_pk)
send_mosaico_email(
code='WELCOME_MESSAGE',
subject=_("Bienvenue sur la plateforme de la France insoumise"),
from_email=settings.EMAIL_FROM,
bindings={'PROFILE_LINK': front_url('change_profile')},
recipients=[person]
)
def send_unsubscribe_email(person_pk):
person = Person.objects.prefetch_related('emails').get(pk=person_pk)
bindings = {
"MANAGE_SUBSCRIPTIONS_LINK": front_url('message_preferences'),
}
send_mosaico_email(
code='UNSUBSCRIBE_CONFIRMATION',
subject=_('Vous avez été désabonné?e des emails de la France insoumise'),
from_email=settings.EMAIL_FROM,
recipients=[person],
bindings=bindings
)
def send_event_changed_notification(event_pk, changes):
try:
event = Event.objects.get(pk=event_pk)
except Event.DoesNotExist:
# event does not exist anymore ?! nothing to do
return
change_descriptions = [desc for label, desc in CHANGE_DESCRIPTION.items() if label in changes]
change_fragment = render_to_string(
template_name='lib/list_fragment.html',
context={'items': change_descriptions}
)
notifications_enabled = Q(notifications_enabled=True) & Q(person__event_notifications=True)
recipients = [rsvp.person for rsvp in event.rsvps.filter(notifications_enabled).prefetch_related('person__emails')]
bindings = {
"EVENT_NAME": event.name,
"EVENT_CHANGES": change_fragment,
"EVENT_LINK": front_url("view_event", kwargs={'pk': event_pk}),
"EVENT_QUIT_LINK": front_url("quit_event", kwargs={'pk': event_pk})
}
send_mosaico_email(
code='EVENT_CHANGED',
subject=_("Les informations d'un événement auquel vous assistez ont été changées"),
from_email=settings.EMAIL_FROM,
recipients=recipients,
bindings=bindings,
)
def send_cancellation_notification(event_pk):
try:
event = Event.objects.get(pk=event_pk)
except Event.DoesNotExist:
return
# check it is indeed cancelled
if event.published:
return
event_name = event.name
notifications_enabled = Q(notifications_enabled=True) & Q(person__event_notifications=True)
recipients = [rsvp.person for rsvp in event.rsvps.filter(notifications_enabled).prefetch_related('person__emails')]
bindings = {
"EVENT_NAME": event_name
}
send_mosaico_email(
code='EVENT_CANCELLATION',
subject=_("Un événement auquel vous participiez a été annulé"),
from_email=settings.EMAIL_FROM,
recipients=recipients,
bindings=bindings
)
def send_validation(strategy, backend, code, partial_token):
url = '{0}?verification_code={1}&partial_token={2}'.format(
reverse('social:complete', args=(backend.name,)),
code.code,
partial_token
)
url = strategy.request.build_absolute_uri(url)
send_mail('Validate your account', 'Validate your account {0}'.format(url),
settings.EMAIL_FROM, [code.email], fail_silently=False)
def send_validation(strategy, backend, code, partial_token):
url = '{0}?verification_code={1}&partial_token={2}'.format(
reverse('social:complete', args=(backend.name,)),
code.code,
partial_token
)
url = strategy.request.build_absolute_uri(url)
send_mail('Validate your account', 'Validate your account {0}'.format(url),
settings.EMAIL_FROM, [code.email], fail_silently=False)
def send_mail(email_name, subject, recipients, request=None, **kwargs):
"""
Send email.
Wraps the default Django send_mail() function. Creates email bodies from
templates. Can produce both text and HTML email bodies and send a multipart
message.
"""
# Make recipients iterable if it is not already (allow caller to pass a
# single recipient, or a list.
if isinstance(recipients, str):
recipients = (recipients,)
# A text template is required, if we can't load it, fail.
try:
text_template = select_template([
'main/email/{}.txt'.format(email_name),
'main/email/{}.text'.format(email_name),
])
except TemplateDoesNotExist:
raise ValueError('No template for email: %s' % email_name)
# An HTML template is optional.
try:
html_template = get_template('main/email/{}.html'.format(email_name))
except TemplateDoesNotExist:
html_template = None
# Produce our message body(s) from our templates using supplied context
# (if any).
message = text_template.render(context=kwargs, request=request)
if html_template:
html_message = html_template.render(context=kwargs, request=request)
else:
html_message = None
# Build the from address.
email_from = '%s <%s>' % settings.EMAIL_FROM
# Send the email using the Django send_mail() function.
_send_mail(subject, message, email_from, recipients,
html_message=html_message)
def send_mail(email_name, subject, recipients, request=None, **kwargs):
"""
Send email.
Wraps the default Django send_mail() function. Creates email bodies from
templates. Can produce both text and HTML email bodies and send a multipart
message.
"""
# Make recipients iterable if it is not already (allow caller to pass a
# single recipient, or a list.
if isinstance(recipients, str):
recipients = (recipients,)
# A text template is required, if we can't load it, fail.
try:
text_template = select_template([
'main/email/{}.txt'.format(email_name),
'main/email/{}.text'.format(email_name),
])
except TemplateDoesNotExist:
raise ValueError('No template for email: %s' % email_name)
# An HTML template is optional.
try:
html_template = get_template('main/email/{}.html'.format(email_name))
except TemplateDoesNotExist:
html_template = None
# Produce our message body(s) from our templates using supplied context
# (if any).
message = text_template.render(context=kwargs, request=request)
if html_template:
html_message = html_template.render(context=kwargs, request=request)
else:
html_message = None
# Build the from address.
email_from = '%s <%s>' % settings.EMAIL_FROM
# Send the email using the Django send_mail() function.
_send_mail(subject, message, email_from, recipients,
html_message=html_message)
def send_mail(email_name, subject, recipients, request=None, **kwargs):
"""
Send email.
Wraps the default Django send_mail() function. Creates email bodies from
templates. Can produce both text and HTML email bodies and send a multipart
message.
"""
# Make recipients iterable if it is not already (allow caller to pass a
# single recipient, or a list.
if isinstance(recipients, str):
recipients = (recipients,)
# A text template is required, if we can't load it, fail.
try:
text_template = select_template([
'main/email/{}.txt'.format(email_name),
'main/email/{}.text'.format(email_name),
])
except TemplateDoesNotExist:
raise ValueError('No template for email: %s' % email_name)
# An HTML template is optional.
try:
html_template = get_template('main/email/{}.html'.format(email_name))
except TemplateDoesNotExist:
html_template = None
# Produce our message body(s) from our templates using supplied context
# (if any).
message = text_template.render(context=kwargs, request=request)
if html_template:
html_message = html_template.render(context=kwargs, request=request)
else:
html_message = None
# Build the from address.
email_from = '%s <%s>' % settings.EMAIL_FROM
# Send the email using the Django send_mail() function.
_send_mail(subject, message, email_from, recipients,
html_message=html_message)
def send_rsvp_notification(rsvp_pk):
try:
rsvp = RSVP.objects.select_related('person', 'event').get(pk=rsvp_pk)
except RSVP.DoesNotExist:
# RSVP does not exist any more?!
return
person_information = str(rsvp.person)
recipients = [organizer_config.person for organizer_config in rsvp.event.organizer_configs.filter(notifications_enabled=True) if organizer_config.person != rsvp.person]
attendee_bindings = {
"EVENT_NAME": rsvp.event.name,
"EVENT_SCHEDULE": rsvp.event.get_display_date(),
"CONTACT_NAME": rsvp.event.contact_name,
"CONTACT_EMAIL": rsvp.event.contact_email,
"LOCATION_NAME": rsvp.event.location_name,
"LOCATION_ADDRESS": rsvp.event.short_address,
"EVENT_LINK": front_url("view_event", args=[rsvp.event.pk])
}
send_mosaico_email(
code='EVENT_RSVP_CONFIRMATION',
subject=_("Confirmation de votre participation à l'événement"),
from_email=settings.EMAIL_FROM,
recipients=[rsvp.person],
bindings=attendee_bindings
)
organizer_bindings = {
"EVENT_NAME": rsvp.event.name,
"PERSON_INFORMATION": person_information,
"MANAGE_EVENT_LINK": front_url("manage_event", kwargs={"pk": rsvp.event.pk})
}
send_mosaico_email(
code='EVENT_RSVP_NOTIFICATION',
subject=_("Un nouveau participant à l'un de vos événements"),
from_email=settings.EMAIL_FROM,
recipients=recipients,
bindings=organizer_bindings
)