Beispiel #1
0
def send_mail(subject,
              message,
              from_email,
              recipient_list,
              priority="medium",
              fail_silently=False,
              auth_user=None,
              auth_password=None,
              headers=None):
    from django.utils.encoding import force_unicode
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority,
                 headers=headers).save()
    return 1
Beispiel #2
0
def send_mail(
    subject,
    message,
    from_email,
    recipient_list,
    priority="medium",
    fail_silently=False,
    auth_user=None,
    auth_password=None,
    connection_kwargs=None,
):
    from django.utils.encoding import force_unicode
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]
    if auth_user:
        connection_kwargs["EMAIL_HOST_USER"] = auth_user
    if auth_password:
        connection_kwargs["EMAIL_HOST_PASSWORD"] = auth_password

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    make_message(
        subject=subject,
        body=message,
        from_email=from_email,
        to=recipient_list,
        priority=priority,
        connection_kwargs=connection_kwargs,
    ).save()
    return 1
Beispiel #3
0
def send_mail(subject,
              message,
              from_email,
              recipient_list,
              priority=None,
              fail_silently=False,
              auth_user=None,
              auth_password=None):
    if six.PY2:
        # Only runs Django 1.11
        from django.utils.encoding import force_unicode as force_str
    else:
        from django.utils.encoding import force_str
    from mailer.models import make_message

    priority = get_priority(priority)
    # need to do this in case subject used lazy version of ugettext
    subject = force_str(subject)
    message = force_str(message)

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority).save()
    return 1
Beispiel #4
0
def send_html_mail(subject, message, message_html, from_email, recipient_list,
                   priority=None, fail_silently=False, auth_user=None,
                   auth_password=None, headers=None, queue=0, metadata=None):
    """
    Function to queue HTML e-mails
    """
    from django.utils.encoding import force_text
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message

    priority = get_priority(priority)

    # need to do this in case subject used lazy version of ugettext
    subject = force_text(subject)
    message = force_text(message)

    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority,
                       queue=queue,
                       metadata=metadata)
    email = msg.email
    email = EmailMultiAlternatives(
        email.subject,
        email.body,
        email.from_email,
        email.to,
        headers=headers
    )
    email.attach_alternative(message_html, "text/html")
    msg.email = email
    msg.save()
    return 1
Beispiel #5
0
def send_html_mail(
    subject,
    message,
    message_html,
    from_email,
    recipient_list,
    priority="medium",
    fail_silently=False,
    auth_user=None,
    auth_password=None,
):
    """
    Function to queue HTML e-mails
    """
    from django.utils.encoding import force_unicode
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    msg = make_message(subject=subject, body=message, from_email=from_email, to=recipient_list, priority=priority)
    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
    email.attach_alternative(message_html, "text/html")
    msg.email = email
    msg.save()
    return 1
Beispiel #6
0
def send_mail(subject, message, from_email, recipient_list, priority="medium",
              fail_silently=False, auth_user=None, auth_password=None, attach_files=[]):
    from django.utils.encoding import force_unicode
    from mailer.models import make_message
    from django.core.mail.message import EmailMessage

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    msg = make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority)
    email = msg.email
    email = EmailMessage(email.subject, email.body, email.from_email, email.to)

    for f in attach_files:
        if isinstance(f, str):
            email.attach_file(f)
        elif isinstance(f, (tuple, list)):
            n, fi, mime = f + (None,) * (3 - len(f))
            email.attach(n, fi, mime)

    msg.email = email
    msg.save()
    return 1
Beispiel #7
0
def send_mail(subject, html_content, from_email, recipient_list, attachments=[], priority="medium",
              fail_silently=False, auth_user=None, auth_password=None):
    
    from django.utils.encoding import force_unicode
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message
    from django.utils.html import strip_tags
    
    priority = PRIORITY_MAPPING[priority]
    
    message = strip_tags(html_content)
    subject = force_unicode(subject)
    message = force_unicode(message)
    
    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority)
    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
    email.attach_alternative(html_content, "text/html")
    
    for att in attachments:
        email.attach(att)
    
    msg.email = email
    msg.save()
    return 1
