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 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, 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 Create Resource Group for Scheduling on the Exclusive Resource Groups tab.
    4. In the Create a dedicated resource group panel, 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 OK.
  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, find the workspace with which you want to associate the exclusive resource group.
    3. Click Bind in the Actions column.
  3. Go to the DataStudio page.
    1. Click Workspaces in the left-side navigation pane.
    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 DataStudio page, move the pointer over the Create 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 the Node Name and Location parameters.
      Note The node name must be 1 to 128 characters in length and can contain letters, digits, underscores (_), and periods (.).
    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)
      Alternatively, you can enter the following code to send emails:
      import smtplib
      from email.mime.text import MIMEText
      from odps import ODPS
      mail_host = 'smtp.office365.com'  // The address of the email server.
      mail_username = 'xxxx' // The username that is used to log on to the mailbox of the sender.
      mail_password = 'xxx'  // The password that is used to log on to the mailbox of the sender.
      mail_sender = 'xxx' // The email address of the sender.
      mail_receivers = ['xxx']  // 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()
                 smtpObj.connect(mail_host,587)
                 smtpObj.ehlo()
                 smtpObj.starttls()
                 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 reads 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 icon in the top toolbar.
  5. Commit the node.
    Notice Before you commit the node, you must click the Properties tab in the right-side navigation pane and set the Rerun and Parent Nodes parameters.
    1. Click the Submit icon icon in the top 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.
    2. In the left-side navigation pane of the Operation Center page, 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 > Modify Scheduling Resource Group in the Actions column.
    5. In the Modify Scheduling 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.