Problem Summary:
I have a paid account on PythonAnywhere and am attempting to set up email sending using Zoho's SMTP server (smtp.zoho.com). However, I'm encountering a "connection refused" error when trying to connect to Zoho's SMTP server from PythonAnywhere. The same code works perfectly in my local environment.
Technical Details:
SMTP Server: smtp.zoho.com
Port: 465 (SSL)
SMTP Connection Code:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email Configuration
EMAIL_REMETENTE = "pandora-system@saghaz.com.br"
EMAIL_SENHA = "**************"
EMAIL_SMTP = "smtp.zoho.com"
EMAIL_PORTA = 465
def enviar_email(destinatario, assunto, corpo):
msg = MIMEMultipart()
msg['From'] = EMAIL_REMETENTE
msg['To'] = destinatario
msg['Subject'] = assunto
msg.attach(MIMEText(corpo, 'plain'))
try:
server = smtplib.SMTP_SSL(EMAIL_SMTP, EMAIL_PORTA)
server.login(EMAIL_REMETENTE, EMAIL_SENHA)
server.sendmail(EMAIL_REMETENTE, destinatario, msg.as_string())
server.quit()
print(f"Email sent successfully to {destinatario}")
except Exception as e:
print(f"Failed to send email. Error: {str(e)}")
Behavior on PythonAnywhere:
When attempting to connect to smtp.zoho.com using port 465 with SSL, I receive a "connection refused" error. I also tried port 587 for TLS but encountered the same issue.
Local Environment:
The same code works correctly in my local environment, sending emails without issues.
Tests Performed:
I created a separate test script to check SMTP connectivity directly:
import smtplib
try:
server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
server.login('pandora-system@saghaz.com.br', '**************')
server.quit()
print("SMTP connectivity successful.")
except Exception as e:
print(f"Error connecting to SMTP: {e}")
Running this script on PythonAnywhere resulted in the error message: [Errno 111] Connection refused.
Additional Details:
I checked the list of allowed domains for free accounts and found no restrictions for Zoho. I suspect there may be some block or restriction on PythonAnywhere, even after upgrading to a paid account.
Conclusion: I need assistance in resolving the "connection refused" issue when attempting to use Zoho's SMTP on PythonAnywhere, as the same code works correctly in my local environment.
[formatted by admin]