AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(block): Improve error handling of SendEmailBlock (#11420)
Swifty0921d2362804
diff --git a/autogpt_platform/backend/backend/blocks/email_block.py b/autogpt_platform/backend/backend/blocks/email_block.pyindex 1e252a608c..fad2f411cb 100644--- a/autogpt_platform/backend/backend/blocks/email_block.py+++ b/autogpt_platform/backend/backend/blocks/email_block.py@@ -1,4 +1,6 @@import smtplib+import socket+import sslfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom typing import Literal@@ -48,9 +50,7 @@ def SMTPCredentialsField() -> SMTPCredentialsInput:class SMTPConfig(BaseModel):- smtp_server: str = SchemaField(- default="smtp.example.com", description="SMTP server address"- )+ smtp_server: str = SchemaField(description="SMTP server address")smtp_port: int = SchemaField(default=25, description="SMTP port number")model_config = ConfigDict(title="SMTP Config")@@ -67,10 +67,7 @@ class SendEmailBlock(Block):body: str = SchemaField(description="Body of the email", placeholder="Enter the email body")- config: SMTPConfig = SchemaField(- description="SMTP Config",- default=SMTPConfig(),- )+ config: SMTPConfig = SchemaField(description="SMTP Config")credentials: SMTPCredentialsInput = SMTPCredentialsField()class Output(BlockSchemaOutput):@@ -120,7 +117,7 @@ class SendEmailBlock(Block):msg["Subject"] = subjectmsg.attach(MIMEText(body, "plain"))- with smtplib.SMTP(smtp_server, smtp_port) as server:+ with smtplib.SMTP(smtp_server, smtp_port, timeout=30) as server:server.starttls()server.login(smtp_username, smtp_password)server.sendmail(smtp_username, to_email, msg.as_string())@@ -130,10 +127,59 @@ class SendEmailBlock(Block):async def run(self, input_data: Input, *, credentials: SMTPCredentials, **kwargs) -> BlockOutput:- yield "status", self.send_email(- config=input_data.config,- to_email=input_data.to_email,- subject=input_data.subject,- body=input_data.body,- credentials=credentials,- )+ try:+ status = self.send_email(+ config=input_data.config,+ to_email=input_data.to_email,+ subject=input_data.subject,+ body=input_data.body,+ credentials=credentials,+ )+ yield "status", status+ except socket.gaierror:+ yield "error", (+ f"Cannot connect to SMTP server '{input_data.config.smtp_server}'. "+ "Please verify the server address is correct."+ )+ except socket.timeout:+ yield "error", (+ f"Connection timeout to '{input_data.config.smtp_server}' "+ f"on port {input_data.config.smtp_port}. "+ "The server may be down or unreachable."+ )+ except ConnectionRefusedError:+ yield "error", (+ f"Connection refused to '{input_data.config.smtp_server}' "+ f"on port {input_data.config.smtp_port}. "+ "Common SMTP ports are: 587 (TLS), 465 (SSL), 25 (plain). "+ "Please verify the port is correct."+ )+ except smtplib.SMTPNotSupportedError:+ yield "error", (+ f"STARTTLS not supported by server '{input_data.config.smtp_server}'. "+ "Try using port 465 for SSL or port 25 for unencrypted connection."+ )+ except ssl.SSLError as e:+ yield "error", (+ f"SSL/TLS error when connecting to '{input_data.config.smtp_server}': {str(e)}. "+ "The server may require a different security protocol."+ )+ except smtplib.SMTPAuthenticationError:+ yield "error", (+ "Authentication failed. Please verify your username and password are correct."+ )+ except smtplib.SMTPRecipientsRefused:+ yield "error", (+ f"Recipient email address '{input_data.to_email}' was rejected by the server. "+ "Please verify the email address is valid."+ )+ except smtplib.SMTPSenderRefused:+ yield "error", (+ "Sender email address defined in the credentials that where used"+ "was rejected by the server. "+ "Please verify your account is authorized to send emails."+ )+ except smtplib.SMTPDataError as e:+ yield "error", f"Email data rejected by server: {str(e)}"+ except Exception as e:+ raise e