Beispiel #8
0
def send_mail(subject, message, from_email, recipient_list, priority=None,
              fail_silently=False, auth_user=None, auth_password=None):
    from django.utils.encoding import force_text
    from mailer.models import make_message

    priority = get_priority(priority)
    # need to do this in case subject used lazy version of ugettext
    subject = force_text(subject)
    message = force_text(message)

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority).save()
    return 1
Beispiel #9
0
def send_html_mail(subject,
                   message,
                   message_html,
                   from_email,
                   recipient_list,
                   priority="medium",
                   fail_silently=False,
                   auth_user=None,
                   auth_password=None):
    """
    Function to queue HTML e-mails
    """
    from django.utils.encoding import force_unicode
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority)
    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email,
                                   email.to)
    email.attach_alternative(message_html, "text/html")
    msg.email = email
    msg.save()
    return 1
Beispiel #10
0
def send_mail(subject, message, from_email, recipient_list, priority="medium",
              fail_silently=False, auth_user=None, auth_password=None):
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = subject
    message = message

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority).save()
    return 1
Beispiel #11
0
def send_mail(subject, message, from_email, recipient_list, bcc=None, attachments=None, 
              headers=None, priority="medium", fail_silently=False, auth_user=None, auth_password=None):
    from django.utils.encoding import force_unicode
    from mailer.models import make_message
    
    priority = PRIORITY_MAPPING[priority]
    
    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)
    
    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 headers=headers,
                 priority=priority).save()
    return 1
Beispiel #12
0
def send_html_mail(subject,
                   message,
                   message_html,
                   from_email,
                   recipient_list,
                   priority=None,
                   fail_silently=False,
                   auth_user=None,
                   auth_password=None,
                   headers={},
                   attachment=None,
                   attachment_filename=None,
                   attachment_content_type=None):
    """
    Function to queue HTML e-mails
    """
    if six.PY2:
        # Only runs Django 1.11
        from django.utils.encoding import force_unicode as force_str
    else:
        from django.utils.encoding import force_str
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message

    priority = get_priority(priority)

    # need to do this in case subject used lazy version of ugettext
    subject = force_str(subject)
    message = force_str(message)

    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority)

    # Attach alternative
    email = msg.email
    email = EmailMultiAlternatives(email.subject,
                                   email.body,
                                   email.from_email,
                                   email.to,
                                   headers=headers)
    email.attach_alternative(message_html, "text/html")

    # Attach file attachment
    if attachment and attachment_filename:
        if attachment_content_type == None:
            email.attach(attachment_filename, attachment)
        else:
            email.attach(attachment_filename, attachment,
                         attachment_content_type)

    # Publish msg
    msg.email = email
    msg.save()
    return 1
Beispiel #13
0
def send_mail(subject, message, from_email, recipient_list, bcc_recipient_list,
              priority="medium", fail_silently=False, auth_user=None,
              auth_password=None):
    from django.utils.encoding import force_unicode
    from mailer import PRIORITY_MAPPING
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    subject = force_unicode(subject)
    message = force_unicode(message)

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 bcc=bcc_recipient_list,
                 priority=priority).save()
    return 1
Beispiel #14
0
 def test_save_existing_message_id(self):
     """
     Test that a preset message-id is saved correctly
     """
     with self.settings(
             MAILER_EMAIL_BACKEND="tests.TestMailerEmailBackend"):
         make_message(
             subject="Subject",
             body="Body",
             from_email="*****@*****.**",
             to=["*****@*****.**"],
             priority=PRIORITY_MEDIUM,
             headers={
                 'message-id': 'foo'
             },
         ).save()
         engine.send_all()
         m = MessageLog.objects.get()
         self.assertEqual(m.email.extra_headers['message-id'], 'foo')
         self.assertEqual(m.message_id, 'foo')
Beispiel #15
0
def send_mail(subject, message, from_email, recipient_list, priority=None,
              fail_silently=False, auth_user=None, auth_password=None, queue=0, metadata={}):
    from django.utils.encoding import force_text
    from mailer.models import make_message
    from mailer.models import Queue
    priority = get_priority(priority)
    # need to do this in case subject used lazy version of ugettext
    subject = force_text(subject)
    message = force_text(message)

    try:
        Queue.objects.get(pk=queue)
    except:
        return 0

    make_message(subject=subject,
                 body=message,
                 from_email=from_email,
                 to=recipient_list,
                 priority=priority,
                 queue=queue,
                 metadata=metadata).save()
    return 1
Beispiel #16
0
 def test_save_existing_message_id(self):
     """
     Test that a preset message-id is saved correctly
     """
     with self.settings(MAILER_EMAIL_BACKEND="mailer.tests.TestMailerEmailBackend"):
         make_message(
             subject="Subject",
             body="Body",
             from_email="*****@*****.**",
             to=["*****@*****.**"],
             priority=PRIORITY_MEDIUM,
             headers={'message-id': 'foo'},
         ).save()
         engine.send_all()
         m = MessageLog.objects.get()
         self.assertEqual(
             m.email.extra_headers['message-id'],
             'foo'
         )
         self.assertEqual(
             m.message_id,
             'foo'
         )
Beispiel #17
0
def send_EmailMultiAlternatives(email, priority=None):

    from mailer.models import make_message
    if priority is None:
        priority = get_priority(priority)

    msg = make_message(subject=email.subject,
                       body=email.body,
                       from_email=email.from_email,
                       to=email.to,
                       priority=priority)
    msg.email = email
    msg.save()

    return 1
Beispiel #18
0
def send_EmailMultiAlternatives(email, priority=None):

    from mailer.models import make_message
    if priority is None :
        priority = get_priority(priority)

    msg = make_message(
        subject =   email.subject,
        body =      email.body,
        from_email= email.from_email,
        to =        email.to,
        priority =  priority
    )
    msg.email = email
    msg.save()

    return 1
Beispiel #19
0
    def save(self, commit=True):
        instance = super(MessageForm, self).save(commit=False)
        instance = make_message(db_msg=instance,
                                from_email=self.cleaned_data['from_email'],
                                to=re.split(", *", self.cleaned_data['to']),
                                subject=self.cleaned_data['subject'],
                                body=self.cleaned_data['body'])

        body_html = self.cleaned_data['body_html']
        if body_html:
            email = instance.email
            email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
            email.attach_alternative(body_html, "text/html")
            instance.email = email

        if commit:
            instance.save()
        return instance
Beispiel #20
0
def send_attachment_mail(subject,
                         message,
                         from_email,
                         recipient_list,
                         attachment_filename,
                         attachment,
                         attachment_content_type=None,
                         priority=None,
                         fail_silently=False,
                         auth_user=None,
                         auth_password=None):
    """
    Function to queue text e-mails with attachments
    """
    from django.utils.encoding import force_text
    from mailer.models import make_message

    priority = get_priority(priority)

    # need to do this in case subject used lazy version of ugettext
    subject = force_text(subject)
    message = force_text(message)

    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority)

    # We have here the EmailMessage class
    email = msg.email
    if attachment_content_type == None:
        email.attach(attachment_filename, attachment)
    else:
        email.attach(attachment_filename, attachment, attachment_content_type)
    msg.email = email
    msg.save()
    return 1
