DEV Community

Cover image for Automate Email Newsletter with Lyzr-Automata
Rasswanth Shankar
Rasswanth Shankar

Posted on

Automate Email Newsletter with Lyzr-Automata

Lyzr-Automata is a low-code multi-agent automation framework that enables us to break down complex tasks into simple agents and thread them together. In this blog post, we’ll create an Automated Newsletter Publisher using lyzr-automata.

Setup

Create a folder, set up a virtual environment and activate it

virtualenv my_env
source my_env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install the following packages

lyzr-automata==0.1.2
python-dotenv==1.0.1
Enter fullscreen mode Exit fullscreen mode

Setup .env file

OPENAI_API_KEY = "YOUR OPENAI API KEY"
EMAIL = "YOUR EMAIL ID"
PASSWORD = "YOUR APP PASSWORD"
Enter fullscreen mode Exit fullscreen mode

Note: To get your App Password, follow these steps here

Get Started

Let’s create a file main.py and use that

1.Load .env variables

from dotenv import load_dotenv
import os

load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
PASSWORD = os.getenv("PASSWORD")
EMAIL = os.getenv("EMAIL")
Enter fullscreen mode Exit fullscreen mode

2.Initialise our Model

  • A Text to Text model to create content (GPT-4)

  • A Text to Image model to create a suitable image (DALLE-3)

open_ai_model_text = OpenAIModel(
    api_key=OPENAI_API_KEY,
    parameters={
        "model": "gpt-4-turbo-preview", # Specify Model
        "temperature": 0.2, # Creativity of the model
        "max_tokens": 1500, # Maximum tokens
    },
)

open_ai_model_image = OpenAIModel(
    api_key=OPENAI_API_KEY,
    parameters={
        "n": 1, # Number of images
        "model": "dall-e-3", # Specify Model
    },
)

Enter fullscreen mode Exit fullscreen mode

3.Build our agent

from lyzr_automata import Agent

content_researcher_agent = Agent(
    prompt_persona="You are an intelligent Travel Newsletter writer good at writing a detailed newsletter on a particular destination",
    role="Travel Newsletter writer",
)
Enter fullscreen mode Exit fullscreen mode

4. Implement our Tasks

from lyzr_automata import Task
from lyzr_automata.tasks.task_literals import InputType, OutputType

# Newsletter Content Creator Task
research_task = Task(
    name="Draft Content Creator",
    agent=content_researcher_agent,
    output_type=OutputType.TEXT,
    input_type=InputType.TEXT,
    model=open_ai_model_text,
    instructions="Write a travel newsletter on Mumbai in 500 words and [IMPORTANT!] send the response in html use bullets for points and beautify it be as creative as you want.",
    log_output=True,
    enhance_prompt=False,
)

# Image Creator Task
image_creation_task = Task(
    name="Newsletter Image Creation",
    output_type=OutputType.IMAGE,
    input_type=InputType.TEXT,
    model=open_ai_model_image,
    log_output=True,
    instructions="Use the travel newsletter provided and create an image that would be suitable for posting. Avoid any text in the image",
)

# Merge Image and Content Task
merge_image_text_task = Task(
    name = "Merge Image and Email",
    model=open_ai_model_text,
    log_output=True,
    instructions="Include the image in the html code provided. Return only the HTML and CSS code",
    input_tasks = [research_task, image_creation_task]
)
Enter fullscreen mode Exit fullscreen mode

5.Create our Email Sender Tool

from lyzr_automata.tools.prebuilt_tools import send_email_by_smtp_tool

email_sender = send_email_by_smtp_tool(
    username=EMAIL,
    password=PASSWORD,
    host="smtp.gmail.com",
    port=587,
    sender_email=EMAIL
)

send_email_task = Task(
    name = "Send Email Task",
    tool = email_sender,
    instructions="Send Email",
    model=open_ai_model_text,
    input_tasks = [merge_image_text_task],
    default_input = ["EMAIL1", "EMAIL2"] # Specify Reciever Email IDs HERE
)
Enter fullscreen mode Exit fullscreen mode

6.Create Pipeline and execute the tasks

from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from lyzr_automata import Logger

def main_program():
    logger = Logger()
    LinearSyncPipeline(
        logger=logger,
        name="Send Email",
        completion_message="Email Sent!",
        tasks=[
            research_task,
            image_creation_task,
            merge_image_text_task,
            send_email_task
        ],
    ).run()

main_program()
Enter fullscreen mode Exit fullscreen mode

7.Run the file and find the email in your inbox!

FLOW Diagram

Flow Diagram

And that’s a wrap. Feel free to play around with Lyzr-Automata on your own!

Lyzr-Automata Github
Code Link — LINK

Top comments (0)