Python

Send emails through Sendfully's SMTP server using Python's built-in smtplib.

Use Python's built-in smtplib and email modules to send emails through Sendfully's SMTP server.

Setup

Before you start, make sure you have:

  1. An API key with sending access
  2. A verified sending domain

Send a basic email

import osimport smtplibfrom email.mime.text import MIMEText
msg = MIMEText("<p>Your order has shipped.</p>", "html")msg["From"] = "Your App <notifications@mail.yourdomain.com>"msg["To"] = "recipient@example.com"msg["Subject"] = "Your order has shipped"
with smtplib.SMTP_SSL("smtp.sendfully.com", 465) as server:    server.login("sendfully", os.environ["SENDFULLY_API_KEY"])    server.send_message(msg)

The From address must use a domain you've verified in Sendfully.

Send with attachments

import osimport smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email import encoders
msg = MIMEMultipart()msg["From"] = "Your App <notifications@mail.yourdomain.com>"msg["To"] = "recipient@example.com"msg["Subject"] = "Your invoice"
msg.attach(MIMEText("<p>Your invoice is attached.</p>", "html"))
with open("invoice.pdf", "rb") as f:    attachment = MIMEBase("application", "octet-stream")    attachment.set_payload(f.read())    encoders.encode_base64(attachment)    attachment.add_header("Content-Disposition", "attachment", filename="invoice.pdf")    msg.attach(attachment)
with smtplib.SMTP_SSL("smtp.sendfully.com", 465) as server:    server.login("sendfully", os.environ["SENDFULLY_API_KEY"])    server.send_message(msg)

What's next?