Beispiel #21
0
def send_html_mail(subject, message, message_html, from_email, recipient_list, recipient_bcc_list,
                   priority="medium", fail_silently=False, auth_user=None,
                   auth_password=None):
    """
    Function to queue HTML e-mails
    """
    from django.utils.encoding import force_unicode
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message, Message
    
    priority = PRIORITY_MAPPING[priority]

    db_msg = Message(priority="0")
    db_msg.save()

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    # substitute in the message id for tracking
    message = message.format(id=db_msg.id)
    message_html = message_html.format(id=db_msg.id)

    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       bcc=recipient_bcc_list,
                       priority=priority)


    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to, email.bcc)
    email.attach_alternative(message_html, "text/html")
    db_msg.email = email
    db_msg.priority = priority
    db_msg.save()
    return db_msg
Beispiel #22
0
def send_html_mail_with_attachments(subject, message, message_html, from_email, recipient_list,
                   priority="medium", fail_silently=False, auth_user=None,
                   auth_password=None, attachments=[]):
    """
    Function to queue HTML e-mails
    path = list of paths to attachments
    ####
    attachments.append((filename, content, mimetype))
    """

    
    priority = PRIORITY_MAPPING[priority]
    
    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    attach_t = []
    for attachment in attachments:
        filename = basename(attachment)
        content = open(attachment, 'rb').read()
        attach_t.append((filename, content, None)) #Guess mimetype
    
    msg = make_message(subject=subject,
                       body=message,
                       from_email=from_email,
                       to=recipient_list,
                       priority=priority,
                       attachments=attach_t)

    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to, attachments=email.attachments)
    email.attach_alternative(message_html, "text/html")
    msg.email = email
    msg.save()
    return 1
Beispiel #23
0
def send_email(request, context, subject_template, body_template,
               from_email, recipients, priority="medium", reply_to=None,
               headers=None, cc=None, bcc=None):
    """
    Sends an email based on templates for subject and body.

    :param request: The current request instance.
    :param context: A dictionary of items that should be added to the
        templates' contexts.
    :param subject_template: A string representing the path to the template of
        of the email's subject.
    :param body_template: A string representing the path to the template of
        the email's body.
    :param from_email: String that represents the sender of the email.
    :param recipients: A list of tuples of recipients. The tuples are similar
        to the ADMINS setting.
    :param priority: Sets the priority of the email (only used by django-mailer
        to prioritise email sendings).
    :param reply_to: Optional email address to reply to.
    :param headers: Additional dictionary to add header attributes.
    :param cc: A list of CC recipients
    :param bcc: A list of BCC recipients
    """
    headers = headers or {}
    if not reply_to:
        reply_to = from_email

    # Add additional context
    if hasattr(settings, 'DJANGO_LIBS_EMAIL_CONTEXT'):
        context_fn = load_member_from_setting('DJANGO_LIBS_EMAIL_CONTEXT')
        context.update(context_fn(request))

    if request and request.get_host():
        domain = request.get_host()
        protocol = 'https://' if request.is_secure() else 'http://'
    else:
        domain = getattr(settings, 'DOMAIN', Site.objects.get_current().domain)
        protocol = getattr(settings, 'PROTOCOL', 'http://')
    context.update({
        'domain': domain,
        'protocol': protocol,
    })
    subject = render_to_string(template_name=subject_template,
                               context=context, request=request)
    subject = ''.join(subject.splitlines())
    message_html = render_to_string(template_name=body_template,
                                    context=context, request=request)
    message_plaintext = html_to_plain_text(message_html)
    subject = force_text(subject)
    message = force_text(message_plaintext)
    email = EmailMultiAlternatives(
        subject=subject,
        body=message,
        from_email=from_email,
        to=recipients,
        cc=cc,
        bcc=bcc,
        headers=headers,
        reply_to=[reply_to],
    )
    email.attach_alternative(message_html, "text/html")
    if settings.EMAIL_BACKEND == 'mailer.backend.DbBackend':
        # We customize `mailer.send_html_mail` to enable CC and BCC
        priority = mailer.get_priority(priority)
        msg = make_message(
            subject=subject,
            body=message,
            from_email=from_email,
            to=recipients,
            priority=priority,
        )
        msg.email = email
        msg.save()
    else:
        email.send()