コード例 #1
0
ファイル: views.py プロジェクト: nakamura9/latrom
    def post(self, request, *args, **kwargs):
        resp = super(EmailPlusPDFView, self).post(request, *args, **kwargs)
        form = self.form_class(request.POST)

        if not form.is_valid():
            return resp

        u = UserProfile.objects.get(user=self.request.user)
        e = EmailSMTP(u)

        self.object.attachment.name = form.cleaned_data['attachment_path']
        self.object.save()

        e.send_email_with_attachment(form.cleaned_data['subject'],
                                     form.cleaned_data['to'].address,
                                     form.cleaned_data['body'],
                                     open(form.cleaned_data['attachment_path'],
                                          'rb'),
                                     html=True)

        if not self.pdf_template_name:
            raise ValueError(
                'Improperly configured. Needs pdf_template_name attribute.')

        if os.path.exists(form.cleaned_data['attachment_path']):
            os.remove(form.cleaned_data['attachment_path'])

        return resp
コード例 #2
0
def send_email(request):
    data = json.loads(request.POST['fields'])
    to = data['to'][0]
    cc = data['cc'] + data['to'][1:]
    bcc = data['bcc']
    subj = data['subject']
    config = {}
    exporter = exporterHTML(config)
    try:
        msg = exporter.render(data['msg'])
    except json.decoder.JSONDecodeError:
        raise forms.ValidationError('The message must have content')

    attachment = request.FILES.get('attachment')

    profile = models.UserProfile.objects.get(user=request.user)
    g = EmailSMTP(profile)

    if attachment:
        g.send_email_with_attachment(subj,
                                     to,
                                     cc,
                                     bcc,
                                     msg,
                                     attachment,
                                     html=True)
    else:
        g.send_html_email(subj, to, cc, bcc, msg)

    return JsonResponse({'status': 'ok'})
コード例 #3
0
def send_draft(request, pk=None):
    email = get_object_or_404(models.Email, pk=pk)
    profile = models.UserProfile.objects.get(user=request.user)

    g = EmailSMTP(profile)
    g.send_html_email(email.subject, email.to.address,
                      [i.address for i in email.copy.all()],
                      [i.address for i in email.blind_copy.all()], email.body)
    email.folder = 'sent'
    email.save()

    return JsonResponse({'status': 'ok'})
コード例 #4
0
    def _run(self):
        for user in User.objects.all():
            if not UserProfile.objects.filter(user=user).exists():
                continue

                try:
                    profile = UserProfile.objects.get(user=user)
                    client = EmailSMTP(profile)
                    client.fetch_messages()

                except:
                    logger.error(
                        'The email service failed to connect to the remote server')
コード例 #5
0
def forward_email_messages(request, pk=None):
    email = get_object_or_404(models.Email, pk=pk)
    data = json.loads(request.body.decode('utf-8'))

    to = models.EmailAddress.objects.get(pk=data['to'].split('-')[0])
    copy = [
        models.EmailAddress.objects.get(pk=addr.split('-')[0])
        for addr in data['copy']
    ]
    blind_copy = [
        models.EmailAddress.objects.get(pk=addr.split('-')[0])
        for addr in data['blind_copy']
    ]

    profile = models.UserProfile.objects.get(user=request.user)
    msg = models.Email.objects.create(to=to,
                                      sent_from=profile.address_obj,
                                      owner=profile.user,
                                      subject=email.subject,
                                      body=email.body,
                                      text=email.text,
                                      folder='sent',
                                      attachment=email.attachment)

    msg.copy.add(*copy)
    msg.blind_copy.add(*blind_copy)
    msg.save()

    g = EmailSMTP(profile)

    if (data.get('attachment', None)):  # and os.path.exists(path):
        path = os.path.join(MEDIA_ROOT, 'messaging', email.attachment.filename)

        g.send_email_with_attachment(email.subject,
                                     to.address, [i.address for i in copy],
                                     [i.address for i in blind_copy],
                                     email.body,
                                     email.attachment,
                                     html=True)
    else:
        g.send_html_email(email.subject, to.address, [i.address for i in copy],
                          [i.address for i in blind_copy], email.body)

    return JsonResponse({'status': 'ok'})
コード例 #6
0
def reply_email(request, pk=None):
    email = get_object_or_404(models.Email, pk=pk)

    profile = models.UserProfile.objects.get(user=email.owner)
    g = EmailSMTP(profile)

    #set up
    config = {}
    exporter = exporterHTML(config)
    form = forms.AxiosEmailForm(request.POST, request.FILES)

    if form.is_valid():
        body = exporter.render(json.loads(form.cleaned_data['body']))
        if form.cleaned_data['attachment']:
            g.send_email_with_attachment(email.subject,
                                         email.to.address,
                                         body,
                                         form.cleaned_data['attachment'],
                                         html=True)
        else:
            g.send_html_email(email.subject, email.to.address, body)

        msg = models.Email.objects.create(
            to=email.sent_from,
            sent_from=email.to,
            subject=email.subject,
            owner=request.user,
            attachment=form.cleaned_data['attachment'],
            sent=True,
            folder='sent',
        )
        msg.write_body(body)

        return JsonResponse({'status': 'ok'})

    else:
        return JsonResponse({'status': 'fail'})
