|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + |
| 4 | +# This script provides a basic example of how to use the Email Service in the Python SDK. This script accepts two |
| 5 | +# will demonstrate: |
| 6 | +# |
| 7 | +# * Creating, retrieving, listing and deleting email senders |
| 8 | +# * Creating, retrieving, listing and deleting email suppressions |
| 9 | +# * Obtaining SMTP credentials for your IAM user so that you can send emails. |
| 10 | +# See https://docs.us-phoenix-1.oraclecloud.com/Content/Email/Tasks/configuresmtpconnection.htm for more |
| 11 | +# information on sending emails |
| 12 | +# |
| 13 | +# This script accepts three arguments: |
| 14 | +# |
| 15 | +# * The compartment ID where email senders will be created |
| 16 | +# * The address of the email sender |
| 17 | +# * The address of the email suppression |
| 18 | +# |
| 19 | +# Note that email senders are created in the compartment which you specify, but the suppressions are always created at the tenancy |
| 20 | +# level. The tenancy will be read from your configuration file. |
| 21 | + |
| 22 | +import oci |
| 23 | +import sys |
| 24 | + |
| 25 | +# Default config file and profile |
| 26 | +config = oci.config.from_file() |
| 27 | +email_client = oci.email.EmailClient(config) |
| 28 | +identity_client = oci.identity.IdentityClient(config) |
| 29 | + |
| 30 | +if len(sys.argv) != 4: |
| 31 | + raise RuntimeError('This script expects an argument of a compartment OCID, sender email address and suppression email address') |
| 32 | + |
| 33 | +# The first argument is the name of the script, so start the index at 1 |
| 34 | +compartment_id = sys.argv[1] |
| 35 | +sender_address = sys.argv[2] |
| 36 | +suppression_address = sys.argv[3] |
| 37 | + |
| 38 | +create_sender_response = email_client.create_sender( |
| 39 | + oci.email.models.CreateSenderDetails( |
| 40 | + compartment_id=compartment_id, |
| 41 | + email_address=sender_address |
| 42 | + ) |
| 43 | +) |
| 44 | +print('Created sender:\n{}'.format(create_sender_response.data)) |
| 45 | +print('\n=========================\n') |
| 46 | + |
| 47 | +# A sender has a lifecycle state, so we can wait for it to become available |
| 48 | +get_sender_response = oci.wait_until(email_client, email_client.get_sender(create_sender_response.data.id), 'lifecycle_state', 'ACTIVE') |
| 49 | +print('Waited for sender to become available:\n{}'.format(get_sender_response.data)) |
| 50 | +print('\n=========================\n') |
| 51 | + |
| 52 | +# We can list all senders, and also provide optional filters and sorts. Here we'll list all |
| 53 | +# senders sorted by their email address, and also demonstrate filtering by sender email |
| 54 | +# address (an exact match filter) |
| 55 | +# |
| 56 | +# Listing senders is a paginated operation, so we can use the functions in oci.pagination |
| 57 | +senders = oci.pagination.list_call_get_all_results( |
| 58 | + email_client.list_senders, |
| 59 | + compartment_id, |
| 60 | + sort_by='EMAILADDRESS', |
| 61 | + sort_order='ASC' |
| 62 | +).data |
| 63 | +print('Listing senders sorted by email address:') |
| 64 | +for s in senders: |
| 65 | + print(s) |
| 66 | +print('\n=========================\n') |
| 67 | + |
| 68 | +senders = oci.pagination.list_call_get_all_results( |
| 69 | + email_client.list_senders, |
| 70 | + compartment_id, |
| 71 | + email_address='fake-{}'.format(sender_address) |
| 72 | +).data |
| 73 | +print('Listing senders filtered by email address - no data expected:') |
| 74 | +for s in senders: |
| 75 | + print(s) |
| 76 | +print('\n=========================\n') |
| 77 | + |
| 78 | +# Suppressions do not have a lifecycle state, so we don't have to wait on anything after creation |
| 79 | +create_suppression_response = email_client.create_suppression( |
| 80 | + oci.email.models.CreateSuppressionDetails( |
| 81 | + compartment_id=config['tenancy'], |
| 82 | + email_address=suppression_address |
| 83 | + ) |
| 84 | +) |
| 85 | +print('Created suppression:\n{}'.format(create_suppression_response.data)) |
| 86 | +print('\n=========================\n') |
| 87 | + |
| 88 | +# We can list all suppressions, and also provide optional filters and sorts. Here we'll list all |
| 89 | +# suppressions sorted by their time created, and also demonstrate filtering by suppression email |
| 90 | +# address (an exact match filter) |
| 91 | +# |
| 92 | +# Listing senders is a paginated operation, so we can use the functions in oci.pagination |
| 93 | +suppressions = oci.pagination.list_call_get_all_results( |
| 94 | + email_client.list_suppressions, |
| 95 | + config['tenancy'], |
| 96 | + sort_by='TIMECREATED', |
| 97 | + sort_order='DESC' |
| 98 | +).data |
| 99 | +print('Listing suppressions sorted by time created:') |
| 100 | +for s in suppressions: |
| 101 | + print(s) |
| 102 | +print('\n=========================\n') |
| 103 | + |
| 104 | +suppressions = oci.pagination.list_call_get_all_results( |
| 105 | + email_client.list_suppressions, |
| 106 | + config['tenancy'], |
| 107 | + email_address='fake-{}'.format(suppression_address) |
| 108 | +).data |
| 109 | +print('Listing suppressions filtered by email address - no data expected:') |
| 110 | +for s in suppressions: |
| 111 | + print(s) |
| 112 | +print('\n=========================\n') |
| 113 | + |
| 114 | +# We can also delete a sender and then wait for it to be deleted. The sender may already be deleted |
| 115 | +# by the time we call oci.wait_until, so pass the get_sender_response to the waiter. It is recommended that |
| 116 | +# you have a get response prior to calling the delete, INSTEAD OF doing: |
| 117 | +# |
| 118 | +# oci.wait_until(email_client, email_client.get_sender(sender_id), ...) |
| 119 | +# |
| 120 | +# When deleting, since the resource may be gone, we set succeed_on_not_found on the waiter so that we consider |
| 121 | +# receiving a 404 back from the service as a successful delete |
| 122 | +email_client.delete_sender(get_sender_response.data.id) |
| 123 | +oci.wait_until(email_client, get_sender_response, 'lifecycle_state', 'DELETED', succeed_on_not_found=True) |
| 124 | +print('Deleted sender') |
| 125 | + |
| 126 | +# Suppressions do not have a lifecycle state, so we don't have to wait on anything after deletion |
| 127 | +email_client.delete_suppression(create_suppression_response.data.id) |
| 128 | +print('Deleted suppression') |
| 129 | + |
| 130 | +# In order to send email, we'll need to create an SMTP credential associated with an IAM user. More |
| 131 | +# information on sending email can be found here: |
| 132 | +# https://docs.us-phoenix-1.oraclecloud.com/Content/Email/Tasks/configuresmtpconnection.htm |
| 133 | +# |
| 134 | +# Note, also, that an IAM user can only have two active SMTP credentials at any time |
| 135 | +# |
| 136 | +# Also the password for the SMTP credential is ONLY available in the create response, so you |
| 137 | +# should store/save this as it won't be retrievable later |
| 138 | +create_smtp_credential_response = identity_client.create_smtp_credential( |
| 139 | + oci.identity.models.CreateSmtpCredentialDetails( |
| 140 | + description='new credential' |
| 141 | + ), |
| 142 | + user_id=config['user'] |
| 143 | +) |
| 144 | +print('Created SMTP credential:\n{}'.format(create_smtp_credential_response.data)) |
| 145 | +print('\n=========================\n') |
| 146 | + |
| 147 | +# We can update the description of an SMTP credential |
| 148 | +update_smtp_credential_response = identity_client.update_smtp_credential( |
| 149 | + config['user'], |
| 150 | + create_smtp_credential_response.data.id, |
| 151 | + oci.identity.models.UpdateSmtpCredentialDetails( |
| 152 | + description='updated credential description' |
| 153 | + ) |
| 154 | +) |
| 155 | +print('Updated SMTP credential:\n{}'.format(update_smtp_credential_response.data)) |
| 156 | +print('\n=========================\n') |
| 157 | + |
| 158 | +# We can list the credentials for a user. Note that this is not a paginated operation |
| 159 | +list_smtp_credentials_response = identity_client.list_smtp_credentials(config['user']) |
| 160 | +print('SMTP credentials for user:\n{}'.format(list_smtp_credentials_response.data)) |
| 161 | +print('\n=========================\n') |
| 162 | + |
| 163 | +identity_client.delete_smtp_credential(config['user'], create_smtp_credential_response.data.id) |
| 164 | +print('Deleted SMTP credential') |
| 165 | + |
| 166 | +print('\nScript Finished') |
0 commit comments