Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create New Email From Django Admin #454

Open
MrAlexWinkler opened this issue Nov 5, 2023 · 3 comments
Open

Create New Email From Django Admin #454

MrAlexWinkler opened this issue Nov 5, 2023 · 3 comments

Comments

@MrAlexWinkler
Copy link

MrAlexWinkler commented Nov 5, 2023

I'd really appreciate if there was a way to create a new email (not template) from the django admin. So one could send out a new newsletter at any time. Just something super simple to not have to use code.

When trying to set this up I get error that Email model is already registered in admin:


from django import forms
from django.contrib import admin
from post_office.models import Email
from post_office.admin import EmailAdmin

class CustomEmailForm(forms.ModelForm):

    class Meta:
        model = Email
        fields = '__all__'

class CustomEmailAdmin(EmailAdmin):
    form = CustomEmailForm

admin.site.unregister(Email)
admin.site.register(Email, CustomEmailAdmin)

In essence; with django post_office how to send emails manually from admin website and not with application logic?

@MrAlexWinkler
Copy link
Author

I got it to work with code below. If this was added as part of the default package I think it would make a lot of sense.

urls.py

from emailapi.admin import send_email_view

urlpatterns = [
    ...
    path('send_email/', send_email_view, name='send_email'),
]

admin.py


from django.contrib import admin, messages
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django import forms

def send_email_view(request):
    if request.method == 'POST':
        form = EmailCreationForm(request.POST)
        if form.is_valid():
            email_data = form.cleaned_data
            mail.send(
                recipients=email_data['to'],
                sender=email_data['from_email'],
                subject=email_data['subject'],
                message=email_data['message'],
                html_message=email_data['html_message'],
                headers=email_data['headers'],
                priority=email_data['priority']
            )
            messages.success(request, 'Email sent successfully.')
            return redirect('admin:index')
    else:
        form = EmailCreationForm()
    
    return render(request, 'admin/send_email.html', {'form': form})

forms.py

from post_office.models import Email

class EmailCreationForm(forms.ModelForm):

    class Meta:
        model = Email
        fields = '__all__'  

@selwin
Copy link
Collaborator

selwin commented Nov 28, 2023

I'd welcome a PR for this :)

@MrAlexWinkler
Copy link
Author

Sounds good, I think I could get around to it before the end of the year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants