This topic describes how to use a PyODPS node that runs 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. DataWorks cannot automatically send emails on schedule. 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, the users of the exclusive resource group cannot log on to the ECS instances in the group. As a result, the users cannot install other third-party Python modules to implement additional 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, click Add Exclusive Resource Group on the Exclusive Resource Groups tab.
    4. In the Add Exclusive Resource Group pane, set the parameters as required. For more information, see Create and use an exclusive resource group for scheduling.
      Note Add an exclusive resource group that is in the same region as the DataWorks workspace.
    5. Click Create.
  2. Associate the exclusive resource group with the desired workspace.
    1. On the Exclusive Resource Groups tab, find the desired exclusive resource group and click Change Workspace in the Actions column.
    2. In the Modify home workspace dialog box, select the workspace with which you want to associate the exclusive resource group.
    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 desired workspace resides in the upper-left corner.
    3. In the workspace list, find the desired workspace and click Data Analytics in the Actions column. The DataStudio page appears.
  4. Create a PyODPS 2 node.
    1. On the Data Development tab, move the pointer over the Create icon icon and choose MaxCompute > PyODPS 2.
      You can also click the desired workflow, right-click MaxCompute, and then choose Create > PyODPS 2.
    2. In the Create Node dialog box, 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 not case-sensitive.
    3. Click Commit.
    4. On the configuration tab of the PyODPS 2 node, enter the following code to send emails by using 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 that is used to log on to the mailbox of the sender.
      mail_password = '<yourPassWord>'  // The password that is used to log 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)
      Note When you use the PyODPS 2 node to send mails, the PyODPS 2 node stores the data it read in a temporary file first and sends the data by email. The number of data records in the email that you want to send is unlimited.
    5. Click the 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 the Commit icon icon in the toolbar.
    2. In the Commit Node dialog box, enter your comments in the Change description field.
    3. Click OK.
    In a workspace in standard mode, you must click Deploy in the upper-right corner after you commit the node. For more information, see Deploy nodes.
  6. Change the resource group that is used to run 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 desired node and choose More > Change Resource Group in the Actions column.
    5. In the Change Resource Group dialog box, 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 View auto triggered nodes.