I’ve been trying to make django send a mail to one user once the user input his email but it keeps failing. For example, user A signup to my list and django sent him a mail. User B signup to my list and django sends both user A and B a mail.
I don’t want that kind of process, I want to send a mail to each user once they fill in their email address. So that when another user signup django won’t send the current user and the ones already on the database the same mail.
Below are my codes:
send invitation
Subject='Join Me'
message=loader.get_template('letter.txt')
from_email='test@testing.com'
def invite_me(request):
if request.method=="POST":
form=InviteForm(request.POST)
if form.is_valid():
form.save()
#get input data and send email to the user.
send_mail(Subject,message.render(Context()),from_email,Invite.objects.values_list('email_address', flat=True))
return HttpResponse('Thanks For Inputting Your Email, Go Check Your Inbox!')
else:
return HttpResponse('Invalid Email Address')
else:
form=InviteForm()
return render_to_response('home.html',{'InviteForm':InviteForm},context_instance=RequestContext(request))
You are using:
Invite.objects.values_list('email_address', flat=True), which returns a List of all theemail_addressfields for all theInvites in your database.This means that
allthe registeredInvite‘s will receive an email.I assume
InviteFormis aModelFormfor youInviteobject.ModelForm.savereturns the newly created object, so you should be doing:Remember that
send_mailexpects an iterable, so using a list here is required, this is achieved by using[invite.email_address]and not justinvite.email_address.