python程序发送email的源码-创新互联
把开发过程经常用的代码段收藏起来,下边代码段是关于python程序发送email的的代码,应该是对码农们有一些好处。
创新互联专业为企业提供新野网站建设、新野做网站、新野网站设计、新野网站制作等企业网站建设、网页设计与制作、新野企业网站模板建站服务,十载新野做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
server = 'smtp.gmail.com:587';
#imports
from time import sleep;
import smtplib;
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText;
from email.mime.multipart import MIMEMultipart;
# takes addresses to, from cc and a subject
def create_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;
return msg;
# if mode = 0 sends to and cc
# if mode = 1 sends to bcc
def send_email(smtp_address, usr, password, msg, mode):
server = smtplib.SMTP(smtp_address);
server.ehlo();
server.starttls();
server.ehlo();
server.login(username,password);
if (mode == 0 and msg['To'] != ''):
server.sendmail(msg['From'],(msg['To']+msg['Cc']).split(","), msg.as_string());
elif (mode == 1 and msg['Bcc'] != ''):
server.sendmail(msg['From'],msg['Bcc'].split(","),msg.as_string());
elif (mode != 0 and mode != 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
def compose_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
for text in body:
attach_text(msg, text[0], text[1]);
# add files
if (files != ''):
file_list = files.split(',');
for afile in file_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
def attach_text(msg, atext, mode):
part = MIMEText(atext, get_mode(mode));
msg.attach(part);
# util function to get mode type
def get_mode(mode):
if (mode == 0):
mode = 'plain';
elif (mode == 1):
mode = 'html';
else:
print 'error in text kind'; print 'email cancled'; exit();
return mode;
# attach file
# takes the message and a file name and attaches the file to the message
def attach_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
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前标题:python程序发送email的源码-创新互联
分享URL:http://pwwzsj.com/article/dshheh.html