python 實現發郵件

2021-08-21 05:59:28 字數 3020 閱讀 3345

#coding:utf-8

from email.header import header

from email.mime.text import mimetext

from email.utils import parseaddr, formataddr

import smtplib

class post_email(object):

def __init__(self,to_addrs):

# 發件人位址

self.from_addr = '[email protected]'

# 郵箱密碼

self.password = '66666666'

self.smtp_server = 'smtp.163.com'

# 收件人位址

self.to_addrs = ['[email protected]']#

self.mail_text='python爬蟲執行異常,異常資訊為遇到http 403'

def _format_addr(self,s):

name, addr = parseaddr(s)

return formataddr((header(name, 'utf-8').encode(), addr))

def send_mail(self):

#設定郵件資訊

msg = mimetext(self.mail_text, 'plain', 'utf-8')

msg['subject'] = header('一號爬蟲執行狀態', 'utf-8').encode()

msg['from'] = self._format_addr('一號爬蟲 <%s>' % self.from_addr)

for to_addr in to_addrs:

msg['to'] = self._format_addr('管理員 <%s>' % to_addr)

print(to_addr)

#傳送郵件

server = smtplib.smtp(self.smtp_server, 25)

server.login(self.from_addr, self.password)

server.sendmail(self.from_addr, [to_addr], msg.as_string())

server.quit()

if __name__=='__main__':

try:

to_addrs=['66666666.com','[email protected]']

send_mail=post_email(to_addrs)

send_mail.send_mail()

except exception as e:

print(e)

print('a')

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

import mysqldb

# 使用cursor()方法獲取操作游標

db = mysqldb.connect("localhost","root","118169","test_demo",charset='utf8' )

cursor = db.cursor()

querysql="""select count(0) from employee """

aa= cursor.execute(querysql)

querysql1="""show tables; """

cursor.execute(querysql1)

results=cursor.fetchall()

# print results

#開啟資料庫連線

# 第三方 smtp 服務

mail_host="smtp.163.com" #設定伺服器

mail_user="[email protected]" #使用者名稱

mail_pass="" #口令

sender = '@163.com'

receivers = '@163.com' # 接收郵件,可設定為你的qq郵箱或者其他郵箱

message = mimetext('工程報錯請及時處理', 'plain', 'utf-8')

# print type(message)

# print message

message['from'] = header("oozie",'utf-8')

message['to'] = header("負責人", 'utf-8')

subject = '監控mysql資料量郵件'

message['subject'] = header(subject, 'utf-8')

print 'aa:'+str(aa)

for i in results:

querysql = "select count(0) from %s "%i

aa=cursor.execute(querysql)

print 'table'+'%s'%i +' data is:'+'%d'%aa

if aa == 0:

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host, 25) # 25 為 smtp 埠號

smtpobj.login(mail_user, mail_pass)

smtpobj.sendmail(sender, receivers,

message.as_string())

print "郵件傳送成功"

except smtplib.smtpexception:

print "error: 無法傳送郵件"

else:

print '資料正常'

Python 實現發郵件

usr bin python coding utf 8 import smtplib from email.mime.text import mimetext from email.header import header 第三方 smtp 服務 mail host smtp.163.com 設定伺...

python實現發郵件

話不多說,直接上 import smtplib from email.mime.text import mimetext from email.header import header def sendmail sender 163.com 發件人賬號 password 發件人密碼 receiver...

python 發郵件 python發郵件

python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它來傳送電子郵件 import smtplib smtpobj smtplib.smtp ...