Search code examples
djangodjango-viewsdjango-usersdjango-email

Django: send emails to users


I have a list of users with their email adress (only for Staff members), I am trying to send a form to the user.

When I use i.email, I get this error: "to" argument must be a list or tuple

When I use ['i.email'] I don't receive the message.

urls.py

 path('users/<int:id>/contact', views.contactUser, name='contact_user'),

views.py

def contactUser(request, id):
    i = User.objects.get(id=id)
    if request.method == 'POST':
        form = ContactUserForm(request.POST)
        if form.is_valid():
            message = form.cleaned_data['message']
            send_mail('Website administration', message, ['website@gmail.com'], ['i.email'])
            return redirect('accounts:users')
    else:
        form = ContactUserForm()

    return render(request, 'accounts/contact_user.html', {'form': form, 'username': i})

I am using SendGrid. I have a 'contact us' form which is similar to contactUser and it works fine.


Solution

  • ['i.email'] should be [i.email]