- 论坛徽章:
- 13
|
本帖最后由 hmchzb19 于 2016-04-15 16:32 编辑
这个代码是可以跑的。- import smtplib
- from email.mime.text import MIMEText
- from email.mime.base import MIMEBase
- from email.mime.multipart import MIMEMultipart
- from email.utils import COMMASPACE,formatdate
- from email import encoders
- #charset.add_charset('utf-8', charset.QP, charset.QP)
- def my_send_mail(subject,message,from_addr,*to_addrs,headers):
- email=MIMEText(message,"plain",_charset="utf-8")
- #email=MIMEMultipart()
- assert type(headers)==dict
- email['subject']=subject
- email['From']=from_addr
- headers=headers.copy()
- for (header,value) in headers.items():
- email[header]=value
-
- host="localhost"
- port=1025
- sender=smtplib.SMTP(host,port)
- for addr in to_addrs:
- del email['To']
- email['To']=addr
- email['Date']=formatdate(localtime=True)
-
- sender.sendmail(from_addr,addr,email.as_string())
- sender.quit()
- headers={"Reply-To":"me2@exmaple.com"}
- my_send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com",headers=headers)
复制代码 下面的代码就不能执行,会报错。- def send_mail(subject,message,from_addr,*to_addrs,**headers):
- email=MIMEText(message,"plain",_charset="utf-8")
- #email=MIMEMultipart()
- assert type(headers)==dict
- email['subject']=subject
- email['From']=from_addr
- headers=headers.copy()
- for (header,value) in headers.items():
- email[header]=value
-
- host="localhost"
- port=1025
- sender=smtplib.SMTP(host,port)
- for addr in to_addrs:
- del email['To']
- email['To']=addr
- email['Date']=formatdate(localtime=True)
-
- sender.sendmail(from_addr,addr,email.as_string())
- sender.quit()
- 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'})
复制代码 不能使用- send_mail("A model subject","The message contents","from@example.com","to1@example.com","to2@exmaple.com","Reply-To"="me2@exmaple.com")
复制代码 |
|