Email Notifier Skill
This skill enables the agent to send emails, particularly for delivering generated artifacts like PDF certificates to students.
Usage
Input:
- •Recipient Email: The student's email address.
- •Subject: Email subject line.
- •Body: The main text of the email.
- •Attachment Path: Absolute path to the file to be attached (e.g., the PDF certificate).
- •Sender Credentials: (Ideally retrieved from environment variables or secure storage, but for this context, may need to be provided or mocked).
Instructions
When the user gives a command like "Send the certificate to student@example.com", generate a Python script with the following logic:
- •Imports:
smtplib,ssl, andemailmodules (MIMEMultipart,MIMEText,MIMEBase,encoders). - •Configuration:
- •SMTP Server (e.g.,
smtp.gmail.comfor Gmail). - •Port (typically 465 for SSL or 587 for TLS).
- •Sender Email & Password (use placeholders if not provided).
- •SMTP Server (e.g.,
- •Construction:
- •Create a
MIMEMultipartobject. - •Attach the body text.
- •Open the attachment file in binary mode (
'rb'), encode it in base64, and attach it with appropriate headers.
- •Create a
- •Sending:
- •Establish a secure connection using
context = ssl.create_default_context(). - •Login and send the mail.
- •Establish a secure connection using
Example Output Script
python
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
def send_email_with_pdf(sender_email, sender_password, receiver_email, subject, body, pdf_path):
# Setup the MIME
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
# Attach body
message.attach(MIMEText(body, 'plain'))
# Attach PDF
filename = os.path.basename(pdf_path)
try:
with open(pdf_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
message.attach(part)
text = message.as_string()
except Exception as e:
print(f"Error attaching file: {e}")
return
# Log in to server using secure context and send email
context = ssl.create_default_context()
smtp_server = "smtp.gmail.com" # Example: Gmail
port = 465 # For SSL
try:
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, text)
print(f"Email sent successfully to {receiver_email}")
except Exception as e:
print(f"Failed to send email: {e}")
# Usage Example
if __name__ == "__main__":
# You would typically load these from env vars
SENDER = "admin@jabalf15ai.com"
PASSWORD = "your_app_password"
RECEIVER = "student@example.com"
PDF_FILE = "path/to/certificate.pdf"
SUBJ = "Congratulations! Your JABALF15AI Certificate"
MSG = "Dear Student,\n\nPlease find attached your certificate of completion.\n\nBest,\nJABALF15AI Team"
# send_email_with_pdf(SENDER, PASSWORD, RECEIVER, SUBJ, MSG, PDF_FILE)
print("Script generated. Please configure sender credentials to run.")