def hernya2(): try: deliveryes = Delivery.objects.filter(delivery_test=False, ) except Delivery.DoesNotExist: deliveryes = None else: for delivery in deliveryes: try: aaa=EmailMiddleDelivery.objects.\ get(delivery=delivery, 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 = False email_middle_delivery.delivery_send = True email_middle_delivery.save() from django.utils.html import strip_tags from django.core.mail import get_connection backend = get_connection(backend='django.core.mail.backends.smtp.EmailBackend', fail_silently=False, ) from django.core.mail import EmailMultiAlternatives from proj.settings import Email_MANAGER msg = EmailMultiAlternatives(subject=delivery.subject, body=strip_tags(delivery.html, ), from_email=u'*****@*****.**', to=[real_email.email, ], connection=backend, ) msg.attach_alternative(content=delivery.html, mimetype="text/html", ) msg.content_subtype = "html" print real_email.email
def handle(self, *args, **options): from apps.delivery.models import Delivery try: deliveryes = Delivery.objects.filter(delivery_test=False, send_test=True, send_general=False, type__in=[1, 2, 3, ], ) except Delivery.DoesNotExist: deliveryes = None else: from apps.delivery.models import EmailMiddleDelivery for delivery in deliveryes: # print 'delivery', delivery try: EmailMiddleDelivery.objects.\ get(delivery=delivery, send_test=False, send_general=True, updated_at__lte=delivery.updated_at, ) except: """ Создаем ссылочку на отсылку рассылки """ email_middle_delivery = EmailMiddleDelivery() email_middle_delivery.delivery = delivery email_middle_delivery.delivery_test_send = False email_middle_delivery.delivery_send = True email_middle_delivery.save() """ Закрываем отсылку теста в самой рассылке """ delivery.send_general = True delivery.save() """ Отсылаем тестовое письмо """ from django.utils.html import strip_tags EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.yandex.ru' EMAIL_PORT = 587 EMAIL_HOST_USER = '******' EMAIL_HOST_PASSWORD = '******' from django.core.mail import get_connection backend = get_connection(backend='django.core.mail.backends.smtp.EmailBackend', host=EMAIL_HOST, port=EMAIL_PORT, username=EMAIL_HOST_USER, password=EMAIL_HOST_PASSWORD, use_tls=EMAIL_USE_TLS, fail_silently=False, ) from django.core.mail import EmailMultiAlternatives from proj.settings import Email_MANAGER from apps.authModel.models import Email """ Создаем указатели на E-Mail адреса рассылки """ try: emails = Email.objects.filter(bad_email=False, ) except Email.DoesNotExist: emails = None """ Здесь нужно помудрить с коммитом """ from apps.delivery.models import EmailForDelivery from apps.delivery.utils import parsing i = 0 time = 0 for real_email in emails: i += 1 # if i < 125: # continue email = EmailForDelivery.objects.create(delivery=email_middle_delivery, email=real_email, ) """ Отсылка """ msg = EmailMultiAlternatives(subject=delivery.subject, body=strip_tags(parsing(value=delivery.html, key=email.key, ), ), from_email='*****@*****.**', to=[real_email.email, ], connection=backend, ) msg.attach_alternative(content=parsing(value=delivery.html, key=email.key, ), mimetype="text/html", ) msg.content_subtype = "html" try: msg.send(fail_silently=False, ) except Exception as e: msg = EmailMultiAlternatives(subject='Error for subject: %s' % delivery.subject, body='Error: %s - E-Mail: %s - real_email.pk: %d' % (e, real_email.email, real_email.pk, ), from_email='*****@*****.**', to=['*****@*****.**', ], connection=backend, ) msg.send(fail_silently=True, ) else: print 'i: ', i, 'Pk: ', real_email.pk, ' - ', real_email.email from random import randrange time1 = randrange(6, 12, ) time2 = randrange(6, 12, ) time += time1 + time2 print 'Time1: ', time1, ' Time2: ', time2, ' Time all: ', time1+time2, ' average time: ', time/i from time import sleep sleep(time1, ) print 'Next' sleep(time2, )
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, )
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, )