python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装,需要的朋友可以参考下
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
Python创建 SMTP 对象语法如下:
1 2 | import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
|
参数说明:
host: SMTP 服务器主机。 你可以指定主机的ip地址或者域名如:w3cschool.cc,这个是可选参数。
port: 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下SMTP端口号为25。
local_hostname: 如果SMTP在你的本机上,你只需要指定服务器地址为 localhost 即可。
Python SMTP对象使用sendmail方法发送邮件,语法如下:
1 | SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
|
参数说明:
from_addr: 邮件发送者地址。
to_addrs: 字符串列表,邮件发送地址。
msg: 发送消息
这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。
实例
以下是一个使用Python发送邮件简单的实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import smtplib
sender = 'from@fromdomain.com'
receivers = [ 'to@todomain.com' ]
message =
try :
smtpObj = smtplib.SMTP( 'localhost' )
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
|
使用Python发送HTML格式的邮件
Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import smtplib
from email.mime.text import MIMEText
mailto_list = [ "YYY@YYY.com" ]
mail_host = "smtp.XXX.com"
mail_user = "XXX"
mail_pass = "XXXX"
mail_postfix = "XXX.com"
def send_mail(to_list,sub,content):
me = "hello" + "<" + mail_user + "@" + mail_postfix + ">"
msg = MIMEText(content,_subtype = 'html' ,_charset = 'gb2312' )
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, "hello" , "<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>" ):
print ( "发送成功" )
else :
print ( "发送失败" )
|
或者你也可以在消息体中指定Content-type为text/html,如下实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import smtplib
message =
try :
smtpObj = smtplib.SMTP( 'localhost' )
smtpObj.sendmail(sender, receivers, message)
print ( "Successfully sent email" )
except SMTPException:
print ( "Error: unable to send email" )
|
Python发送带附件的邮件
发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
msg = MIMEMultipart()
att1 = MIMEText( open ( 'd:\\123.rar' , 'rb' ).read(), 'base64' , 'gb2312' )
att1[ "Content-Type" ] = 'application/octet-stream'
att1[ "Content-Disposition" ] = 'attachment; filename="123.doc"'
msg.attach(att1)
att2 = MIMEText( open ( 'd:\\123.txt' , 'rb' ).read(), 'base64' , 'gb2312' )
att2[ "Content-Type" ] = 'application/octet-stream'
att2[ "Content-Disposition" ] = 'attachment; filename="123.txt"'
msg.attach(att2)
msg[ 'to' ] = 'YYY@YYY.com'
msg[ 'from' ] = 'XXX@XXX.com'
msg[ 'subject' ] = 'hello world'
try :
server = smtplib.SMTP()
server.connect( 'smtp.XXX.com' )
server.login( 'XXX' , 'XXXXX' )
server.sendmail(msg[ 'from' ], msg[ 'to' ],msg.as_string())
server.quit()
print '发送成功'
except Exception, e:
print ( str (e))
|
以下实例指定了Content-type header 为 multipart/mixed,并发送/tmp/test.txt 文本文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import smtplib
import base64
filename = "/tmp/test.txt"
fo = open (filename, "rb" )
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent)
sender = 'webmaster@tutorialpoint.com'
reciever = 'amrood.admin@gmail.com'
marker = "AUNIQUEMARKER"
body =
part1 =
% (marker, marker)
part2 =
% (body,marker)
part3 =
% (filename, filename, encodedcontent, marker) message = part1 + part2 + part3
try :
smtpObj = smtplib.SMTP( 'localhost' )
smtpObj.sendmail(sender, reciever, message)
print ( "Successfully sent email" )
except Exception:
print ( "Error: unable to send email" )
|
更多关于Python 使用SMTP发送邮件的代码小结请关注PHP中文网(www.php.cn)其他文章!