Skip to content

Send E-mails

This Python script demonstrates sending emails via SMTP using the smtplib library and constructing MIME objects to compose rich content emails within SeaTable.


  • Setup and Authentication: Uses SMTP parameters and SeaTable API credentials for authentication and sending emails.
  • Recipient Selection: Retrieves email addresses from a SeaTable table's column (Contacts -> Email) to serve as multiple recipients.
  • Email Composition:

    • Constructs an email with a specified subject, sender, and HTML content body for the email.
    • Allows for attaching files stored in SeaTable records to the email.

Process Overview

  1. Retrieves recipient email addresses from a designated SeaTable table column (Contacts -> Email).
  2. Composes an email using HTML content to create a rich-text message body.
  3. Attaches a file from SeaTable to the email by fetching its download link using the SeaTable API and attaching it to the email.

This script offers an automated way to send emails with rich content and attachments using data stored within SeaTable, enabling streamlined communication and file sharing within the SeaTable environment.


import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
from urllib import parse
import requests
from seatable_api import Base, context

# SeaTable API authentication
base = Base(context.api_token, context.server_url)

# SMTP server configurations for sending emails
smtpserver = ''
username = ''
password = 'topsecret'
sender = ''

# Option a) define the recipient email address in this script
receivers = ['']

# Option b) Retrieving recipient email addresses from the 'Contacts' table in SeaTable
receiver_rows = base.list_rows('Contacts')
receivers = [row['Email'] for row in receiver_rows if row.get('Email')]

# Email subject
subject = 'SeaTable Send email'

# Constructing the email message
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = ' <>'
msg['To'] = ";".join(receivers)

# Option a) plain text message
# text = "Hi!\nHow are you?\nHere is the link you wanted:\n"
# text_plain = MIMEText(text,'plain', 'utf-8')
# msg.attach(text_plain)

# Option b) HTML content for the email body
html = """
       This is a sample of messages
       from SeaTable
text_html = MIMEText(html,'html', 'utf-8')

# Attaching a file from SeaTable to the email
rows = base.list_rows('Table3')
filename = rows[0]['File'][0]['name']
file_url = rows[0]['File'][0]['url']
path = file_url[file_url.find('/files/'):]
download_link = base.get_file_download_link(parse.unquote(path))

    response = requests.get(download_link)
    if response.status_code != 200:
        print('Failed to download image, status code: ', response.status_code)
except Exception as e:

# Attaching the file to the email
text_att = MIMEText(response.content, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
text_att["Content-Disposition"] = 'attachment;filename*=UTF-8\'\'' + parse.quote(filename)


# Sending the email using SMTP
smtp = smtplib.SMTP()
smtp.login(username, password)
smtp.sendmail(sender, receivers, msg.as_string())