def send(recipients, subject, template_name=None, context=None, content=None, template_id=None, logger=logger, sendgrid_templates=settings.SENDGRID_TEMPLATES, generic_id=generic_id): """ Send an email to an email address. If there is a SendGrid template id configured for the given template name, create substitutions from `context` so that `-key-` in the template is replaced by the value of `key` in `context`. If there is no such SendGrid template, falls back to using a Django template in <templates>/email. """ assert template_name or template_id, "missing argument" context = {} if context is None else context if not template_id: try: # TODO Use site config for template lookup template_id = sendgrid_templates[template_name] except KeyError: if generic_id: try: context = render_generic_body(template_name, context) template_id = generic_id except TemplateDoesNotExist: pass if not template_id: # Fall back to Django templates return send_template(recipients, subject, template_name, context) substitutions = {"-{}-".format(k): str(v) for k, v in context.items()} recipients = recipients if isinstance(recipients, list) else [recipients] mail = EmailMultiAlternatives( from_email=f"{settings.EMAIL_NAME} <{settings.EMAIL_ADDRESS}>", subject=subject, body="", to=recipients) mail.template_id = template_id mail.substitutions = substitutions mail.alternatives = [(" ", "text/html")] if content: mail.attach_alternative(content, "text/html") mail.send() if logger: logger.info("Email sent to %s", recipients) return True
def send(recipients, subject, template_name=None, context=None, content=None, template_id=None, logger=logger, sendgrid_templates=settings.SENDGRID_TEMPLATES, generic_id=generic_id): """ Send an email to an email address. If there is a SendGrid template id configured for the given template name, create substitutions from `context` so that `-key-` in the template is replaced by the value of `key` in `context`. If there is no such SendGrid template, falls back to using a Django template in <templates>/email. """ assert template_name or template_id, "missing argument" context = {} if context is None else context if not template_id: try: # TODO Use site config for template lookup template_id = sendgrid_templates[template_name] except KeyError: if generic_id: try: context = render_generic_body(template_name, context) template_id = generic_id except TemplateDoesNotExist: pass if not template_id: # Fall back to Django templates return send_template(recipients, subject, template_name, context) substitutions = {"-{}-".format(k): str(v) for k, v in context.items()} recipients = recipients if isinstance(recipients, list) else [recipients] mail = EmailMultiAlternatives( from_email=f"{settings.EMAIL_NAME} <{settings.EMAIL_ADDRESS}>", subject=subject, body="", to=recipients ) mail.template_id = template_id mail.substitutions = substitutions mail.alternatives = [(" ", "text/html")] if content: mail.attach_alternative(content, "text/html") mail.send() if logger: logger.info("Email sent to %s", recipients) return True
def message(self): m = EmailMultiAlternatives(self.subject, self.body) m.to = self.to m.cc = self.cc m.bcc = self.bcc m.from_email = self.from_email m.alternatives = \ [(att.content, att.mimetype) for att in self.alternatives()] for attachment in self.attachments(): m.attach(attachment.filename, attachment.content.read(), attachment.mimetype) m.extra_headers = self.extra_headers return m