Example #1
0
    def handle(self, *args, **options):
        try:
            deliveryes = Delivery.objects.filter(delivery_test=True, send_test=False, )
        except Delivery.DoesNotExist:
            deliveryes = None
        else:
            for delivery in deliveryes:
                # print 'delivery', delivery
                try:
                    EmailMiddleDelivery.objects.\
                        get(delivery=delivery,
                            delivery_test_send=True,
                            delivery_send=False,
                            updated_at__lte=delivery.updated_at, )
                    #print aaa, delivery.updated_at
                except:
                    """ Создаем ссылочку на отсылку рассылки """
                    email_middle_delivery = EmailMiddleDelivery()
                    email_middle_delivery.delivery = delivery
                    email_middle_delivery.delivery_test_send = True
                    email_middle_delivery.delivery_send = False
                    email_middle_delivery.save()
                    """ Закрываем отсылку теста в самой рассылке """
                    delivery.send_test = True
                    delivery.save()

                    real_email = get_email(delivery=delivery, email_class=Email, pk=6, ) # pk=2836, )  # [email protected]
                    email = EmailForDelivery.objects.create(delivery=email_middle_delivery,
                                                            now_email=real_email,
                                                            email=real_email, )
                    """ Отсылаем тестовое письмо """
                    mail_account = Mail_Account(pk=1, )
                    msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, test=True, )

                    try:
                        connection = connect(mail_account=mail_account, fail_silently=False, )
                    except SMTPSenderRefused as e:
                        print 'SMTPSenderRefused: ', e
                        # email.delete()
                        # print 'SMTPSenderRefused'
                        # sleep(30, )
                        # time += 30
                    except SMTPDataError as e:
                        print 'SMTPDataError: ', e
                        # email.delete()
                        # print 'SMTPDataError'
                        # sleep(30, )
                        # time += 30
                    except Exception as e:
                        print 'Exception: ', e
                        if "(554, '5.7.1 Message rejected under suspicion of SPAM; http://help.yandex.ru/mail/spam/sending-limits.xml" in e:
                            print 'SPAM Bloked E-Mail: ', mail_account, ' NOW !!!!!!!!!!!!!!!!!!!!!!!'
                            from datetime import datetime
                            mail_account.is_auto_active = False
                            mail_account.auto_active_datetime = datetime.now()
                            mail_account.save()
                        connection = connect(mail_account=mail_account, fail_silently=True, )
                        msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, exception=e, test=True, )
                        send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )
                    else:
                        send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )

                    real_email = get_email(delivery=delivery, email_class=Email, pk=7, ) # pk=3263, )  # [email protected]
                    from apps.delivery.models import EmailForDelivery

                    email = EmailForDelivery.objects.create(delivery=email_middle_delivery,
                                                            now_email=real_email,
                                                            email=real_email, )
                    msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, test=True, )
                    from smtplib import SMTPSenderRefused, SMTPDataError
                    try:
                        connection = connect(mail_account=mail_account, fail_silently=False, )
                    except SMTPSenderRefused as e:
                        print 'SMTPSenderRefused: ', e
                    except SMTPDataError as e:
                        print 'SMTPDataError: ', e
                    except Exception as e:
                        print 'Exception: ', e
                        if "(554, '5.7.1 Message rejected under suspicion of SPAM; http://help.yandex.ru/mail/spam/sending-limits.xml" in e:
                            print 'SPAM Bloked E-Mail: ', mail_account, ' NOW !!!!!!!!!!!!!!!!!!!!!!!'
                            from datetime import datetime
                            mail_account.is_auto_active = False
                            mail_account.auto_active_datetime = datetime.now()
                            mail_account.save()
                        connection = connect(mail_account=mail_account, fail_silently=True, )
                        msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, exception=e, test=True, )
                        send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )
                    else:
                        send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )
