环境准备
背景: 阿里云文档通过DataWorks实现邮件外发最佳实践没有实际案例,本篇实测以供参考
1.创建项目
工作空间: zhqtest
项目名称: zhqtest_maxcompute_dev 和 zhqtest_maxcompute
地域: 杭州
2.准备邮箱
本文准备两个邮箱: QQ邮箱(zhq@qq.com) 发送至网易云邮箱(zhq@163.com)
3.准备数据
可自行创建一个表或者使用已有表, 插入少量数据即可
-- 创建admin表 CREATE TABLE IF NOT EXISTS zhqtest_maxcompute_dev.admin ( id STRING ,account STRING ) ; -- 插入数据 INSERT INTO TABLE zhqtest_maxcompute_dev.admin(id,account) VALUES (666,"jack"); -- 查询数据 SELECT id,account FROM admin where id =666;
配置步骤
1.创建业务流程
在zhqtest工作空间下创建一个业务流程,名称为 linshitest
2.创建节点
创建pyodps2节点,以官方文档提供的代码为例(提示:后续文档会有更新),目前代码仅供参考
下面是复制出来的内容,未做任何修改
import smtplib from email.mime.text import MIMEText from odps import ODPS mail_host ='<yourHost>' //邮箱服务地址 mail_username ='<yourUserName>' //登录用户名 mail_password ='<yourPassWord>' //登录用户密码 mail_sender ='<senderAddress>' //发件人邮箱地址 mail_receivers = ['<receiverAddress>'] //收件人邮箱地址 mail_content="" //邮件内容 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)
3.配置说明
mail_host = '<yourHost>' //邮箱服务地址 示例: smtp.qq.com
注意: dataworks邮件外发限制: TCP 25端口是默认的邮箱 服务端口。出于安全考虑,云服务器ECS的25端口默认受限,独享资源组无法支持该端口,建议您使用465端口发送邮件。所以代码配置465端口
● 谷歌邮箱(google.com): POP3服务器地址:pop.gmail.com(SSL启用端口:995) SMTP服务器地址:smtp.gmail.com(SSL启用端口:587) ● 网易邮箱(163.com): POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) ● Foxmail邮箱(foxmail.com): POP3服务器地址:POP.foxmail.com(端口:110) SMTP服务器地址:SMTP.foxmail.com(端口:25) ● QQ邮箱(mail.qq.com) POP3服务器地址:pop.qq.com(端口:110) SMTP服务器地址:smtp.qq.com(端口:25) ● 阿里云邮箱(mail.aliyun.com): POP3服务器地址:pop3.aliyun.com(SSL加密端口:995;非加密端口:110) SMTP服务器地址:smtp.aliyun.com(SSL加密端口:465;非加密端口:25) IMAP服务器地址:imap.aliyun.com(SSL加密端口:993;非加密端口:143)
mail_username = '<yourUserName>' //登录用户名 示例: zhq@qq.com
mail_password = '<yourPassWord>' //登录用户密码 示例: uamebrmlmjtydjff
mail_sender = '<senderAddress>' //发件人邮箱地址 示例: zhq@qq.com
mail_receivers = ['<receiverAddress>'] //收件人邮箱地址 示例: zhq@163.com
endpoint='maxcompute_service_endpoint' //项目接入点,公网和VPC内网都可以 示例: http://service.cn-hangzhou.maxcompute.aliyun.com/api (http://service.cn-hangzhou.maxcompute.aliyun-inc.com/api)
其他配置略
4.配置案例
下面是配置好的demo, 实际测试的时候改为自己的配置信息即可
import smtplib from email.mime.text import MIMEText from odps import ODPS mail_host ='smtp.qq.com'mail_username ='zhq@qq.com'mail_password ='uamebrmlmjtydjff'mail_sender ='zhq@qq.com'mail_receivers = ['zhq@163.com'] mail_content="查询admin表"o=ODPS('LTAI*******NoxVAV','GhPL*************ckjvR6zKCI0','zhqtest_maxcompute_dev',endpoint='http://service.cn-hangzhou.maxcompute.aliyun.com/api') with o.execute_sql('SELECT id,account FROM admin where id = 666').open_reader() as reader: for record in reader: mail_content+=str(record['id'])+' '+record['account']+'\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+'smtp.qq.com: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.运行代码
注意:
- 使用高级运行,选择独享资源组
- 注释信息全部需要去掉, 例如: //邮箱服务地址
根据上述配置报错如下 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
报错原因: 编码有误 代码中增加三行
import sys reload(sys) sys.setdefaultencoding("UTF-8")
重新运行成功
6.登录验证
手机登录网易邮箱确认收到信息
常见问题
1.注释报错
报错: 上传后点击"上传安装"时报错 如附件
原因: 有代码注释信息需要去掉
2.报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
原因: 编码格式有误
解决: 增加三行代码
import sys reload(sys) sys.setdefaultencoding("UTF-8")
3.报错: ('mail send error', SMTPAuthenticationError(535, 'Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256'))
原因: 一般是密码有误
解决: 需要PC端登录QQ邮箱获取授权码