python发邮件的代码
# -*- coding: cp936 -*-
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
att = MIMEText(open('e:\\test.txt').read(), 'base64', 'gb2312')
att["Content-Type"] = 'application/octet-stream'
#att["Content-Disposition"] = 'attachment; filename="e:\\test.txt"' 以附件形式发送
msg.attach(att)
mailto_list=["wsywfw3@163.com"]
#####################
# 设置服务器,用户名、口令以及邮箱的后缀
mail_host="smtp.163.com"
mail_user="wsywfw3@163.com"
mail_pass="7451920"
mail_postfix="163.com"
######################
def send_mail(to_list,sub,content):
'''
to_list:发给谁
sub:主题
content:内容
send_mail("aaa@126.com","sub","content")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg = att
msg['Subject'] = sub
msg['from'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto_list,"subject","content"):
相关文档:
os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,如posix或NT系统平台,os模块会根据不同的平台进行相应的操作.本节内容将对os模块提供的函数进行详细的解读.
1.1 文件操作函数
1.1.1 open()函数提供创建、打开、修改文件的功能。
Example 1-1. Using the os Module to Rename ......
在C语言中,三目运算经常用到(cond?a:b),非常的简洁,而在Python中不支持这种语法。
但是,可以用Python中and or来实现(这里是有陷阱的,下面会讲到)
我们来看下面几个表达式
>>> False
or 1
1
>>> False
or 0
0
>>> True or
0
True
>>> True
and 1
1
>> ......
上一篇中我们已经了解如何在Python程序和C模块之间进行值的相互传递,现在我们来进入实作阶段,看看如何将一个C语言开发的开源mp3编解码库LAME包装为一个Python下可以使用的扩展模块。首先去http://lame.sourceforge.net/download.php下载LAME的源代码,然后切换到root用户编译源代码,./configure
make
make instal ......
在上一篇中我们已经使用c语言实现了一个最简单的扩展模块,这一篇中将在其基础上进行功能的丰富。首先来考虑如何从外部的Python向C模块传递进参数,foo_bar2展示了如何向C模块传递整数,浮点数,字符串三个参数,其中"ids"指明了传入参数的数据类型。PyArg_ParseTuple负责对args进行解析,若解析失败则返回0.代码#include&n ......
今天一上课,大牛老师就给大家出了一道题:
编程:请从字符串“goOoOogle”中找出以“O”开头,并以“O”结束的部分。
“这还不简单,看我的”小菜不一会儿就给出了答案:
>>> s="goOoOogle"
>>> s.find("O")
2
>>> s.find("O",3)
4
>>& ......