Example #2
0
    def handle(self, *args, **options):
        from apps.delivery.models import Delivery
        try:
            deliveryes = Delivery.objects.filter(delivery_test=False,
                                                 send_test=True, send_spam=False, send_general=False,
                                                 type__in=[4, ], )

        except Delivery.DoesNotExist:
            deliveryes = None
        else:
            from apps.delivery.models import EmailMiddleDelivery
            for delivery in deliveryes:
                print delivery
                # print 'delivery', delivery
                try:
                    EmailMiddleDelivery.objects.\
                        get(delivery=delivery,
                            delivery_test_send=False,
                            spam_send=True,
                            delivery_send=False,
                            updated_at__gte=delivery.updated_at, )
                except:
                    """ Создаем ссылочку на отсылку рассылки """
                    email_middle_delivery = EmailMiddleDelivery()
                    email_middle_delivery.delivery = delivery
                    email_middle_delivery.delivery_test_send = False
                    email_middle_delivery.spam_send = True
                    email_middle_delivery.delivery_send = False
                    email_middle_delivery.save()
                    """ Закрываем отсылку теста в самой рассылке """
                    delivery.send_spam = True
                    delivery.save()

                    count_emails_try = Email.objects.filter(bad_email=False, ).count()

                    i = 0
                    time = 0
                    resolver = dns.resolver.Resolver()
                    resolver.nameservers = ['192.168.1.100', ]
                    for n in range(1, count_emails_try, ):
                        mail_account = Mail_Account()
                        email = get_email(delivery=delivery, email_class=Email, )
                        print 'n: ', n, 'in: ', count_emails_try
                        if email:
                            if not Test_Server_MX_from_email(email_string=email.email, resolver=resolver, ):
                                email.bad_email = True
                                email.save()
                            else:
                                i += 1
                                email = EmailForDelivery.objects.create(delivery=email_middle_delivery,
                                                                        # content_type=real_email.content_type,
                                                                        # object_id=real_email.pk,
                                                                        now_email=email, )
                                """ Отсылка """
                                msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, test=False, )

                                while True:
                                    try:
                                        connection = connect(mail_account=mail_account, fail_silently=False, )
                                        break
                                    except error:
                                        sleep(1); time += 1

                                try:
                                    send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )
                                except SMTPSenderRefused as e:
                                    print 'SMTPSenderRefused: ', e
                                    email.delete()
                                    sleep(30, ); time += 30
                                except SMTPDataError as e:
                                    print 'SMTPDataError: ', e
                                    print 'SMTPDataError: smtp_code', e.smtp_code
                                    print 'SMTPDataError: smtp_error', e.smtp_error
                                    print 'SMTPDataError: args', e.args
                                    print 'SMTPDataError: message', e.message
                                    if e.smtp_code == 554 and "5.7.1 Message rejected under suspicion of SPAM;" in e.smtp_error:
                                        print 'SPAM Bloked E-Mail: ', mail_account, ' NOW !!!!!!!!!!!!!!!!!!!!!!!'
                                        mail_account.is_auto_active = False
                                        mail_account.auto_active_datetime = datetime.now()
                                        mail_account.save()
                                    connection = connect(mail_account=mail_account, fail_silently=True, )
                                    msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, exception=e, test=False, )
                                    send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, execption=e, )
                                    email.delete()
                                    sleep(30, ); time += 30
                                except Exception as e:
                                    print 'Exception: ', e
                                    email.delete()
                                time = sleep_now(time=time, email=email, i=i, )

#====================== SPAM
                        email = get_email(delivery=delivery, email_class=SpamEmail, )
                        if email:
                            if not Test_Server_MX_from_email(email_string=email.email, resolver=resolver, ):
                                email.bad_email = True
                                email.save()
                            else:
                                i += 1
                                email = EmailForDelivery.objects.create(delivery=email_middle_delivery,
                                                                        # content_type=real_email.content_type,
                                                                        # object_id=real_email.pk,
                                                                        now_email=email, )
                                """ Отсылка """
                                msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, test=False, )
                                connection = connect(mail_account=mail_account, fail_silently=False, )
                                try:
                                    send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, )
                                except SMTPSenderRefused as e:
                                    print 'SMTPSenderRefused: ', e
                                    email.delete()
                                    sleep(30, ); time += 30
                                except SMTPDataError as e:
                                    print 'SMTPDataError: ', e
                                    if e.smtp_code == 554 and "5.7.1 Message rejected under suspicion of SPAM;" in e.smtp_error:
                                        print 'SPAM Bloked E-Mail: ', mail_account, ' NOW !!!!!!!!!!!!!!!!!!!!!!!'
                                        mail_account.is_auto_active = False
                                        mail_account.auto_active_datetime = datetime.now()
                                        mail_account.save()
                                    connection = connect(mail_account=mail_account, fail_silently=True, )
                                    msg = create_msg(delivery=delivery, mail_account=mail_account, email=email, exception=e, test=False, )
                                    send_msg(connection=connection, mail_account=mail_account, email=email, msg=msg, execption=e, )
                                    email.delete()
                                    sleep(30, ); time += 30
                                except Exception as e:
                                    print 'Exception: ', e
                                    email.delete()
                                time = sleep_now(time=time, email=email, i=i, )