コード例 #7
0
ファイル: cbv.py プロジェクト: adarsh115/core
    def form_valid(self, form):
        data = form.cleaned_data

        # add all to addresses to email object
        # add all address to smtp send

        to_data = self.request.POST['to']
        raw_cc_data = self.request.POST['copy']
        raw_bcc_data = self.request.POST['blind_carbon_copy']

        to = models.EmailAddress.objects.get(pk=to_data.split('-')[0])

        cc_data = json.loads(urllib.parse.unquote(raw_cc_data))
        bcc_data = json.loads(urllib.parse.unquote(raw_bcc_data))
        cc = []
        bcc = []

        for addr in cc_data:
            cc.append(models.EmailAddress.objects.get(pk=addr.split('-')[0]))

        for addr in bcc_data:
            bcc.append(models.EmailAddress.objects.get(pk=addr.split('-')[0]))

        resp = super().form_valid(form)

        self.object.to = to
        self.object.copy.add(*cc)
        self.object.blind_copy.add(*bcc)
        self.object.write_body(form.cleaned_data['body'])
        self.object.save()

        profile = models.UserProfile.objects.get(user=self.request.user)
        g = EmailSMTP(profile)

        # create local drafts folder

        # create local sent folder

        if data['save_as_draft']:
            qs = models.EmailFolder.objects.filter(owner=profile,
                                                   label='Local Drafts')
            if qs.exists():
                self.object.folder = qs.first()
            else:
                self.object.folder = models.EmailFolder.objects.create(
                    owner=profile, label='Local Drafts', name='Local Drafts')
            self.object.save()
            return resp

        if (data.get('attachment', None)):  # and os.path.exists(path):

            g.send_email_with_attachment(data['subject'],
                                         to.address, [i.address for i in cc],
                                         [i.address for i in bcc],
                                         data['body'],
                                         data['attachment'],
                                         html=True)
        else:
            g.send_html_email(data['subject'], to.address,
                              [i.address for i in cc],
                              [i.address for i in bcc], data['body'])

        self.object.save()

        return resp
コード例 #8
0
ファイル: cbv.py プロジェクト: adarsh115/core
    def form_valid(self, form):
        # slightly different from compose 'form_valid'
        data = form.cleaned_data

        to_data = self.request.POST['to']
        raw_cc_data = self.request.POST['copy']
        raw_bcc_data = self.request.POST['blind_carbon_copy']

        # string lacking a pk
        if ('-' not in to_data):
            to = models.EmailAddress.get_address(to_data)
        else:
            # in case another receiver has been chosen
            to = models.EmailAddress.objects.get(pk=to_data.split('-')[0])

        cc_data = json.loads(urllib.parse.unquote(raw_cc_data))
        bcc_data = json.loads(urllib.parse.unquote(raw_bcc_data))

        cc = []
        bcc = []
        for addr in cc_data:
            if ('-' not in addr):
                cc.append(models.EmailAddress.get_address(addr))
            else:
                address = models.EmailAddress.objects.get(
                    pk=addr.split('-')[0])
                cc.append(address)

        for addr in bcc_data:
            if ('-' not in addr):
                bcc.append(models.EmailAddress.get_address(addr))
            else:
                address = models.EmailAddress.objects.get(
                    pk=addr.split('-')[0])
                bcc.append(address)

        resp = super().form_valid(form)

        self.object.to = to
        # remove all m2m before rebuilding relations
        for a in self.object.copy.all():
            self.object.copy.remove(a)

        self.object.copy.add(*cc)

        for a in self.object.blind_copy.all():
            self.object.blind_copy.remove(a)

        self.object.blind_copy.add(*bcc)
        self.object.write_body(form.cleaned_data['body'])

        self.object.save()

        profile = models.UserProfile.objects.get(user=self.request.user)
        g = EmailSMTP(profile)

        if data['save_as_draft']:
            qs = models.EmailFolder.objects.filter(owner=profile,
                                                   label='Local Drafts')
            if qs.exists():
                self.object.folder = qs.first()
            else:
                self.object.folder = models.EmailFolder.objects.create(
                    owner=profile, label='Local Drafts', name='Local Drafts')
            self.object.save()
            return resp

        if (data.get('attachment', None)):  # and os.path.exists(path):
            path = os.path.join(MEDIA_ROOT, 'messaging',
                                data['attachment'].name)

            g.send_email_with_attachment(data['subject'],
                                         to.address, [i.address for i in cc],
                                         [i.address for i in bcc],
                                         data['body'],
                                         data['attachment'],
                                         html=True)
        else:
            g.send_html_email(data['subject'], to.address,
                              [i.address for i in cc],
                              [i.address for i in bcc], data['body'])

        return resp
コード例 #9
0
def sync_folders(request, profile_id=None):
    profile = models.UserProfile.objects.get(pk=profile_id)
    client = EmailSMTP(profile)
    messages = client.fetch_all_folders()

    return JsonResponse({'status': 'ok', 'emails': 0})