Ive built an contact form which sends an email. I'm just having a bit of trouble in relation to the account its being sent to. I want the email to be sent from "servwishes#gmail.com" to the "Contact_Email".
Right now the email is going from "Contact_Email" to "servwishes#gmail.com".
my views.py looks like this:
def contact(request):
Contact_Form = ContactForm
if request.method == 'POST':
form = Contact_Form(data=request.POST)
if form.is_valid():
contact_name = request.POST.get('contact_name')
contact_email = request.POST.get('contact_email')
contact_content = request.POST.get('content')
template = get_template('users/contact_form.txt')
context = {
'contact_name' : contact_name,
'contact_email' : contact_email,
'contact_content' : contact_content,
}
content = template.render(context)
email = EmailMessage(
"New contact form email",
content,
"Creative web" + '',
['servwishes#gmail.com'],
headers = { 'Reply To': contact_email }
)
email.send()
return render(request, 'users/contact.html', {'form':Contact_Form })
And my setting.py looks like:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'servwishes#gmail.com'
EMAIL_HOST_PASSWORD = '*******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
If you look at the order of your arguments and convert them from positional to keyword, you currently have:
email = EmailMessage(
subject="New contact form email",
body=content,
from_email="Creative web" + '',
to=['servwishes#gmail.com'],
headers = { 'Reply To': contact_email }
)
I think there were a couple of issues here. I think you probably meant to do:
from_email='"Creative web" <servwishes#gmail.com>'
But since you didn't get that, it messed up the order of your positional arguments.
To should be to=contact_email
The other issue is I think you are misunderstanding the 'Reply To' header. That's who, when the recipient hits the reply button, the email will be sent back to. It is not who you are sending the email to.
Related
Tests fail with an error response meaning that it is likely to be allowing email with wrong data and yet it should throw an HttpResponse as expected, I have tried to figure it out why my test is failing and returning 200 http status code but not as expected = 400.
reset password
class ResetPassword(View):
form_class = ForgetPasswordForm()
template_name = 'authentication/password_reset.html'
def get(self, request):
form = self.form_class
return render(request, self.template_name, {'form': form})
def post(self, request):
msg = None
form = ForgetPasswordForm(request.POST)
if form.is_valid():
data = form.cleaned_data.get('email')
associated_users = Users.objects.filter(Q(email = data))
if associated_users.exists():
for user in associated_users:
subject = 'Password Reset Requested'
email_template_name = "authentication/password_reset_email.txt"
c = {
"email": user.email,
'domain': '127.0.0.1:8000',
'site_name': 'ATB Project Organizer',
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'user': user,
'token': default_token_generator.make_token(user),
'protocol': 'http',
}
email = render_to_string(email_template_name, c)
try:
send_mail(subject, email, 'admin#example.com', [user.email], fail_silently=False)
except BadHeaderError:
return HttpResponse('Invalid header found')
msg = 'You have successfully reset your password!'
return redirect('/password_reset/done/')
else:
msg = 'No records found for this email, please make sure you have entered the correct email address!'
form = ForgetPasswordForm()
return render(request, 'authentication/password_reset.html', {'form': form, 'msg': msg})
Test to raise an exception
from django.test import TestCase
from django.urls import reverse
from django.core import mail
from django.contrib.auth import get_user_model
User = get_user_model()
class PasswordResetTest(TestCase):
def setUp(self):
self.user1 = User.objects.create_user("user1", email = "user1#mail.com", password = "password#121", orcid = '1234567890987654')
self.user2 = User.objects.create_user("user2", email = "user2#mail.com", password = "password#122", orcid = '1234567890987655')
self.user3 = User.objects.create_user("user3#mail.com", email = "not-that-mail#mail.com", password = "password#123", orcid = '1234567890987656')
self.user4 = User.objects.create_user("user4", email = "user4#mail.com", password = "passs", orcid = '1234567890987657')
self.user5 = User.objects.create_user("user5", email = "uѕer5#mail.com", password = "password#125", orcid = '1234567890987658') # email contains kyrillic s
self.user={
'username':'username',
'email':'testemail#gmail.com',
'password1':'password#123',
'password2':'password#123',
'orcid': '1234123412341239',
}
def test_user_can_resetpassword(self):
response = self.client.get(reverse('resetpassword'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'authentication/password_reset.html')
# Then test that the user doesn't have an "email address" and so is not registered
response = self.client.post(reverse('resetpassword'), {'email': 'admin#example.com'}, follow=True)
self.assertEqual(response.status_code, 200)
# Then test that the user doesn't have an "email address" and so is not registered
# Then post the response with our "email address"
# Then post the response with our "email address"
response = self.client.post(reverse('resetpassword'),{'email':'user1#mail.com'})
self.assertEqual(response.status_code, 302)
# At this point the system will "send" us an email. We can "check" it thusly:
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Password Reset Requested')
def test_exception_raised(self):
# verify the email with wrong data
data = {"uid": "wrong-uid", "token": "wrong-token"}
response = self.client.post(reverse('resetpassword'), data, format='text/html')
self.assertEqual(response.status_code, 400)
Error
File "../tests/test_passwordreset.py", line 55, in test_exception_raised
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
AssertionError: 200 != 400
Failing code
The default HTTP status code when you instantiate the HttpResponse class is 200 (OK). This is why your test fails.
Try this:
...
except BadHeaderError:
return HttpResponse('Invalid header found', status=400)
# Or more verbose:
# return HttpResponse('Invalid header found', status=HttpStatus.BAD_REQUEST)
...
or
...
except BadHeaderError:
return HttpResponseBadRequest('Invalid header found')
...
See the docs for more details.
Anyone know how to solved my issue, Im working with DJango-email with multiple recipient. Sending email in multiple recipient accounts from my DB are working, but now I want to send email and the email:body are depending on the Data ID.
This are the email list,
Scenario: Plate_No: 123123 will be send to example_email1#gmail.com only and ABV112 will be send again to example_email2#gmail.com and so on. Only the Plate_no assign in email will send, can someone help me to work my problem. Thank you!
auto send email script:
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(), sent_email="No")
print(cstatus)
recipient_list = []
for recipient in cstatus:
recipient_list.append(recipient.email)
print(recipient_list)
plate = ""
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html', {'content':cstatus})
plain_message = strip_tags(html_message)
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, recipient_list, html_message=html_message, fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
You can use a for-loop on your cstatus queryset to send the emails to the recipents. Did not test it, but it should look something like this:
for item in cstatus:
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html'{'content':item.Plate_no})
plain_message = item.Plate_no
recipent_list = [item.email]
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, recipient_list, html_message=html_message, fail_silently=False)
item.update(sent_email="Yes")
According to what I understood regarding your query, this might what you need:
class HomeView(ListView):
cstatus = VR.objects.filter(Deadline__date = datetime.datetime.today(), sent_email="No")
print(cstatus)
recipient_list = {}
for recipient in cstatus:
recipient_list[recipient.plate_no] = recipient.email
print(recipient_list)
for carreg in cstatus:
print(carreg.plate_no)
plate = carreg.plate_no
if plate != "":
subject = 'FMS Automated Email'
html_message = render_to_string('vr/pms_email.html', {'content':carreg}) # or use plate for just plate_no
plain_message = strip_tags(html_message)
from_email = 'FMS <fms#gmail.com>'
mail.send_mail(subject, plain_message, from_email, [recipient_list[plate]], html_message=html_message, fail_silently=False)
cstatus.update(sent_email="Yes")
model = VR
context_object_name = 'list'
template_name = 'vr/list.html'
Or Use mass emailing in django:
link: https://docs.djangoproject.com/en/1.8/topics/email/#send-mass-mail
message1 = ('Subject here', 'Here is the message', 'from#example.com', ['first#example.com', 'other#example.com'])
message2 = ('Another Subject', 'Here is another message', 'from#example.com', ['second#test.com'])
send_mass_mail((message1, message2), fail_silently=False)
Add all the above message results in a tuple and add it in send_mass_mail. For eg.
datatuple = (
(subject, plain_message, from_email, to_email),
(subject, plain_message, from_email, to_email)
) # to_mail -> recipient_list[plate]
send_mass_mail(datatuple)
Let me know if I was wrong.
I'm generating token while user verified OTP. but while i verify the token in header then I'm getting Invalid payload.
if any help for why i'm getting this error then would be much appreciated.
serializers.py :
class OTPVerifyForResetPasswordAPIView(APIView):
permission_classes = (AllowAny,)
def post(self,request,*args,**kwargs):
data = request.data
user = request.user
print(user)
phone_number = request.data['phone_number']
country_code = request.data['country_code']
verification_code = request.data['verification_code']
if phone_number and country_code and verification_code:
obj_qs = User.objects.filter(phone_number__iexact = phone_number,country_code__iexact = country_code)
obj = ''
if obj_qs.exists() and obj_qs.count() ==1:
user_obj = obj_qs.first()
#Development
if verification_code == '1234':
payload = jwt_payload_handler(user_obj)
token = jwt_encode_handler(payload)
token = 'JWT '+str(token)
return Response({
'success' : 'True',
'message' : 'Your mobile number verified successfully',
'data' : {
'phone_number' : user_obj.phone_number,
'country_code' : user_obj.country_code,
'token' : token,
}
},status=HTTP_200_OK)
else:
##some logic....
else:
## some logic...
I see an open issue in django-rest-framework-jwt about a similar problem: https://github.com/jpadilla/django-rest-framework-jwt/issues/284
It looks like this library isn't maintained for a few years already, I'd probably suggest you switching to some alternative, they suggest using this one: https://github.com/SimpleJWT/django-rest-framework-simplejwt
Edit the line with user_obj = obj_qs.first() to be like
user_obj = {
'username': obj_qs.first().username,
...
}
From the below code, I am sending mail with some info.Here i need to get the message to my mail id,When he/she opens the mail.
How to do this in Python.
def Data(request):
templateName = "sendmail.html"
mapDictionary = {'fromMail': "xxxx.xxxx#gmail.com", 'password': "xxxxx", 'toMail': "yyyyy.yyyy#gmail.com#gmail.com",
"subject": "New Trip Confirmation", 'username': 'Ramesh','trip_start_date' : '2014-02-10',
'trip_start_place' : 'Visak', 'trip_start_time' : '11:00 AM', "templateName" : templateName
}
print ("call send mail from processNewTripData...")
return sendmail(request, mapDictionary)
def sendmail(request, mapDictionary):
try:
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(mapDictionary["fromMail"],mapDictionary["password"])
context = Context(mapDictionary)
html_content = render_to_string(mapDictionary["templateName"], context)
#text_content = "This is Confirmation mail"
msg = MIMEText(html_content,'html')
msg['Subject'] = mapDictionary["subject"]
server.sendmail(mapDictionary["fromMail"], mapDictionary['toMail'], msg.as_string())
to_json = {'result' : "True"}
return HttpResponse(simplejson.dumps(to_json), content_type='application/json')
except Exception as e:
print str(e)
to_json = {'result' : "False"}
return HttpResponse(simplejson.dumps(to_json), content_type='application/json')
Have a try and add this header:
Disposition-Notification-To: "User" <user#user.com>
The reader may need to confirm that you get a reply. Also adding html content served by your server can be an option to recognize that the mail is read.
You should be able to do this with any of these lines
msg['Disposition-Notification-To'] = '"User" <user#user.com>'
msg['Disposition-Notification-To'] = 'user#user.com'
I wrote a simple contact form for a client in Django. However, whenever it sends e-mail, it wraps the header values in u'' objects. For example, the From: header is
From: (u'my#email.com',)
Here's the code that sends the message:
The form:
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
sender = forms.EmailField()
subject = forms.CharField(max_length=255)
message = forms.CharField(widget=forms.widgets.Textarea(attrs={'rows':15, 'cols': 72}))
The contact function:
def contact(request):
RECAPTCHA_PRIVATE_KEY = '******************'
captcha_error = ''
if request.method == 'POST':
form = ContactForm(request.POST)
captcha_response = captcha.submit(request.POST.get("recaptcha_challenge_field", None),
request.POST.get("recaptcha_response_field", None),
RECAPTCHA_PRIVATE_KEY,
request.META.get("REMOTE_ADDR", None))
if not captcha_response.is_valid:
captcha_error = "&error=%s" % captcha_response.error_code
elif form.is_valid():
name = form.cleaned_data['name'],
sender = form.cleaned_data['sender'],
subject = form.cleaned_data['subject'],
message = form.cleaned_data['message']
recipients = ['email#email.com']
try:
send_mail(subject, message, sender, recipients)
except BadHeaderError:
pass
flash_message = 'Thank you for contacting us. We will get back to you shortly.'
return render_to_response('pages/contact.html', {
'form': form,
'captcha_error': captcha_error,
'message': flash_message
})
It sends the e-mail perfectly, I check the appropriate mailbox and the e-mail appears. But these u'' objects prevent the e-mail's subject from appearing correctly and prevents it from being replied to.
What am I doing wrong?
Thanks in advance.
Lose the trailing commas here:
elif form.is_valid():
name = form.cleaned_data['name']
sender = form.cleaned_data['sender']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']