#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib # smtplib這個模組是管發郵件
from email.mime.text import mimetext # 構造郵件內容
from email.mime.multipart import mimemultipart # 發帶附件的郵件用的
class sendmail(object):
def __init__(self, username, passwd, recv, title, content, file=none, email_host='smtp.163.com', port=25):
'''傳送郵件封裝
:param username: 傳送者賬號
:param passwd: 傳送者郵箱的授權碼
:param recv: 收件人
:param title: 郵件主題
:param content:郵件正文的內容
:param file:附件
:param email_host:郵箱伺服器位址
:param port:埠
'''self.username = username
self.passwd = passwd
self.recv = recv
self.title = title
self.content = content
self.file = file
self.email_host = email_host
self.port = port
def send_mail(self):
# 傳送內容的物件
msg = mimemultipart()
# 處理附件
if self.file:
att = mimetext(open(self.file).read())
att["content-disposition"] = 'attachment; filename="%s"' % self.file
msg.attach(att)
msg.attach(mimetext(self.content)) # 郵件正文的內容
msg['subject'] = self.title # 郵件主題
msg['from'] = self.username # 傳送者賬號
msg['to'] = self.recv # 接收者賬號列表
self.smtp = smtplib.smtp(self.email_host, port=self.port) # 傳送郵件伺服器的物件
self.smtp.login(self.username, self.passwd)
try:
self.smtp.sendmail(self.username, self.recv, msg.as_string())
except exception as e:
print('出錯了。。', e)
else:
print('傳送成功!')
def __del__(self):
self.smtp.quit()
if __name__ == '__main__':
m = sendmail(
username='[email protected]', passwd='123123', recv='[email protected]',
title='傳送郵件-附件', content='啊哈哈哈哈', file='cs.txt'
)m.send_mail()
python傳送帶附件郵件
1.不包括附件的郵件 coding utf 8 import smtplib import string 傳送普通的文字郵件 郵件smtp的位址 host smtp.163.com 定義郵件的標題 subject 這是郵件標題 發件人 from 163.com 收件人 to qq.com 傳送的郵件...
Python 傳送帶附件的郵件
usr bin python3 import smtplib from email.mime.text import mimetext from email.mime.multipart import mimemultipart from email.header import header sen...
python傳送帶附件的郵件
coding utf8 import os,smtplib from email.mime.multipart import mimemultipart from email.mime.text import mimetext from email.header import header clas...