Skip to content

Commit

Permalink
Merge pull request #357 from jupyter-naas/356-integration---send-emai…
Browse files Browse the repository at this point in the history
…l-with-sendgrid

feat: add function to send email with Sendgrid integration
  • Loading branch information
FlorentLvr authored Feb 17, 2025
2 parents e2bd2f7 + 163c598 commit 15eb4b2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/core/integrations/SendGridIntegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,41 @@ def get_unsubscribe_groups(self) -> Dict:
Returns:
Dict: List of unsubscribe groups
"""
return self._make_request("GET", "/asm/groups")
return self._make_request("GET", "/asm/groups")

def send_email(
self,
from_email: str,
to_emails: List[str],
subject: str,
html_content: str,
plain_text_content: Optional[str] = None
) -> Dict:
"""Send an email using SendGrid.
Args:
from_email (str): Sender email address
to_emails (List[str]): List of recipient email addresses
subject (str): Email subject line
html_content (str): HTML content of the email
plain_text_content (Optional[str]): Plain text version of the email
Returns:
Dict: API response
"""
data = {
"personalizations": [{"to": [{"email": email} for email in to_emails]}],
"from": {"email": from_email},
"subject": subject,
"content": [
{"type": "text/html", "value": html_content}
]
}

if plain_text_content:
data["content"].insert(0, {"type": "text/plain", "value": plain_text_content})

return self._make_request("POST", "/mail/send", data)

def as_tools(configuration: SendGridIntegrationConfiguration):
"""Convert SendGrid integration into LangChain tools."""
Expand All @@ -157,6 +191,13 @@ class GetListsSchema(BaseModel):

class GetUnsubscribeGroupsSchema(BaseModel):
pass

class SendEmailSchema(BaseModel):
from_email: str = Field(..., description="Sender email address")
to_emails: List[str] = Field(..., description="List of recipient email addresses")
subject: str = Field(..., description="Email subject line")
html_content: str = Field(..., description="HTML content of the email")
plain_text_content: Optional[str] = Field(None, description="Plain text version of the email")

return [
StructuredTool(
Expand All @@ -182,5 +223,11 @@ class GetUnsubscribeGroupsSchema(BaseModel):
description="Get all unsubscribe groups.",
func=lambda: integration.get_unsubscribe_groups(),
args_schema=GetUnsubscribeGroupsSchema
),
StructuredTool(
name="sendgrid_send_email",
description="Send an email using SendGrid.",
func=lambda **kwargs: integration.send_email(**kwargs),
args_schema=SendEmailSchema
)
]
32 changes: 32 additions & 0 deletions tests/integrations/test_SendGridIntegration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from src.core.integrations.SendGridIntegration import SendGridIntegration, SendGridIntegrationConfiguration
from abi import logger
from src import secret

# Initialize configuration
configuration = SendGridIntegrationConfiguration(
api_key=secret.get("SENDGRID_API_KEY"),
)

# Initialize integration
sendgrid = SendGridIntegration(configuration)

# Create a test email
email_data = {
"from_email": "notifications@naas.ai",
"to_emails": ["florent@naas.ai"],
"subject": "Test Email",
"html_content": "<p>This is a test email from SendGrid Integration</p>",
"plain_text_content": "This is a test email from SendGrid Integration"
}

# Send email
response = sendgrid.send_email(
from_email=email_data["from_email"],
to_emails=email_data["to_emails"],
subject=email_data["subject"],
html_content=email_data["html_content"],
plain_text_content=email_data["plain_text_content"]
)

# Log the response
logger.info(f"Email sent with response: {response}")

0 comments on commit 15eb4b2

Please sign in to comment.