python實現傳送郵件

2021-08-04 08:14:14 字數 1566 閱讀 3167

# coding=utf-8

import os

import socket

import time

from email.mime.text import mimetext

from email.header import header

from smtplib import smtp_ssl

from threading import timer

def sendmail(qq_email,port):

#qq郵箱smtp伺服器

host_server = 'smtp.qq.com'

#sender_qq為發件人的qq號碼

sender_qq = '*********x'

#pwd為qq郵箱的授權碼

pwd = '*********xx'

#發件人的郵箱

sender_qq_mail = '******[email protected]'

#收件人郵箱

receiver = qq_email;

#郵件的正文內容

mail_content = '內容測試'

#郵件標題

mail_title = str(port)+' 這是標題 !!'

#ssl登入

smtp = smtp_ssl(host_server)

#set_debuglevel()是用來除錯的。引數值為1表示開啟除錯模式,引數值為0關閉除錯模式

smtp.set_debuglevel(0)

smtp.ehlo(host_server)

smtp.login(sender_qq, pwd)

msg = mimetext(mail_content, "plain", 'utf-8')

msg["subject"] = header(mail_title, 'utf-8')

msg["from"] = sender_qq_mail

msg["to"] = receiver

smtp.sendmail(sender_qq_mail, receiver, msg.as_string())

smtp.quit()

def isopen(ip,port):

s = socket.socket(socket.af_inet,socket.sock_stream)

try:

s.connect((ip,int(port)))

s.shutdown(2)

print '%d is open' % port

return false

except:

print '%d is down' % port

sendmail('*********@qq.com',port);

if __name__ == '__main__':

while true:

isopen("127.0.0.1",8888)

isopen("127.0.0.1",8887)

time.sleep(9000)

#sendmail();

python 實現傳送郵件

可採用email模組傳送電子郵件附件。傳送乙個未知mime型別的檔案附件其基本思路如下 1.構造mimemultipart物件做為根容器 2.構造mimetext物件做為郵件顯示內容並附加到根容器 3.構造mimebase物件做為檔案附件內容並附加到根容器 a.讀入檔案內容並格式化 b.設定附件頭 ...

python實現傳送郵件

有時我們需要程式在執行出現問題時傳送郵件通知我們,在這裡寫了乙個使用qq傳送的python指令碼,也是綜合了網上的資源 然而網上的案例好像都不能用 搞得,當然只要把主機埠啥的改一下就能使用其他郵箱了,如下 usr bin python coding utf 8 import smtplib from...

Python實現傳送郵件

coding utf 8 引入相關的模組 import smtplib from email.mime.text import mimetext from email.mime.image import mimeimage from email.header import header from e...