def create_user_from_mt4demouser(mt4du):
        if (not email_re.match(mt4du.email)
                or not get_account_type(mt4du.group) or not mt4du.name):
            return None

        names = mt4du.name.split()
        try:
            new_user = register_user(
                email=mt4du.email.lower(),
                first_name=names[0],
                last_name=names[1] if len(names) > 1 else u'',
                phone_mobile=mt4du.phone,
                country=guess_country(mt4du.country),
                state=guess_state(mt4du.state, mt4du.city),
                city=mt4du.city,
                address=mt4du.address)
        #skip accounts with wrong email
        except SMTPRecipientsRefused:
            return None

        assert new_user
        #update date_joined to match mt4 account creation date
        new_user.date_joined = mt4du.regdate
        new_user.save()

        profile = new_user.profile
        profile.user_from = {'system': 'MT4'}
        profile.save()
        return new_user
Exemple #2
0
def unsubscribe(request, email, signature, campaign_id=None):
    email = urllib.unquote(email)
    if massmail.utils.get_signature(email) != signature or not email_re.match(
            email):
        raise Http404

    if campaign_id is None:
        campaign_id = request.GET.get('campaign_id', None)

    # if Unsubscribed.objects.filter(email=email).exists():
    #     # messages.success(request, _('You have already unsubscribed'))
    #     try:
    #         return redirect('massmail_unsubscribed_id', urllib.quote(email), int(campaign_id))
    #     except (ValueError, TypeError):
    #         return redirect('massmail_unsubscribed', urllib.quote(email))
    #
    # if request.method == 'POST':
    #     Unsubscribed.objects.get_or_create(email=email)
    #     try:
    #         campaign_id = request.POST.get('campaign_id', None)
    #         campaign = Campaign.objects.get(id=int(campaign_id))
    #         campaign.unsubscribed += 1
    #         campaign.save()
    #         # messages.success(request, _('Copy of this message has been sent to your email'))
    #         notification.send([email], 'unsubscribed', {"email":urllib.quote(email), "campaign_id": campaign_id})
    #         return redirect('massmail_unsubscribed_id', urllib.quote(email), campaign_id)
    #     except (ValueError, TypeError, Campaign.DoesNotExist):
    #         # messages.success(request, _('Copy of this message has been sent to your email'))
    #         notification.send([email], 'unsubscribed', {"email":urllib.quote(email), "campaign_id": campaign_id})
    #         return redirect('massmail_unsubscribed', urllib.quote(email))
    return {
        'campaign_id': campaign_id,
        'unsubscribed': Unsubscribed.objects.get_or_create(email=email)[0],
        'email': urllib.quote(email)
    }
Exemple #3
0
def unsubscribed(request, email, campaign_id=None):
    email = urllib.unquote(email)
    if email_re.match(email):
        return {
            'unsubscribed': Unsubscribed.objects.filter(email=email).exists(),
            'email': urllib.quote(email),
            'campaign_id': campaign_id
        }
    else:
        raise Http404
Exemple #4
0
def _get_massmail_params(request):
    """Get campaign and email from request"""
    email = request.GET.get('email')
    if not (email and email_re.match(email)):
        email = None
    signature = request.GET.get('signature')
    if signature != massmail.utils.get_signature(email):
        email = None
    try:
        campaign = request.GET['campaign_id']
        campaign = Campaign.objects.get(pk=int(campaign))
    except (KeyError, Campaign.DoesNotExist, ValueError):
        campaign = None
    return campaign, email
Exemple #5
0
def subscribe(request,
              email,
              signature,
              mail_list_id,
              first_name="",
              last_name="",
              phone=""):

    if massmail.utils.get_signature(email) != signature or not email_re.match(
            email):
        raise Http404

    list_id = mail_list_id.split(u'+')
    for mail_list_id in list_id:
        mail_list_id = int(mail_list_id)
        try:
            ml = MailingList.objects.get(pk=mail_list_id)
        except (ObjectDoesNotExist, ValueError):
            raise Http404

        if not request.user.is_anonymous():
            first_name = request.user.first_name
            last_name = request.user.last_name

        elif not first_name or not last_name:
            try:
                usr = User.objects.get(email=email)
                last_name = usr.last_name
                first_name = usr.first_name
            except ObjectDoesNotExist:
                # we dont know user last and first name here
                first_name = " "
                last_name = " "
        sub = Subscribed(email=email,
                         first_name=first_name,
                         last_name=last_name,
                         phone=phone)
        ml.subscribers.add(sub)
        ml.subscribers_count += 1
        ml.save()

    if not request.user.is_anonymous():
        return redirect('/account/profile/subscriptions')
    else:
        return redirect('subscribed')
