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_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
def send_emails(idsstr,request): ids=idsstr.split(",") connection= mail.get_connection("oauthemail.smtp.OauthEmailBackend", user=request.user) connection.open() count=0 for id in ids: ui=DirUpdate.objects.get(id=id); if ui.invite_state==DirUpdate.ACTIVE: email_content=get_email_content(ui,request) subject,to,cc,bcc,content=parse_email(email_content) to_email= to if to else ui.invite_email try: result=mail.EmailMessage(subject, content, to=to_email.split(';'), cc=[cc]if cc else [], bcc=[bcc]if bcc else [], connection=connection).send() ui.invite_state=DirUpdate.SENT ui.comment=repr(result) ui.save() count+=1 except SMTPException as e: ui.invite_state=DirUpdate.FAILED ui.comment=repr(e) ui.save() connection.close() return HttpResponse("stopped with error {0}".format(e)) connection.close() return HttpResponse("{0} email sent.".format(count))
def message(request, ppant_id, html, subject, message): p = Participant.objects.get(pk=ppant_id) connection = get_connection() connection.open() if html == '1': msg = open('media/template.html','r') message = msg.read() msg.close() if "**name**" in message: message = message.replace("**name**", p.fname + ' ' + p.sname) email = EmailMessage(subject, message, '*****@*****.**', [p.email]) email.content_subtype = 'html' email.send() connection.close() return HttpResponse('sent')
def check_email_server_is_alive(app_configs=None, **kwargs): from django.conf import settings errors = [] if settings.POOTLE_SIGNUP_ENABLED or settings.POOTLE_CONTACT_ENABLED: from django.core.mail import get_connection connection = get_connection() try: connection.open() except Exception: errors.append(checks.Warning( _("Email server is not available."), hint=_("Review your email settings and make sure your email " "server is working."), id="pootle.W004", )) else: connection.close() return errors
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")