def to_mail(self, user, event, locale=None, context=None, skip_queue=False): with override(locale): context = context or dict() try: subject = str(self.subject).format(**context) text = str(self.text).format(**context) except KeyError as e: raise SendMailException( f'Experienced KeyError when rendering Text: {str(e)}') mail = QueuedMail( event=self.event, to=user.email, reply_to=self.reply_to or event.email, bcc=self.bcc, subject=subject, text=text, ) if skip_queue: mail.send() else: mail.save() return mail
def to_mail( self, user, event, locale=None, context=None, skip_queue=False, commit=True, submission=None, full_submission_content=False, ): address = user.email if hasattr(user, 'email') else user with override(locale): context = context or dict() try: subject = str(self.subject).format(**context) text = str(self.text).format(**context) if submission and full_submission_content: text += '\n\n\n***********\n\n' + str(_('Full submission content:\n\n')) text += submission.get_content_for_mail() except KeyError as e: raise SendMailException(f'Experienced KeyError when rendering Text: {str(e)}') if len(subject) > 200: subject = subject[:198] + '…' mail = QueuedMail( event=self.event, to=address, reply_to=self.reply_to or event.email, bcc=self.bcc, subject=subject, text=text, ) if skip_queue: mail.send() elif commit: mail.save() return mail
def to_mail( self, user, event, locale: str = None, context: dict = None, skip_queue: bool = False, commit: bool = True, submission=None, full_submission_content: bool = False, ): """Creates a :class:`~pretalx.mail.models.QueuedMail` object from a MailTemplate. :param user: Either a :class:`~pretalx.person.models.user.User` or an email address as a string. :param event: The event to which this email belongs. May be ``None``. :param locale: The locale will be set via the event and the recipient, but can be overridden with this parameter. :param context: Context to be used when rendering the template. :param skip_queue: Send directly without saving. Use with caution, as it removes any logging and traces. :param commit: Set ``False`` to return an unsaved object. :param submission: Pass a submission if one is related to the mail. Will be used to generate context. :param full_submission_content: Attach the complete submission with all its fields to the email. """ from pretalx.person.models import User if isinstance(user, str): address = user users = None elif isinstance(user, User): address = None users = [user] else: raise Exception( 'First argument to to_mail must be a string or a User, not ' + str(type(user))) if users and (not commit or skip_queue): address = ','.join(user.email for user in users) users = None with override(locale): context = context or dict() try: subject = str(self.subject).format(**context) text = str(self.text).format(**context) if submission and full_submission_content: text += '\n\n\n***********\n\n' + str( _('Full submission content:\n\n')) text += submission.get_content_for_mail() except KeyError as e: raise SendMailException( f'Experienced KeyError when rendering Text: {str(e)}') if len(subject) > 200: subject = subject[:198] + '…' mail = QueuedMail( event=self.event, to=address, reply_to=self.reply_to, bcc=self.bcc, subject=subject, text=text, ) if skip_queue: mail.send() elif commit: mail.save() mail.to_users.set(users) return mail