Exemple #6
0
    def get_emails(self, languages=None, ignore_unsubscribed=False):
        """
        Get unique emails for this mailing list
        :param languages: list of appropriate languages to filter users by country
        :param ignore_unsubscribed: flag, if False, send unsubscribed users, if True, send.
                                    In other words, 'ignore the fact that users unsubscribed'
        :return: dict in format {email: (first_name, last_name)}, to whom we should send emails
        """
        self.languages = languages
        result = {}
        result.update(self._eval_query())
        subscribers = self.subscribers.all()
        if subscribers:
            from geobase.models import Country
            negative_countries = Country.objects.exclude(
                pk__in=self.countries.values('pk'))
            subscribers = subscribers.exclude(email__in=User.objects.filter(
                email__in=subscribers.values('email'),
                profile__country__in=negative_countries).values('email'))
        for subscriber in subscribers:  # In case of different names in profiles and subscription.
            result[subscriber.email] = (subscriber.first_name,
                                        subscriber.last_name)

        if not ignore_unsubscribed:
            for email in Unsubscribed.objects.values_list('email', flat=True):
                if email in result:
                    log.debug(
                        'MailingList pk={}, get_emails, Removed email {},'.
                        format(self.pk, email))
                    del (result[email])

        # Validate emails
        result = dict((email, data) for email, data in result.iteritems()
                      if email_re.match(email))

        return result
    def handle(self, *args, **options):
        """
        Command execution, no parameters are used.
        """
        log.info("Started unsibscribe command")
        log.debug("Logging to POP3 mailbox")
        mailbox = poplib.POP3_SSL(
            settings.MASSMAIL_UNSUBSCRIBE_MAILBOX_POP_SERVER)
        mailbox.user(settings.MASSMAIL_UNSUBSCRIBE_EMAIL)
        n = mailbox.pass_(
            settings.MASSMAIL_UNSUBSCRIBE_MAILBOX_PASSWORD).split(" ")[1]

        log.info("List-unsubscribe stats: {} messages".format(n))

        # Loop over messages
        for i in range(1, int(n) + 1):
            raw_message = mailbox.retr(i)[1]
            log.debug("Raw msg: {}".format(raw_message))

            msg = HeaderParser().parsestr("\n".join(raw_message))

            email_from = parseaddr(msg["From"])[1]
            email_to = parseaddr(msg["To"])[1]

            log.debug("{0} -> {1}".format(email_from, email_to))

            try:
                _, signature, campaign_id = (email_to.split("@")[0]).split("+")
            except ValueError:
                log.error("Wrong 'To' format: '%s'" % email_to)
                continue

            real_signature = get_email_signature(email_from)
            if real_signature != signature or not email_re.match(email_from):
                log.error("Signature fail: got '%s' instead of '%s'" %
                          (signature, real_signature))
                continue
            log.debug("Signature OK")

            obj, is_created = Unsubscribed.objects.get_or_create(
                email=email_from)

            if is_created:
                log.debug("Unsubscibed from massmail")
                try:
                    campaign = Campaign.objects.get(id=int(campaign_id))
                except (ValueError, TypeError, Campaign.DoesNotExist):
                    log.warn(
                        "Can't find compaign by id {}".format(campaign_id))
                    pass
                else:
                    log.debug("Incrementing campaign unsubscribed")
                    campaign.unsubscribed += 1
                    campaign.save()

                    log.debug("User notification about unsubscribe")
                    notification.send(
                        [email_from], 'unsubscribed', {
                            "email": urllib.quote(email_from),
                            "campaign_id": campaign_id
                        })

            log.debug("Deleting email")
            mailbox.dele(i)

        log.debug("Quiting POP3")
        mailbox.quit()
        log.info("Command finished")
