Python FAQ

来自Fantasy的维基百科
(版本间的差异)
跳转至: 导航, 搜索
 
(未显示1个用户的4个中间版本)
第11行: 第11行:
 
===发送MIME邮件附件时,收件人列表注意事项===
 
===发送MIME邮件附件时,收件人列表注意事项===
 
#!/usr/bin/env python
 
#!/usr/bin/env python
#-*- coding: utf-8 -*-
+
#coding = utf-8
#encoding = utf-8
+
  
 
import smtplib, mimetypes  
 
import smtplib, mimetypes  
import time
+
 
from email.mime.text import MIMEText  
+
from email.mime.text import MIMEText
 +
 
 +
 
 
from email.mime.multipart import MIMEMultipart   
 
from email.mime.multipart import MIMEMultipart   
 +
 
from email.mime.image import MIMEImage   
 
from email.mime.image import MIMEImage   
  
 
msg            = MIMEMultipart()   
 
msg            = MIMEMultipart()   
 +
 
msg['From']    = "mail1@163.com"  
 
msg['From']    = "mail1@163.com"  
 +
 
#msg['To']    = ["mail1@163.com","mail1@163.com","mail1@163.com"] 会出错  
 
#msg['To']    = ["mail1@163.com","mail1@163.com","mail1@163.com"] 会出错  
 +
 
mail_to        = ["mail1@163.com","mail1@163.com","mail1@163.com"]  
 
mail_to        = ["mail1@163.com","mail1@163.com","mail1@163.com"]  
 +
 
msg['To']      = ', '.join(mail_to)  
 
msg['To']      = ', '.join(mail_to)  
 +
 
msg['Subject'] = "Report " + (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))  
 
msg['Subject'] = "Report " + (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))  
http://bytes.com/topic/python/answers/472868-sending-emails-list-recipients: The problem is that SMTP.sendmail and email.MIMEText need two different things. email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.So, what you need to do is COMBINE the two replies you received. Set msg‘To’ to a single string, but pass the raw list to sendmail:msg['To'] = ', '.join( emails ) s.sendmail( msg['From'], emails, msg.as_string() )
+
 
 +
http://bytes.com/topic/python/answers/472868-sending-emails-list-recipients
 +
 
 +
The problem is that SMTP.sendmail and email.MIMEText need two different things. email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.So, what you need to do is COMBINE the two replies you received. Set msg‘To’ to a single string, but pass the raw list to sendmail:msg['To'] = ', '.join( emails ) s.sendmail( msg['From'], emails, msg.as_string() )

2014年10月11日 (六) 02:15的最后版本

[编辑] 设置默认字符集

Python2的string默认为bytes类型,改为UniCode,然后使用UTF-8编码即可避免频繁的检查和转码操作。 在Lib/site.py里查找: encoding = "ascii" # Default value set by _PyUnicode_Init() 在下面加上一行 encoding = "utf-8" 以后基本上所有的乱码问题和EncodeError都会自动消失。

Python3的做法:是把 sys.setdefaultencoding("ascii") 改成了 "utf-8"。

[编辑] 发送MIME邮件附件时,收件人列表注意事项

  1. !/usr/bin/env python
  2. coding = utf-8

import smtplib, mimetypes

from email.mime.text import MIMEText


from email.mime.multipart import MIMEMultipart

from email.mime.image import MIMEImage

msg = MIMEMultipart()

msg['From'] = "mail1@163.com"

  1. msg['To'] = ["mail1@163.com","mail1@163.com","mail1@163.com"] 会出错

mail_to = ["mail1@163.com","mail1@163.com","mail1@163.com"]

msg['To'] = ', '.join(mail_to)

msg['Subject'] = "Report " + (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

http://bytes.com/topic/python/answers/472868-sending-emails-list-recipients

The problem is that SMTP.sendmail and email.MIMEText need two different things. email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.So, what you need to do is COMBINE the two replies you received. Set msg‘To’ to a single string, but pass the raw list to sendmail:msg['To'] = ', '.join( emails ) s.sendmail( msg['From'], emails, msg.as_string() )

个人工具
名字空间

变种
操作
导航
工具