免费注册 查看新帖 |

Chinaunix

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

关于用python来发email,用smtplib [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-15 11:40 |只看该作者 |倒序浏览
毕业设计里需要用到的,代码如下:

  1.       1 #! /usr/bin/env python
  2.       2
  3.       3 import smtplib
  4.       4 import email.Message
  5.       5
  6.       6 serv = "localhost"
  7.       7 sender = "zdai@localhost"
  8.       8 to = "bigapple2008@gmail.com"
  9.       9 subject = "hello, this mail is from bigapple"
  10.      10 text = open("emacs.html","r").read()
  11.      11
  12.      12 def mail(serverURL=None, sender='', to='', subject='', text=''):
  13.      13         message = email.Message.Message()
  14.      14         message["To"]      = to
  15.      15         message["From"]    = sender
  16.      16         message["Subject"] = subject
  17.      17         message.set_payload(text)
  18.      18         mailServer = smtplib.SMTP(serverURL)
  19.      19         mailServer.set_debuglevel(1)
  20.      20         mailServer.sendmail(sender, to, message.as_string())
  21.      21         mailServer.quit()
  22.      22
  23.      23
  24.      24 mail(serv,sender,to,subject,text)
复制代码

这个是我在测试的一个函数,能正常发送。但发出去的是文本,我读入的即使是html文件,邮箱里也只能收到<html><head>。。。。等之类的文本符号。
我原来是想能让用户收到的时候是一个html页面,然后可以放点链接在信里。小弟对smtp和邮件方面不熟悉,是不是需要用到MIME之类的东西啊。

论坛徽章:
0
2 [报告]
发表于 2006-05-16 09:14 |只看该作者
给你参考一下,这个可以发送附件。

其中 MIMEText 类型可以解决你的问题。


[code]
msg=MIMEMultipart()
msg['From'] = '''pppp@sina.com.cn'''
msg['To'] = '''happy@gmail.com'''
msg['Subject'] =  '''corp backup files: ''' + target_bakfile
msg['Reply-To'] = '''pppp@sina.com.cn'''
msg['Date'] = time.ctime(time.time())
msg['X-Priority'] =  '''3'''
msg['X-MSMail-Priority'] =  '''Normal'''
msg['X-Mailer'] =  '''Microsoft Outlook Express 6.00.2900.2180'''
msg['X-MimeOLE'] =  '''Produced By Microsoft MimeOLE V6.00.2900.2180'''
body=MIMEText(target,_subtype='plain',_charset='utf-8')
msg.attach(body)

fp = open(target,'rb')
ctype,encoding = mimetypes.guess_type(target)
if ctype is None or encoding is not None:
   ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)
m = MIMEBase(maintype,subtype)
m.set_payload(fp.read())
fp.close()
Encoders.encode_base64(m)                                             
m.add_header('Content-disposition','attachment; filename="%s"' % os.path.basename(target))
msg.attach(m)

smtpServer = smtplib.SMTP('smtp.sina.com.cn')
smtpServer.login('username','pass')
smtpServer.sendmail(msg['From'],msg['To'],msg.as_string())
smtpServer.close()
[/code]

论坛徽章:
0
3 [报告]
发表于 2006-05-17 12:37 |只看该作者
试试加上对文本类型信息的说明:

纯文本:
message["Content-Type"] = "text/plain"
HTML的:
message["Content-Type"] = "text/html"

论坛徽章:
0
4 [报告]
发表于 2006-05-18 09:29 |只看该作者
赞,我用楼上的方法试了下,果然可以..
多谢啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP