Exemple #1
0
def _send_messages(email_messages):
    connection = _get_real_backend()

    # Create a messages on a database for correct
    # tracking of their status.
    email_models = [models.Message.from_email_message(email, save=True)
                    for email in email_messages]

    # Open connection for send all messages
    connection.open()
    sended_counter = 0

    for email, model_instance in zip(email_messages, email_models):
        if hasattr(email, "priority"):
            if email.priority <= models.PRIORITY_LOW:
                model_instance.priority = email.priority
                model_instance.status = models.STATUS_PENDING
                model_instance.save()

                continue

        sended = connection.send_messages([email])

        if sended == 1:
            sended_counter += 1
            model_instance.status = models.STATUS_SENT
            model_instance.sent_at = timezone.now()
        else:
            model_instance.status = models.STATUS_FAILED

        model_instance.save()

    connection.close()
    return sended_counter
Exemple #2
0
def _retry_send_messages():
    """
    Function that retry send failed messages.
    """

    max_retry_value = getattr(settings, "DJMAIL_MAX_RETRY_NUMBER", 3)
    queryset = models.Message.objects.filter(status=models.STATUS_FAILED)\
                        .filter(retry_count__lte=max_retry_value)\
                        .order_by("-priority", "created_at")

    connection = _get_real_backend()
    paginator = Paginator(list(queryset), getattr(settings, "DJMAIL_MAX_BULK_RETRY_SEND", 10))

    for page_index in paginator.page_range:
        connection.open()
        for message_model in paginator.page(page_index).object_list:
            email = message_model.get_email_message()
            sended = connection.send_messages([email])

            if sended == 1:
                message_model.status = models.STATUS_SENT
                message_model.sent_at = timezone.now()
            else:
                message_model.retry_count += 1

            message_model.save()

        connection.close()
def send_pending(request):
    potential_profits = RawLeads.objects.filter(activated__gte=1, mail__isnull=False, reminder=0)

    connection = mail.get_connection()
    connection.open()
    asdi = 0
    for potential_profit in potential_profits:
        hash_base_id = potential_profit.hash_base_id
        try:
            iterator = randint(0, 3)
            link = ('http://www.' + str(hosts[iterator]) + '/offer/?id=' + str(hash_base_id))
            unsubscribe = ('http://www.' + str(hosts[iterator]) + '/unsubscribe/?id=' + str(hash_base_id))
            # case = randint(1, 10)
            case = 1
            msg = eval('form_a_msg' + str(case) + '("' + str(potential_profit.name_redemption) + '","' + str(
                link) + '","' + str(unsubscribe) + '")')

            req = requests.post(
                "http://www.webdomainexpert.pw/add_offer/",
                data={
                    'base_id': potential_profit.id,
                    'drop': potential_profit.name_redemption,
                    'lead': potential_profit.name_zone,
                    'hash_base_id': hash_base_id,
                    'remail': potential_profit.mail,
                }
            )
            while req.status_code == 204:
                hash_base_id = binascii.hexlify(os.urandom(16))

                req = requests.post(
                    "http://www.webdomainexpert.pw/add_offer/",
                    data={
                        'base_id': potential_profit.id,
                        'drop': potential_profit.name_redemption,
                        'lead': potential_profit.name_zone,
                        'hash_base_id': hash_base_id,
                        'remail': potential_profit.mail,
                    }
                )

            if req.status_code == 200:
                    # AllHash.objects.filter(hash_base_id=potential_profit.hash_base_id).update(hash_base_id=hash_base_id)
                als = AllHash.objects.filter(hash_base_id=potential_profit.hash_base_id)
                for al in als:
                    al.hash_base_id = hash_base_id
                    al.save()
                    # RawLeads.objects.filter(id=potential_profit.id).update(reminder=1, hash_base_id=hash_base_id, last_email_date=timezone.now())
                rls = RawLeads.objects.filter(id=potential_profit.id)
                for rl in rls:
                    rl.reminder = 1
                    rl.hash_base_id = hash_base_id
                    rl.last_email_date = timezone.now()
                    rl.save()

                emails = []
                email = mail.EmailMultiAlternatives(
                    msg[0],
                    'potential_profit.name_zone',
                    'Web Domain Expert <' + str(settings.EMAIL_HOST_USER) + '>',
                    [potential_profit.mail],
                    reply_to=("*****@*****.**", ),
                    bcc=["*****@*****.**"],
                )
                email.attach_alternative(msg[1], "text/html")
                emails.append(email)
                try:
                    connection.send_messages(emails)
                    asdi += 1

                    if not Log.objects.filter(date=potential_profit.date).exists():
                        Log(date=potential_profit.date).save()
                    number_of_old_2 = Log.objects.get(date=potential_profit.date).number_sent_2
                    # Log.objects.filter(date=potential_profit.date).update(number_sent_2=(1 + int(number_of_old_2)))
                    ls = Log.objects.filter(date=potential_profit.date)
                    for l in ls:
                        l.number_sent_2 = (1 + int(number_of_old_2))
                        l.save()

                except SMTPServerDisconnected:
                    connection = mail.get_connection()
                    connection.open()
                    connection.send_messages(emails)
        except:
            return HttpResponse(traceback.format_exc(), content_type='application/json')
    connection.close()

    if not Log.objects.filter(date=datetime.now().date()).exists():
        Log().save()
    number_of_old = Log.objects.get(date=datetime.now().date()).number_sent
    # Log.objects.filter(date=datetime.now().date()).update(number_sent=(int(asdi) + int(number_of_old)))
    ls = Log.objects.filter(date=datetime.now().date())
    for l in ls:
        l.number_sent = (int(asdi) + int(number_of_old))
        l.save()

    return HttpResponse('{"status": "success"}', content_type="application/json")