免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3021 | 回复: 4
打印 上一主题 下一主题

函数参数使用**dict 会报错 [复制链接]

论坛徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役纪念章
日期:2016-07-22 11:15:35数据库技术版块每日发帖之星
日期:2016-05-27 06:20:002015亚冠之吉达阿赫利
日期:2015-08-05 10:06:542015年亚洲杯之韩国
日期:2015-04-01 16:05:42双鱼座
日期:2014-11-13 11:04:24丑牛
日期:2014-07-25 17:29:54子鼠
日期:2014-04-25 12:25:45丑牛
日期:2014-04-17 08:35:48巨蟹座
日期:2014-04-16 16:50:05CU大牛徽章
日期:2013-03-14 14:14:29CU大牛徽章
日期:2013-03-14 14:14:26
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-04-15 15:30 |只看该作者 |倒序浏览
本帖最后由 hmchzb19 于 2016-04-15 16:32 编辑

这个代码是可以跑的。
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.base import MIMEBase
  4. from email.mime.multipart import MIMEMultipart

  5. from email.utils import COMMASPACE,formatdate
  6. from email import encoders

  7. #charset.add_charset('utf-8', charset.QP, charset.QP)
  8. def my_send_mail(subject,message,from_addr,*to_addrs,headers):
  9.     email=MIMEText(message,"plain",_charset="utf-8")
  10.     #email=MIMEMultipart()
  11.     assert type(headers)==dict
  12.     email['subject']=subject
  13.     email['From']=from_addr
  14.     headers=headers.copy()
  15.     for (header,value) in headers.items():
  16.         email[header]=value
  17.    
  18.     host="localhost"
  19.     port=1025
  20.     sender=smtplib.SMTP(host,port)
  21.     for addr in to_addrs:
  22.         del email['To']
  23.         email['To']=addr
  24.         email['Date']=formatdate(localtime=True)
  25.         
  26.         sender.sendmail(from_addr,addr,email.as_string())
  27.     sender.quit()


  28. headers={"Reply-To":"me2@exmaple.com"}
  29. my_send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",headers=headers)
复制代码
下面的代码就不能执行,会报错。
  1. def send_mail(subject,message,from_addr,*to_addrs,**headers):
  2.     email=MIMEText(message,"plain",_charset="utf-8")
  3.     #email=MIMEMultipart()
  4.     assert type(headers)==dict
  5.     email['subject']=subject
  6.     email['From']=from_addr
  7.     headers=headers.copy()
  8.     for (header,value) in headers.items():
  9.         email[header]=value
  10.    
  11.     host="localhost"
  12.     port=1025
  13.     sender=smtplib.SMTP(host,port)
  14.     for addr in to_addrs:
  15.         del email['To']
  16.         email['To']=addr
  17.         email['Date']=formatdate(localtime=True)
  18.         
  19.         sender.sendmail(from_addr,addr,email.as_string())
  20.     sender.quit()

  21. send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",{"Reply-To":"me2@exmaple.com"})
复制代码
调用语句可以这么写:
  1. send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",**{'Reply-To':'me2@exmaple.com'})
复制代码
不能使用
  1. send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com","Reply-To"="me2@exmaple.com")
复制代码

论坛徽章:
5
巨蟹座
日期:2014-08-28 18:12:342015年迎新春徽章
日期:2015-03-04 10:01:4415-16赛季CBA联赛之江苏
日期:2016-04-28 09:43:3115-16赛季CBA联赛之吉林
日期:2016-06-22 10:34:4315-16赛季CBA联赛之山西
日期:2016-08-16 16:29:55
2 [报告]
发表于 2016-04-15 16:08 |只看该作者
  1. 不是应该传这样的么?
  2. "Reply-To" = "me2@exmaple.com"
复制代码

论坛徽章:
13
CU大牛徽章
日期:2013-03-14 14:14:082016科比退役纪念章
日期:2016-07-22 11:15:35数据库技术版块每日发帖之星
日期:2016-05-27 06:20:002015亚冠之吉达阿赫利
日期:2015-08-05 10:06:542015年亚洲杯之韩国
日期:2015-04-01 16:05:42双鱼座
日期:2014-11-13 11:04:24丑牛
日期:2014-07-25 17:29:54子鼠
日期:2014-04-25 12:25:45丑牛
日期:2014-04-17 08:35:48巨蟹座
日期:2014-04-16 16:50:05CU大牛徽章
日期:2013-03-14 14:14:29CU大牛徽章
日期:2013-03-14 14:14:26
3 [报告]
发表于 2016-04-15 16:17 |只看该作者
本帖最后由 hmchzb19 于 2016-04-15 16:27 编辑

回复 2# Linux_manne
  1.     send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",'Reply-To'='me2@exmaple.com')
  2.                                                                                                              ^
  3. SyntaxError: keyword can't be an expression
复制代码

论坛徽章:
5
巨蟹座
日期:2014-08-28 18:12:342015年迎新春徽章
日期:2015-03-04 10:01:4415-16赛季CBA联赛之江苏
日期:2016-04-28 09:43:3115-16赛季CBA联赛之吉林
日期:2016-06-22 10:34:4315-16赛季CBA联赛之山西
日期:2016-08-16 16:29:55
4 [报告]
发表于 2016-04-15 17:54 |只看该作者
回复 3# hmchzb19


   哦,  那就用** unpacking 吧

论坛徽章:
2
巳蛇
日期:2014-06-02 13:33:59午马
日期:2015-01-11 00:11:16
5 [报告]
发表于 2016-04-16 22:07 |只看该作者
send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com","Reply-To"="me2@exmaple.com")

请改成

send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",Reply-To="me2@exmaple.com")

最后一个Reply-To不要用双引号引起来.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP