python mail sent using thread

mail sent

from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from threading import Thread

def send_email_threaded(otp, recipient_email):
    """Helper function to send email asynchronously in a separate thread."""
    try:
        # Render the HTML template with the OTP
        html_content = render_to_string(
            'otp_email_template.html', {'otp': otp}
        )

        # Create the email object
        email = EmailMessage(
            subject="Your OTP for Login",  # Email subject
            body=html_content,  # HTML content for the email
            from_email="pythonpopit@gmail.com",  # Your email address
            to=[recipient_email],  # Recipient's email
        )
        email.content_subtype = 'html'  # Specify that the email content is HTML

        # Send the email
        email.send(fail_silently=False)
        print("OTP email sent successfully!")
    except Exception as e:
        print(f"Error sending OTP email: {e}")

def test_email(otp, the_host):
    """Function to initiate sending the email in a separate thread."""
    # Run the email sending in a separate thread to avoid blocking
    thread = Thread(target=send_email_threaded, args=(otp, the_host))
    thread.start()



python mail port open or not finding in  puttyy

telnet mail.mycarpresso.com 587

telnet mail.mycarpresso.com 465

telnet mail.mycarpresso.com 25



to find ther error just creat dummy mail sent function with try expect and pass to httprespose
url it in a button



def test_email_view(request):
    subject = 'Test Email via EmailMessage'
    body = 'This is a test email sent using Django EmailMessage class.'
    from_email = 'webadmin@mycarpresso.com'
    to_email = ['jobinj5210@gmail.com']  # Replace with the test recipient

    try:
        email = EmailMessage(subject, body, from_email, to_email)
        email.send()
        return HttpResponse("Test email sent successfully via EmailMessage.")
    except Exception as e:
        return HttpResponse(f"Failed to send email via EmailMessage. Error: {str(e)}")
   





Comments

Popular posts from this blog

React Js

Custom Middleware in django using session key