0

I am writing to seek your assistance with a connectivity issue that I am experiencing while trying to use the Zoho Mail SMTP server for sending emails from an Ubuntu server.

The issue I am encountering is a timeout error when attempting to run the code on the Ubuntu server. The same code works without any issues on a Windows environment, but for some reason, it does not work on the Ubuntu server.

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from fastapi import FastAPI, HTTPException
from email.header import Header
from email.utils import formataddr

sender_name= " Account Security"
smtp_server = "smtp.zoho.com"
smtp_port = 587
smtp_username = "########"
smtp_password = "PASSWORD" 
def send_reset_email(email, token,fullname):
    try:
        # Create a message
        msg = MIMEMultipart()
        
        #print(Header(sender_name, 'utf-8') + f" <{smtp_username}>")
        msg["From"] = formataddr((str(Header(sender_name, 'utf-8')), smtp_username))
        msg["To"] = email
        msg["Subject"] = "Password Reset Request"

        # Compose the email body
        body = f"Dear {fullname},\n\n"
        body += "We received a request to reset your password. To proceed, please click the link below:\n\n"
        body += f"http://#####.com/reset-password?token={token}\n\n"
        body += "If you did not request this password reset, you can safely ignore this email. Your account security is important to us.\n\n"
        body += "Thank you for using our service!\n\n"
  

        msg.attach(MIMEText(body, "plain"))

        # Connect to the SMTP server and send the email
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.sendmail(smtp_username, email, msg.as_string())

    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to send reset email")

#Function to send email to the user with the verification token
def send_verification_email(email, token):
    try:
        # Create a message
        msg = MIMEMultipart()
        # msg["From"] = Header(seander_name)+ f"<{smtp_username}>"
        msg["From"] = smtp_username
        msg["To"] = email
        msg["Subject"] = "Email Verification Request"

        # Compose the email body
        body = f"Dear User,\n\n"
        body += "We received a request to verify your email. To proceed, please click the link below:\n\n"
        body += f"https://########/verify_email/{token}\n\n"
        body += "If you did not request this email verification, you can safely ignore this email. Your account security is important to us.\n\n"
        body += "Thank you for using our service!\n\n"
    

        msg.attach(MIMEText(body, "plain"))

        # Connect to the SMTP server and send the email
        with smtplib.SMTP(smtp_server, smtp_port) as server:
            server.starttls()
            server.login(smtp_username, smtp_password)
            server.sendmail(smtp_username, email, msg.as_string())
        print("Verification email sent successfully")
    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to send verification email")
    
print(send_verification_email("info@####.com","123456"))
1
  • As an aside, your code seems to be written for Python 3.5 or earlier. The email library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the examples from the email documentation. Commented Oct 18, 2023 at 12:40

1 Answer 1

0

Here is a guide you can check for send emails with python APP in Zoho email using free account https://medium.com/@mayurrathi2/sending-mail-from-zoho-mail-forever-free-plan-using-python-dca11bbd055e

My script for sending emails is this one:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

to_email = '[email protected]'
info_email = '[email protected]'
password = 'noLoAveriguaras'

message = "body message part"
msg = MIMEText(message)
msg['Subject'] = 'this is the subject'
msg['From'] = info_email
msg['To'] = to_email

server = smtplib.SMTP_SSL('smtp.zoho.com', 465)

server.login(info_email, password)
server.sendmail(info_email, to_email, msg.as_string())
server.quit()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.