- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
Creating a web application may not be challenging, but mastering the skills to enhance its visual appeal can be quite demanding. This is why the prevalence of full-stack frameworks is increasing—they allow you to build comprehensive applications with just backend expertise. In this blog post, we will explore the process of how to send an email using Panel, a robust and user-friendly visualization and full-stack Python framework.
Proceed with the blog if you have already installed the Nylas Python SDK and configured your Python environment. Alternatively, you can refer to the Nylas Quickstart Guides.
If you haven’t done so, I suggest reading the post titled How to Send Emails with the Nylas Python SDK, which guides the basic setup.
Let’s outline the envisioned appearance of our application. It will feature a combo box for selecting the recipient, three text inputs for entering the name, email, and subject, a text area for composing the body, and a submit button. The combo box will retrieve the contacts list through the Nylas Contacts API. Upon selecting a contact, its name and email will automatically fill the corresponding fields, though we can manually specify the name and email.
Panel’s first version was released on August 31, 2018, and it’s currently on version 1.3.2a1.
The web applications run on Tornado by default, but others like Flask, Django or Fast API.
As a stand-alone application, it’s powered by Pyodide or PyScript.
The learning curve encompasses a blend of straightforward and more challenging elements, resulting in an overall positive experience. The documentation proves to be sufficiently helpful, and the additional support from their GitHub repository enhances the learning process significantly.
We can install it by typing the following command in our terminal window:
$ pip3 install panel
Once installed, we’re ready to go. Let’s create a file called panel_send_email.py with the following code:
# Import your dependencies from dotenv import load_dotenv import os from nylas import APIClient # type: ignore import panel as pn # Load your env variables load_dotenv() # Initialize an instance of the Nylas SDK using the client credentials nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ACCESS_TOKEN"), ) # List to hold all contacts emails_list = {'':''} # Grab the contacts from the specified group contacts = nylas.contacts.where(source = 'address_book', group = "517v55haghlcvnuu7lcm4f7k8") # Loop all contacts and get the Id and Full Name for contact in contacts: emails_list[f'{contact.given_name + " " + contact.surname}'] = list(contact.emails.values())[0][0] # Call the notification message def notify(event, msg): pn.state.notifications.info(msg) # Get the contact name def get_name(value): name = pn.widgets.TextInput(name='Name') try: # Get the dictionary key from the value name.value = list(emails_list.keys())[list(emails_list.values()).index(value)] except: name.value = "" return name # Clean up widgets def clean(event): name_hold().value = "" email.value = "" subject.value = "" body.value = "" name_hold().value = "" def send_email(event): # Create the draft draft = nylas.drafts.create() # Assign subject, body and name and email of recipient draft.subject = subject.value draft.body = body.value draft.to = [{"name": f"{name_hold().value}", "email": f"{email.value}"}] try: # Send the email draft.send() clean(event) notify(event, "Email was sent successfully") except Exception as e: notify(event, f'An error ocurred: {e}') finally: pass # Activate extensions pn.extension(notifications=True) pn.extension('texteditor') # Define the widgets html_pane = pn.pane.HTML("""<h1>Email Sender</h1>""") select = pn.widgets.Select(name='Contacts', options=emails_list) email = pn.widgets.TextInput(name='Email') name_hold = pn.bind(get_name, email) subject = pn.widgets.TextInput(name='Subject') body = pn.widgets.TextEditor(placeholder='Enter some text') send = pn.widgets.Button(name='Send', button_type='primary') # Link the select with the email widget select.jslink(email, value='value') # When we press the send button send.on_click(send_email) # Add the widgets to the application pn.Row( # Add spacers to keep everything in the middle pn.Spacer(sizing_mode='stretch_both'), pn.Column( # Add the widgets in order html_pane, select, email, name_hold, subject, body, send ),pn.Spacer(sizing_mode='stretch_both') # Add this to call the web server ).servable()
To run our application, we need to type the following on our terminal window:
$ panel serve panel_send_email.py --show --autoreload
The browser will open automatically:
Here we can fill in the details and send our email:
After clicking send, a notification will appear at the bottom right of the screen, indicating whether the email was successfully sent or not:
And we can check our mailbox just to confirm:
We just send an email using Panel without spending too much time building a complicated UI.
To learn more about our Email APIs, visit our documentation Email API Overview.
You can sign up for Nylas for free and start building!
Don’t miss the action, join our LiveStream Coding with Nylas!
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.