Exemple #8
0
    def send(self,
             send_inactive=False,
             mail_list=None,
             global_template_context=None):
        """
        Send mass mail.
        Read the comments to understand WHAT it does.
        :param send_inactive: added for sending mail once or on cron task. False -> once, True -> cron task
        :param mail_list: local context, dict in format {email: (first_name, last_name,[per_user_template_context])}
        :param global_template_context: template.Context instance
        :return: None 
        """
        if not self.is_active and not send_inactive:  # if is_active = False and send_inactive = False, return None
            return
        # Lock through DB from sending more than one thread at a time
        if self._lock:
            return
        translation.activate(self.template.language)
        self._lock = True
        self.save()
        server = None
        try:
            # Get Django's SMTP connection
            server = get_connection()
            server.open()

            sent_emails = []

            for email, data in self._emails(mail_list).iteritems():
                # send to internal messages
                if self.send_in_private:
                    log.debug('Sending message (campaign_id %s) to %s' %
                              (self.pk, email))
                    users = User.objects.filter(email=email)
                    if users:
                        user = users[0]
                        if not user.received_messages.filter(
                                campaign=self).exists():
                            context = template.Context({
                                'first_name':
                                user.first_name,
                                'last_name':
                                user.last_name
                            })
                            subject = template.Template(
                                self.email_subject).render(context).encode(
                                    'utf-8')
                            message_private_office = Message(campaign=self,
                                                             recipient=user,
                                                             subject=subject)
                            message_private_office.save()

                            self.po_sent_count += 1
                            self.save()
                    else:
                        log.exception(
                            'Error while sending message to %s: no profile' %
                            email)
                # if we should send
                if self.send_email:

                    if not email_re.match(email):  # if email validation failed
                        log.error(
                            'Email %s does not look like an email, skipping' %
                            email)
                        continue

                    # Check twice, that we haven't sent the message yet
                    # FIXME:
                    # This creates some overhead, but it is a quick way to solve
                    # concurrency problems
                    if self.sent_messages.filter(email=email).exists():
                        continue

                    if len(data) > 2:
                        # local context is specific for this email and is set at mail_list,
                        # global context is the same for all emails and is set at send
                        first_name, last_name, per_user_template_context = data
                        # {per_user_template_context} is local_context here, template.Context instance
                    else:
                        first_name, last_name = data
                        per_user_template_context = None

                    context = self.get_context(first_name, last_name, email)

                    if global_template_context is not None:  # template.Context instance
                        context.update(global_template_context)
                    if per_user_template_context is not None:
                        context.update(per_user_template_context)

                    # Make separate contexts for html and text versions
                    context_text, context_html = self._get_block_context(
                        context)

                    if self.custom_email_from:
                        email_from = "%s <%s>" % (self.custom_email_from_name,
                                                  self.custom_email_from)
                        reply_to = self.custom_email_from
                    else:
                        email_from = settings.MASSMAIL_DEFAULT_FROM_EMAIL
                        reply_to = settings.MASSMAIL_DEFAULT_REPLY_TO

                    # Create a proper MIME/Multipart email and send it
                    msg = self.template.create_email(
                        context,
                        context_text=context_text,
                        context_html=context_html,
                        subject=self.email_subject,
                        html_processor=lambda text: self.process_html(
                            text, email=email),
                        email_to=email,
                        email_from=email_from,
                        reply_to=reply_to,
                        connection=server,
                        msgtype='massmail_campaign_{}'.format(self.pk),
                    )

                    log.debug('Sending email (campaign_id %s) to %s' %
                              (self.pk, email))
                    try:
                        msg.send()  # Django builtin method to send emails
                    except smtplib.SMTPRecipientsRefused:
                        log.exception('Error while sending to email %s' %
                                      email)
                        continue
                    sent_emails.append(email)
                    SentMessage.objects.create(campaign=self, email=email)

            self.is_active = False
            self.is_sent = True
        finally:
            if server is not None:
                server.close()
            self._lock = False
            self.save()