Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782349
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-04-15 21:47:51

下面是可以运行的代码。

点击(此处)折叠或打开

  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()


可以执行的调用:

点击(此处)折叠或打开

  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')
  2.                                                                                                              ^
  3. SyntaxError: keyword can't be an expression
这个报错有点像这个:

点击(此处)折叠或打开

  1. >>> d1={1:"one"}
  2. >>> d2={"1":"one"}
  3. >>> a=dict(1="one")
  4.   File "", line 1
  5. SyntaxError: keyword can't be an expression
  6. >>> b=dict("1"="one")
  7.   File "", line 1
  8. SyntaxError: keyword can't be an expression

参考python 发送邮件的代码:
http://blog.csdn.net/menglei8625/article/details/7721746


阅读(1276) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~