اواجهة مشكلة عدم ارسال ايميل من الاستضافة و في localhost يقوم بالارسال بدون اي مشكلة
views.py
تحتوي على ثنين من form و اضفت لهم captcha ... انا قمت بازالة المفتاح السري للcaptcha
def home(request):if request.method =='POST':
form_type = request.POST.get('form_type','')# Common reCAPTCHA validation for both forms
recaptcha_response = request.POST.get('g-recaptcha-response')
data ={'secret':'**********************','response': recaptcha_response
}
r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
result = r.json()if result['success']:if form_type =='contact_form':# Fetch data from Contact Us page
name = request.POST.get('name','')
email = request.POST.get('email','')
subject = request.POST.get('subject','')
message = request.POST.get('message','')# Save in database
data =Contact(name=name, email=email, subject=subject, message=message)
data.save()# Send an email
send_mail(
f'New Contact: {subject}',
f'From: {name}, Email: {email}\n\nMessage:\n{message}',
email,['support@webside.ca'],
fail_silently=False,)
messages.success(request,'Your message has been sent. Thank you!')return redirect('home')elif form_type =='application_form':
first_name = request.POST.get('firstName','')
last_name = request.POST.get('lastName','')
status = request.POST.get('status','')
phone = request.POST.get('phone','')
email = request.POST.get('email','')
postal_code = request.POST.get('postalCode','')
province = request.POST.get('province','')
agreement = request.POST.get('flexCheckIndeterminate')=='on'
data =Apply(first_name=first_name, last_name=last_name, status=status, phone=phone, email=email, postal_code=postal_code, province=province, agreement=agreement)
data.save()
send_mail(
f'New Apply',
f'From: {first_name} {last_name}, Status: {status}, Phone: {phone}, Email: {email}, Postal Code: {postal_code}, Province: {province}\n\nHas agreed to contact: {"Yes" if agreement else "No"}',
email,['admin@webside.ca'],
fail_silently=False,)returnJsonResponse({'success':True})else:# Handle the case where CAPTCHA is not successfully completed
messages.error(request,'Invalid reCAPTCHA. Please try again.')returnJsonResponse({'success':False}, status=400)return render(request,'pages/home.html')
كود html:
هذه form تكون على شكل نافذة منبثقة عند النقر على زر معين تعرض form
<divclass="modal fade"id="formModal"tabindex="-1"aria-hidden="true"><divclass="modal-dialog modal-dialog-centered"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title"id="exampleModalLabel">Apply Now</h5><buttontype="button"class="btn-close"data-bs-dismiss="modal"aria-label="Close"></button></div><divclass="modal-body"><formmethod="post"id="applyForm">
{% csrf_token %}
<divclass="mb-3"><inputtype="hidden"name="form_type"value="application_form"><labelfor="firstName"class="form-label">First Name*</label><inputtype="text"class="form-control"id="firstName"name="firstName"required></div><divclass="mb-3"><labelfor="lastName"class="form-label">Last Name*</label><inputtype="text"class="form-control"id="lastName"name="lastName"required></div><divclass="mb-3"><labelfor="status"class="form-label">Status*</label><selectclass="form-control"id="status"name="status"required><optionvalue="">Select Status</option><optionvalue="Canadian Citizen">Canadian Citizen</option><optionvalue="Permanent Resident">Permanent Resident</option><optionvalue="Refugee">Refugee</option><optionvalue="Work Permit">Work Permit</option><optionvalue="Visitor Visa">Visitor Visa</option><optionvalue="International Student">International Student</option></select></div><divclass="mb-3"><labelfor="userPhone"class="form-label">Phone</label><inputtype="number"class="form-control"id="userPhone"name="phone"></div><divclass="mb-3"><labelfor="userEmail"class="form-label">Email*</label><inputtype="email"class="form-control"id="userEmail"name="email"required></div><divclass="mb-3"><labelfor="postalCode"class="form-label">Postal Code</label><inputtype="text"class="form-control"id="postalCode"name="postalCode"></div><divclass="mb-3"><labelfor="province"class="form-label">Province</label><selectclass="form-control"id="province"name="province"><optionvalue="">Select Province</option><optionvalue="Alberta">Alberta</option><optionvalue="British Columbia">British Columbia</option><optionvalue="Manitoba">Manitoba</option><optionvalue="New Brunswick">New Brunswick</option><optionvalue="Newfoundland and Labrador">Newfoundland and Labrador</option><optionvalue="Nova Scotia">Nova Scotia</option><optionvalue="Ontario">Ontario</option><optionvalue="Prince Edward Island">Prince Edward Island</option><optionvalue="Quebec">Quebec</option><optionvalue="Saskatchewan">Saskatchewan</option><optionvalue="Northwest Territories">Northwest Territories</option><optionvalue="Nunavut">Nunavut</option><optionvalue="Yukon">Yukon</option></select></div><divclass="mb-3"><inputclass="form-check-input"type="checkbox"value="on"id="flexCheckIndeterminate"name="flexCheckIndeterminate"required><labelclass="form-check-label"for="flexCheckIndeterminate">
I agree to be contacted by email
</label></div><divclass="g-recaptcha"data-sitekey="6LeV7rApAAAAANhHYmNEky9nNreToYW3fJonpSar"></div><br><spanclass="loader"></span><buttontype="submit"class="btn btn-primary">Submit</button></form><!-- Success Message, hidden initially --><divid="successMessage"style="display: none;"><divclass="content"><divclass="wrapper-1"><divclass="wrapper-2"><h1>Thank you!</h1><p>We will contact you as soon as possible</p><buttonclass="go-home"onclick="window.location.href='/';">
go home
</button></div></div></div></div></div></div></div></div>
كود جافا سكريبت:
document.getElementById('applyForm').addEventListener('submit',function(e){
e.preventDefault();const formData =newFormData(this);const submitButton =this.querySelector('button[type="submit"]');const loader = document.querySelector('.loader');
submitButton.disabled =true;
loader.style.display ='block';// Display the loader
fetch("{% url 'home' %}",{
method:'POST',
body: formData,
headers:{'X-Requested-With':'XMLHttpRequest'},}).then(response =>{if(!response.ok)thrownewError('Network response was not ok');return response.json();}).then(data =>{if(data.success){
$('#applyForm').hide();// Hide the form
$('#successMessage').show();// Show success message}else{thrownewError('Failed to submit');}}).catch(error =>{
alert(error.message);}).finally(()=>{
loader.style.display ='none';// Hide the loader
submitButton.disabled =false;});});
السؤال
Abdulazeez Altameemi
اواجهة مشكلة عدم ارسال ايميل من الاستضافة و في localhost يقوم بالارسال بدون اي مشكلة
views.py
تحتوي على ثنين من form و اضفت لهم captcha ... انا قمت بازالة المفتاح السري للcaptcha
كود html:
هذه form تكون على شكل نافذة منبثقة عند النقر على زر معين تعرض form
كود جافا سكريبت:
8 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.