This topic describes how to use a PyODPS node running on an exclusive resource group to send emails.

Background information

Different from a Python script, a PyODPS node in DataWorks can interact with MaxCompute for data analytics and processing. Currently, DataWorks cannot automatically send emails as scheduled. You can create a PyODPS node and run it on an exclusive resource group to read data from MaxCompute and then send emails.
Note TCP port 25 is the default email service port. For security purposes, port 25 is blocked on Elastic Compute Service (ECS) instances. Therefore, you cannot use port 25 on exclusive resource groups. We recommend that you use port 465 to send emails.

If you run a PyODPS node on an exclusive resource group to send emails, users of the exclusive resource group cannot log on to the ECS instances in the group. As a result, the users cannot install more third-party Python modules to implement more features.

Procedure

  1. Add an exclusive resource group.
    1. Log on to the DataWorks console.
    2. In the left-side navigation pane, click Resource Groups.
    3. On the Resource Groups page that appears, click Add Exclusive Resource Group on the Exclusive Resource Groups tab.
    4. In the Add Exclusive Resource Group pane that appears, set the parameters as required. For more information, see Add and use exclusive resource groups for scheduling.
      Note Add an exclusive resource group that is in the same region as the target DataWorks workspace.
    5. Click Create.
  2. Bind the exclusive resource group to the target workspace.
    1. On the Exclusive Resource Groups tab, find the target exclusive resource group and click Change Workspace in the Actions column.
    2. In the Change Workspace dialog box that appears, select the workspace to which the exclusive resource group will be bound.
    3. Click OK.
  3. Go to the DataStudio page.
    1. Click Workspaces in the left-side navigation pane. The Workspaces page appears.
    2. Select the region where the target workspace resides in the upper-left corner.
    3. In the workspace list, find the target workspace and click Data Analytics in the Actions column. The DataStudio page appears.
  4. Create a PyODPS 2 node.
    1. On the Data Analytics tab, move the pointer over the Create icon icon and choose MaxCompute > PyODPS 2.
      You can also click the target workflow, right-click MaxCompute, and then choose Create > PyODPS 2.
    2. In the Create Node dialog box that appears, set Node Name and Location.
      Note The node name must be 1 to 128 characters in length and can contain letters, digits, underscores (_), and periods (.). It is case-insensitive.
    3. Click Commit.
    4. On the configuration tab of the PyODPS 2 node, enter the following code for sending emails through Simple Mail Transfer Protocol (SMTP):
      import smtplib
      from email.mime.text import MIMEText
      from odps import ODPS
      mail_host = '<yourHost>' // The address of the email server.
      mail_username = '<yourUserName>' // The username for logging on to the mailbox of the sender.
      mail_password = '<yourPassWord>'  // The password for logging on to the mailbox of the sender.
      mail_sender = '<senderAddress>' // The email address of the sender.
      mail_receivers = ['<receiverAddress>']  // The email address of the recipient.
      mail_content=""        // The email content to be sent.
      o=ODPS('access_key','access_secretkey','default_project_name',endpoint='maxcompute_service_endpoint')
      with o.execute_sql('query_sql').open_reader() as reader:
                 for record in reader:
                         mail_content+=str(record['column_name'])+' '+record['column_name']+'\n'
      message = MIMEText(mail_content,'plain','utf-8')
      message['Subject'] = 'mail test'
      message['From'] = mail_sender
      message['To'] = mail_receivers[0]
      try:
                 smtpObj = smtplib.SMTP_SSL(mail_host+':465')
                 smtpObj.login(mail_username,mail_password)
                 smtpObj.sendmail(
                     mail_sender,mail_receivers,message.as_string())
                 smtpObj.quit()
                 print('mail send success')
      except smtplib.SMTPException as e:
                 print('mail send error',e)
    5. Click Save icon in the toolbar.
  5. Commit the node.
    Notice Before you commit the node, you must click Properties in the right-side navigation pane and set Rerun and Parent Nodes.
    1. Click Submit icon in the toolbar.
    2. In the Commit Node dialog box that appears, enter your comments in the Description field.
    3. Click OK.
    In a workspace in the standard mode, you need to click Deploy in the upper-right corner after you commit the node. For more information, see Deploy a node.
  6. Change the resource group for running the PyODPS 2 node.
    1. On the configuration tab of the PyODPS 2 node, click Operation Center in the upper-right corner. The Operation Center page appears.
    2. In the left-side navigation pane, choose Cycle Task Maintenance > Cycle Task.
    3. On the page that appears, click the rightwards arrow in the middle to show the node list.
    4. Find the target node and choose More > Change Resource Group in the Actions column.
    5. In the Change Resource Group dialog box that appears, select the desired resource group from the New Resource Group drop-down list.
    6. Click OK.
  7. Test the PyODPS 2 node. For more information, see Auto triggered nodes.