代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
发送email
作者:
/ 发布于
2014/1/17
/
661
username='***'; password='***'; server='smtp.gmail.com:587'; #imports fromtimeimportsleep; importsmtplib; fromemail.mime.applicationimportMIMEApplication fromemail.mime.textimportMIMEText; fromemail.mime.multipartimportMIMEMultipart; # create msg - MIME* object # takes addresses to, from cc and a subject # returns the MIME* object defcreate_msg(to_address, from_address='', cc_address='', bcc_address='', subject=''): msg=MIMEMultipart(); msg['Subject']=subject; msg['To']=to_address; msg['Cc']=cc_address; msg['From']=from_address; returnmsg; # sharejs.com send an email # takes an smtp address, user name, password and MIME* object # if mode = 0 sends to and cc # if mode = 1 sends to bcc defsend_email(smtp_address, usr, password, msg, mode): server=smtplib.SMTP(smtp_address); server.ehlo(); server.starttls(); server.ehlo(); server.login(username,password); if(mode==0andmsg['To'] !=''): server.sendmail(msg['From'],(msg['To']+msg['Cc']).split(","), msg.as_string()); elif(mode==1andmsg['Bcc'] !=''): server.sendmail(msg['From'],msg['Bcc'].split(","),msg.as_string()); elif(mode !=0andmode !=1): print'error in send mail bcc';print'email cancled'; exit(); server.quit(); # compose email # takes all the details for an email and sends it # address format: list, [0] - to # [1] - cc # [2] - bcc # subject format: string # body format: list of pairs [0] - text # [1] - type: # 0 - plain # 1 - html # files is list of strings defcompose_email(addresses, subject, body, files): # addresses to_address=addresses[0]; cc_address=addresses[1]; bcc_address=addresses[2]; # create a message msg=create_msg(to_address, cc_address=cc_address , subject=subject); # add text fortextinbody: attach_text(msg, text[0], text[1]); # add files if(files !=''): file_list=files.split(','); forafileinfile_list: attach_file(msg, afile); # send message send_email(server, username, password, msg,0); # check for bcc if(bcc_address !=''): msg['Bcc']=bcc_address; send_email(server, username, password, msg,1); print'email sent' # attach text # attaches a plain text or html text to a message defattach_text(msg, atext, mode): part=MIMEText(atext, get_mode(mode)); msg.attach(part); # util function to get mode type defget_mode(mode): if(mode==0): mode='plain'; elif(mode==1): mode='html'; else: print'error in text kind';print'email cancled'; exit(); returnmode; # attach file # takes the message and a file name and attaches the file to the message defattach_file(msg, afile): part=MIMEApplication(open(afile,"rb").read()); part.add_header('Content-Disposition','attachment', filename=afile); msg.attach(part); #to be tested... compose_email(['cpt@thelivingpearl.com','',''], 'test v.5.0', [['some text goes here...\n',0]], ''); #compose_email can take the following arguments: # 1. to recipients (separated by a comma) # 2. cc recipients (separated by a comma) # 3. bcc recipients (separated by a comma) # 4. subject # 5. a list with message and mode (plain txt or html) # 6. files to be attached
试试其它关键字
发送email
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3