All Products
Search
Document Center

DataWorks:Use a PyODPS node to send emails

Last Updated:Jul 17, 2026

You can use a PyODPS node running on an exclusive resource group to send emails from DataWorks.

Background information

DataWorks supports sending emails through PyODPS nodes that interact with MaxCompute, or through data push tasks.

Note
  • TCP port 25 is the default email service port but is disabled on Elastic Compute Service (ECS) instances for security reasons. Because exclusive resource groups cannot use port 25, use port 465 to send emails.

  • When a PyODPS node runs on an exclusive resource group, users cannot connect to the ECS instance used for sending emails. Therefore, you cannot install additional third-party Python modules on the resource group.

Methods to send emails

Use a PyODPS node to send emails

  1. Create an exclusive resource group.

    1. Log on to the DataWorks console. In the target region, click Resource Group in the left-side navigation pane to open the resource group list.

    2. On the Exclusive Resource Group tab, click Create Resource Group.

    3. Configure the parameters based on your business requirements. For more information, see Use serverless resource groups.

      Note

      Make sure that the resource group that you want to create resides in the same region as your DataWorks workspace.

    4. Click Buy Now.

  2. Associate the resource group with a workspace.

    1. Find the created resource group and click Associate Workspace in the Actions column.

    2. In the Bind Workspace dialog box, find the target workspace and click Associate in the Actions column.

  3. Go to the DataStudio page.

    Log on to the DataWorks console. In the target region, click Data Development and O&M > Data Development in the left-side navigation pane. Select a workspace from the drop-down list and click Go to Data Development.

  4. Create a PyODPS 2 node.

    1. Hover over the image.png icon, and choose Create Node > MaxCompute > PyODPS 2.

      Alternatively, open a workflow, right-click MaxCompute, and choose Create Node > PyODPS 2.

    2. In the Create Node dialog box, enter a Name and select a Path.

      Note

      The node name cannot exceed 128 characters in length and can contain letters, digits, underscores (_), and periods (.).

    3. Click OK.

    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.
      mail_password = '<yourPassWord>'  //The password that is used to log on to the mailbox.
      mail_sender = '<senderAddress>' //The email address of the sender.
      mail_receivers = ['<receiverAddress>']  //The email address of the recipient.
      mail_content=""        //The email content that you want to send.
      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)           

      You can also enter the following code to send emails:

      import smtplib
      from email.mime.text import MIMEText
      from odps import ODPS
      mail_host = 'username@example.com'  //The address of the email server.
      mail_username = 'xxxx' //The username that is used to log on to the mailbox.
      mail_password = 'xxx'  //The password that is used to log on to the mailbox.
      mail_sender = 'xxx' //The email address of the sender.
      mail_receivers = ['xxx']  //The email address of the recipient.
      mail_content=""        //The email content that you want to send.
      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

      The PyODPS 2 node stores data in a temporary file before sending it by email. There is no limit on the number of data records in the email.

    5. Click the 保存 icon in the top toolbar.

  5. Commit the node.

    Important

    Before you submit the node, you must click Scheduling Settings on the right side of the page to configure the Rerun attribute and Parent Nodes properties.

    1. Click the 提交 icon in the top toolbar.

    2. In the Commit Node dialog box, enter your Remarks.

    3. Click OK.

    If your workspace is in Deploy, click Deploy in the upper-right corner after you submit the node. For more information, see Deploy tasks.

  6. Change the resource group that is used to run the PyODPS 2 node.

    1. On the editor page for the PyODPS 2 node, click O&M in the upper-right corner.

    2. In the left-side navigation pane, choose Auto Triggered Task O&M > Periodic tasks.

    3. Find the target node and choose More > Change Resource Group for Scheduling in the Actions column.

    4. In the Change Resource Group for Scheduling dialog box, select a resource group from the Select Resource Group drop-down list.

    5. Click OK.

  7. Test the node. For more information, see View and manage auto triggered tasks.

Use a data push node to send emails

You can also use the data push feature or data push nodes to send data to specified email addresses.

  • Data push node: In DataStudio, you can create a data push node within a workflow to push the output parameters of nodes in an auto triggered task to a destination email address. For more information, see Data push node.

  • Data push feature: In DataService Studio, you can create a data push task that uses SQL statements for single- or multi-table queries to define push data, organize it in rich text or tables, and deliver it to a specified email address on a schedule. For more information, see Data push.

Note

Each data push node or task supports only one email body. Once an email body is added, you cannot add another one.