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.
Feat(Builder): Add email sending block (#7736)
Bently904b444b13b7
diff --git a/rnd/autogpt_server/.env.template b/rnd/autogpt_server/.env.templateindex 8b1ce0ee8f..9a5ed2fc0f 100644--- a/rnd/autogpt_server/.env.template+++ b/rnd/autogpt_server/.env.template@@ -10,4 +10,10 @@ REDDIT_USERNAME=REDDIT_PASSWORD=# Discord-DISCORD_BOT_TOKEN=\ No newline at end of file+DISCORD_BOT_TOKEN=++# SMTP/Email+SMTP_SERVER=+SMTP_PORT=+SMTP_USERNAME=+SMTP_PASSWORD=\ No newline at end of filediff --git a/rnd/autogpt_server/autogpt_server/blocks/email_block.py b/rnd/autogpt_server/autogpt_server/blocks/email_block.pynew file mode 100644index 0000000000..d73708887a--- /dev/null+++ b/rnd/autogpt_server/autogpt_server/blocks/email_block.py@@ -0,0 +1,101 @@+import smtplib+from email.mime.multipart import MIMEMultipart+from email.mime.text import MIMEText++from pydantic import BaseModel, ConfigDict, Field++from autogpt_server.data.block import Block, BlockCategory, BlockOutput, BlockSchema+from autogpt_server.data.model import BlockSecret, SchemaField, SecretField+++class EmailCredentials(BaseModel):+ smtp_server: str = Field(+ default="smtp.gmail.com", description="SMTP server address"+ )+ smtp_port: int = Field(default=25, description="SMTP port number")+ smtp_username: BlockSecret = SecretField(key="smtp_username")+ smtp_password: BlockSecret = SecretField(key="smtp_password")++ model_config = ConfigDict(title="Email Credentials")+++class SendEmailBlock(Block):+ class Input(BlockSchema):+ to_email: str = SchemaField(+ description="Recipient email address", placeholder="[email protected]"+ )+ subject: str = SchemaField(+ description="Subject of the email", placeholder="Enter the email subject"+ )+ body: str = SchemaField(+ description="Body of the email", placeholder="Enter the email body"+ )+ creds: EmailCredentials = Field(+ description="SMTP credentials",+ default=EmailCredentials(),+ )++ class Output(BlockSchema):+ status: str = SchemaField(description="Status of the email sending operation")+ error: str = SchemaField(+ description="Error message if the email sending failed"+ )++ def __init__(self):+ super().__init__(+ id="a1234567-89ab-cdef-0123-456789abcdef",+ description="This block sends an email using the provided SMTP credentials.",+ categories={BlockCategory.TEXT},+ input_schema=SendEmailBlock.Input,+ output_schema=SendEmailBlock.Output,+ test_input={+ "to_email": "[email protected]",+ "subject": "Test Email",+ "body": "This is a test email.",+ "creds": {+ "smtp_server": "smtp.gmail.com",+ "smtp_port": 25,+ "smtp_username": "[email protected]",+ "smtp_password": "your-gmail-password",+ },+ },+ test_output=[("status", "Email sent successfully")],+ test_mock={"send_email": lambda *args, **kwargs: "Email sent successfully"},+ )++ @staticmethod+ def send_email(+ creds: EmailCredentials, to_email: str, subject: str, body: str+ ) -> str:+ try:+ smtp_server = creds.smtp_server+ smtp_port = creds.smtp_port+ smtp_username = creds.smtp_username.get_secret_value()+ smtp_password = creds.smtp_password.get_secret_value()++ msg = MIMEMultipart()+ msg["From"] = smtp_username+ msg["To"] = to_email+ msg["Subject"] = subject+ msg.attach(MIMEText(body, "plain"))++ with smtplib.SMTP(smtp_server, smtp_port) as server:+ server.starttls()+ server.login(smtp_username, smtp_password)+ server.sendmail(smtp_username, to_email, msg.as_string())++ return "Email sent successfully"+ except Exception as e:+ return f"Failed to send email: {str(e)}"++ def run(self, input_data: Input) -> BlockOutput:+ status = self.send_email(+ input_data.creds,+ input_data.to_email,+ input_data.subject,+ input_data.body,+ )+ if "successfully" in status:+ yield "status", status+ else:+ yield "error", statusdiff --git a/rnd/autogpt_server/autogpt_server/util/settings.py b/rnd/autogpt_server/autogpt_server/util/settings.pyindex 791f40e97d..db20f88cb9 100644--- a/rnd/autogpt_server/autogpt_server/util/settings.py+++ b/rnd/autogpt_server/autogpt_server/util/settings.py@@ -111,6 +111,11 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):discord_bot_token: str = Field(default="", description="Discord bot token")+ smtp_server: str = Field(default="", description="SMTP server IP")+ smtp_port: str = Field(default="", description="SMTP server port")+ smtp_username: str = Field(default="", description="SMTP username")+ smtp_password: str = Field(default="", description="SMTP password")+# Add more secret fields as neededmodel_config = SettingsConfigDict(