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.
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
Add an exclusive resource group.
Log on to the DataWorks console.
In the left-side navigation pane, click Resource Groups.
On the Resource Groups page, click Create Resource Group for Scheduling on the Exclusive Resource Groups tab.
Set the parameters as required. For more information, see Create and use an exclusive resource group for scheduling.
NoteAdd an exclusive resource group that is in the same region as the DataWorks workspace.
Click Buy Now.
Associate the exclusive resource group with the desired workspace.
On the Exclusive Resource Groups tab, find the desired exclusive resource group and click Change Workspace in the Actions column.
In the Modify home workspace dialog box, find the workspace with which you want to associate the exclusive resource group.
Click Bind in the Actions column.
Go to the DataStudio page.
Log on to the DataWorks console. In the left-side navigation pane, choose . On the page that appears, select the desired workspace from the drop-down list and click Go to DataStudio.
Create a PyODPS 2 node.
On the DataStudio page, move the pointer over the icon and choose .
You can also click the desired workflow, right-click MaxCompute, and then choose
.In the Create Node dialog box, set the Name and Path parameters.
NoteThe node name must be 1 to 128 characters in length and can contain letters, digits, underscores (_), and periods (.).
Click Confirm.
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. 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. 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)
NoteWhen 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.
Click the icon in the top toolbar.
Commit the node.
ImportantBefore you commit the node, you must click the Properties tab in the right-side navigation pane and set the Rerun and Parent Nodes parameters.
Click the icon in the top toolbar.
In the Commit Node dialog box, enter your comments in the Change description field.
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.
Change the resource group that is used to run the PyODPS 2 node.
On the configuration tab of the PyODPS 2 node, click Operation Center in the upper-right corner.
In the left-side navigation pane of the Operation Center page, choose .
On the page that appears, click the rightwards arrow in the middle to show the node list.
Find the desired node and choose in the Actions column.
In the Modify Scheduling Resource Group dialog box, select the desired resource group from the New Resource Group drop-down list.
Click OK.
Test the PyODPS 2 node. For more information, see View and manage auto triggered nodes.