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.
- Email Address Used: [email protected]
- SMTP Server:smtp.zoho.com
- SMTP Port: 587
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"))
emaildocumentation.