我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.core.mail.mail_admins()。
def form_valid(self, form): ''' If the form is valid send email notifications to the site administrators. We don't return the super().form_valid because we don't want the data to be saved. ''' subject = 'Correction submitted for exercise #{0}'.format(self.get_object().pk) context = { 'exercise': self.get_object(), 'form_data': form.cleaned_data, 'user': self.request.user } message = render_to_string('exercise/email_correction.tpl', context) mail.mail_admins(six.text_type(subject), six.text_type(message), fail_silently=True) messages.success(self.request, self.messages) return HttpResponseRedirect(reverse('exercise:exercise:view', kwargs={'id': self.object.id}))
def set_author(self, request): ''' Set author and status This is only used when creating exercises (via web or API) ''' if request.user.has_perm('exercises.add_exercise'): self.status = self.STATUS_ACCEPTED if not self.license_author: self.license_author = request.get_host().split(':')[0] else: if not self.license_author: self.license_author = request.user.username subject = _('New user submitted exercise') message = _(u'The user {0} submitted a new exercise "{1}".').format( request.user.username, self.name) mail.mail_admins(six.text_type(subject), six.text_type(message), fail_silently=True)
def set_author(self, request): ''' Set author and status This is only used when creating images (via web or API) ''' if request.user.has_perm('exercises.add_exerciseimage'): self.status = self.STATUS_ACCEPTED if not self.license_author: self.license_author = request.get_host().split(':')[0] else: if not self.license_author: self.license_author = request.user.username subject = _('New user submitted image') message = _(u'The user {0} submitted a new image "{1}" for exercise {2}.').format( request.user.username, self.name, self.exercise) mail.mail_admins(six.text_type(subject), six.text_type(message), fail_silently=True)
def form_valid(self, form): # set the submitter, if admin, set approrpiate status form.instance.user = self.request.user if self.request.user.has_perm('nutrition.add_ingredient'): form.instance.status = Ingredient.INGREDIENT_STATUS_ADMIN else: subject = _('New user submitted ingredient') message = _(u'''The user {0} submitted a new ingredient "{1}".'''.format( self.request.user.username, form.instance.name)) mail.mail_admins(subject, message, fail_silently=True) form.instance.language = load_language() return super(IngredientCreateView, self).form_valid(form)
def send_mail(self, subject, message, *args, **kwargs): mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
def mail_admins(subject, message, *args, **kwargs): # noqa """ Wraps the mail_admins function from Django to wrap long lines in emails. The exim mail server used in EDD dis-allows lines longer than 998 bytes. """ wrapper = TextWrapper(width=79, break_on_hyphens=False, replace_whitespace=False, subsequent_indent=' ') message = '\n'.join(chain(*[wrapper.wrap(line) for line in message.splitlines()])) _mail_admins(subject, message, *args, **kwargs)
def form_valid(self, form): ''' Send the feedback to the administrators ''' messages.success(self.request, _('Your feedback was successfully sent. Thank you!')) context = {} context['user'] = self.request.user context['form_data'] = form.cleaned_data subject = 'New feedback' message = render_to_string('user/email_feedback.html', context) mail.mail_admins(subject, message) return super(FeedbackClass, self).form_valid(form)