自動傳送郵件功能是我們經常要用到的,比如每天定時統計報表資訊,然後自動傳送給運營人員,協助運營人員進行業務資料分析。本文是用python寫的乙個自動傳送郵件的指令碼,呼叫函式時,直接把發件人郵箱位址、密碼、收件人郵箱位址、郵件標題、內容等資訊傳遞給函式,即可實現自動傳送郵件功能。
指令碼**如下:
[python]view plain
copy
print?
# -*- coding: utf-8 -*-
import sys, os, re, urllib, urlparse
import smtplib
import traceback
from email.mime.text import mimetext
from email.mime.multipart import mimemultipart
def sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password): 」」』
@subject:郵件主題
@msg:郵件內容
@toaddrs:收信人的郵箱位址
@fromaddr:發信人的郵箱位址
@smtpaddr:smtp服務位址,可以在郵箱看,比如163郵箱為smtp.163.com
@password:發信人的郵箱密碼
」』mail_msg = mimemultipart()
ifnot isinstance(subject,unicode):
subject = unicode(subject, 』utf-8』)
mail_msg[』subject』] = subject
mail_msg[』from』] =fromaddr
mail_msg[』to』] = 『,』.join(toaddrs)
mail_msg.attach(mimetext(msg, 』html』, 『utf-8』))
try:
s = smtplib.smtp()
s.connect(smtpaddr) #連線smtp伺服器
s.login(fromaddr,password) #登入郵箱
s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) #傳送郵件
s.quit()
except exception,e:
「error: unable to send email」
print traceback.format_exc()
if __name__ == 『__main__』:
fromaddr = 」******[email protected]」
smtpaddr = 」smtp.163.com」
toaddrs = [」******[email protected]」,「*********@163.com」]
subject = 」測試郵件」
password = 」******xx」
msg = 」測試一下」
sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password)
# -*- coding: utf-8 -*-
import sys, os, re, urllib, urlparse
import smtplib
import traceback
from email.mime.text import mimetext
from email.mime.multipart import mimemultipart
def sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password):
'''@subject:郵件主題
@msg:郵件內容
@toaddrs:收信人的郵箱位址
@fromaddr:發信人的郵箱位址
@smtpaddr:smtp服務位址,可以在郵箱看,比如163郵箱為smtp.163.com
@password:發信人的郵箱密碼
'''mail_msg = mimemultipart()
if not isinstance(subject,unicode):
subject = unicode(subject, 'utf-8')
mail_msg['subject'] = subject
mail_msg['from'] =fromaddr
mail_msg['to'] = ','.join(toaddrs)
mail_msg.attach(mimetext(msg, 'html', 'utf-8'))
try:
s = smtplib.smtp()
s.connect(smtpaddr) #連線smtp伺服器
s.login(fromaddr,password) #登入郵箱
s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) #傳送郵件
s.quit()
except exception,e:
print "error: unable to send email"
print traceback.format_exc()
if __name__ == '__main__':
fromaddr = "******[email protected]"
smtpaddr = "smtp.163.com"
toaddrs = ["******[email protected]","*********@163.com"]
subject = "測試郵件"
password = "******xx"
msg = "測試一下"
sendmail(subject,msg,toaddrs,fromaddr,smtpaddr,password)
python自動傳送郵件指令碼
因為最近專案需要,所以嘗試寫了個自動傳送郵件的指令碼。主要用到的三方庫 smtplib email 對你沒看錯,就是有這個庫 from smtplib import smtp from email.mime.text import mimetext from email.header import ...
Python 指令碼傳送郵件
使用python完成郵件自動傳送 usr bin python coding utf 8 import smtplib from email.header import header from email.mime.text import mimetext class email sender ob...
python郵件傳送指令碼
複製 usr bin python coding utf 8 author dengyike date 2010 09 28 version 1.0 description auto sending email with attachment file import email import mim...