Oracle Cloud Infrastructure Documentation

Integrating Python with Email Delivery

You can use Python to send emails through Email Delivery. Before you can send email you must configure Email Delivery in Python.


These steps assume you are logged into an Oracle Linux instance. Other distributions of Linux may have different commands and file locations.

Configure Python to Send Email Through Email Delivery

To enable Python to test the configuration of Email Delivery:

  1. Ensure Email Delivery is configured to send email. See Getting Started with Email Delivery.


The SMTP credentials are required to configure Python to use Email Delivery. Be sure to note the user name and password when you generate the SMTP credentials.

  1. Ensure Python is installed. The installation process differs depending on which operating system you are using. For example, run the following command to install Python on Oracle Linux:

    sudo yum install python3 -y
  2. In a file editor such as vi, create a python script to test Email Delivery.

    Run the following command:

    sudo vi
  3. In the file, replace the variables with your own values.

    For example:

    #python script for sending SMTP configuration with Oracle Cloud Infrastructure Email Delivery
    import smtplib 
    import email.utils
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    # Replace with your "From" address.
    # This address must be verified.
    SENDER = '' 
    SENDERNAME = 'Sender Name'
    # Replace with a "To" address. If your account
    # is still in the sandbox, this address must be verified.
    RECIPIENT  = ''
    # Replace the USERNAME_SMTP value with your Email Delivery SMTP username.
    USERNAME_SMTP = 'ocid1.user.oc1..<unique_ID>@ocid1.tenancy.oc1..<unique_ID>'
    # Replace the PASSWORD_SMTP value with your Email Delivery SMTP password.
    PASSWORD_SMTP = '<password>'
    # If you're using Email Delivery in a different region, replace the HOST value with an SMTP endpoint. Use port 25 or 587 to connect to the SMTP endpoint.
    HOST = ""
    PORT = 587
    # The subject line of the email.
    SUBJECT = 'Email Delivery Test (Python smtplib)'
    # The email body for recipients with non-HTML email clients.
    BODY_TEXT = ("Email Delivery Test\r\n"
                 "This email was sent through the Email Delivery SMTP "
                 "Interface using the Python smtplib package."
    # The HTML body of the email.
    BODY_HTML = """<html>
      <h1>Email Delivery SMTP Email Test</h1>
      <p>This email was sent with Email Delivery using the
        <a href=''>Python</a>
        <a href=''>
        smtplib</a> library.</p>
    # Create message container - the correct MIME type is multipart/alternative.
    msg = MIMEMultipart('alternative')
    msg['Subject'] = SUBJECT
    msg['From'] = email.utils.formataddr((SENDERNAME, SENDER))
    msg['To'] = RECIPIENT
    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(BODY_TEXT, 'plain')
    part2 = MIMEText(BODY_HTML, 'html')
    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
    # Try to send the message.
        server = smtplib.SMTP(HOST, PORT)
        #smtplib docs recommend calling ehlo() before & after starttls()
        server.login(USERNAME_SMTP, PASSWORD_SMTP)
        server.sendmail(SENDER, RECIPIENT, msg.as_string())
    # Display an error message if something goes wrong.
    except Exception as e:
        print ("Error: ", e)
        print ("Email successfully sent!")
  4. To send a test email with Python, run the following command from the directory the script is located in:


More Information

  • More Python script examples can be found on GitHub.