Reputation: 639
I am trying to send a test email from my Django python project and following the django docs for that. When the below function is executed I get an error (image attached). What I am doing wrong?
https://docs.djangoproject.com/en/2.0/topics/email/
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
def send_email(request):
subject = request.POST.get('subject', 'Test')
message = request.POST.get('message', 'Test Message')
from_email = request.POST.get('from_email', '[email protected]')
if subject and message and from_email:
try:
send_mail(subject, message, from_email, ['[email protected]'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return HttpResponseRedirect('/contact/thanks/')
else:
# In reality we'd use a form class
# to get proper validation errors.
return HttpResponse('Make sure all fields are entered and valid.')
Error:
Traceback: I am adding a Traceback as well to clarify my question. Hope it helps
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
574. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
1596. response = self.response_action(request, queryset=cl.get_queryset(request))
File "/home/jimtiaz/PycharmProjects/TestAoo/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in response_action
1330. response = func(self, request, queryset)
Exception Type: TypeError at /admin/home/something
Exception Value: send_email() takes 1 positional argument but 3 were given
Upvotes: 0
Views: 2027
Reputation: 1
If you are using function name and built in send_mail function is same that time you will get error just change your function name as shown below
from django.core.mail import send_mail
def send_mail_fn():
send_mail("enter parameteres here")
Upvotes: 0
Reputation: 6616
As you pointed out in your comment, this code is used for an admin action. However, in the Writing action functions section of the documentation, it's pointed out that such function should take three arguments. The given example:
def make_published(modeladmin, request, queryset):
queryset.update(status='p')
Your function takes just one argument, but gets called with three, hence the error.
Change your function definition to:
def send_email(modeladmin, request, queryset):
Upvotes: 1