def queue_mail_template(content_object, template_slug, context): """Queue a mail message. The message will be rendered using the template. When the mail is sent, the template will be found and rendered using Django, Mandrill or Sparkpost. The context is a dict containing email addresses and optionally a key, value dict for each email address. """ template = get_mail_template(template_slug) message = Message(**dict( content_object=content_object, subject=template.subject, template=template, )) message.save() for email in context.keys(): mail = Mail(**dict( email=email, message=message, )) mail.save() email_data = context[email] if email_data: for key in email_data.keys(): value = email_data.get(key, None) if value: mf = MailField(**dict(mail=mail, key=key, value=value)) mf.save() return message
def send(**kwargs): mail = Message(sender_name=kwargs.get('sender')[0], sender_email=kwargs.get('sender')[1], recipient_name=kwargs.get('recipient')[0], recipient_email=kwargs.get('recipient')[1], subject=kwargs.get('subject'), message=kwargs.get('body')) return mail.save()
def queue_mail_message( content_object, email_addresses, subject, description, is_html=False, attachments=None): """queue a mail message for one or more email addresses. The subject and description are fully formed i.e. this function does not do any templating. """ if not attachments: attachments = [] if not email_addresses: raise MailError( "Cannot 'queue_mail_message' without " "'email_addresses': '{}'".format(subject) ) message = Message(**dict( content_object=content_object, subject=subject, description=description, is_html=is_html, )) message.save() for email in email_addresses: mail = Mail(**dict( email=email, message=message, )) mail.save() for file_name in attachments: attachment = Attachment(message=message) with open(file_name, 'rb') as f: django_file = File(f) base_name = os.path.basename(file_name) attachment.document.save(base_name, django_file, save=True) return message