python2 7實現郵件傳送功能

2022-10-04 18:33:11 字數 4207 閱讀 4010

要想實現乙個能夠傳送帶有文字、、附件的python程式,首先要熟悉兩大模組:

email以及smtplib

然後對於mime(郵件擴充套件)要有一定認知,因為有了擴充套件才能傳送附件以及這些**或者非文字資訊

最後乙個比較細節的方法就是mimemultipart,要理解其用法以及對應引數所實現的功能區別

傳送郵件三部曲:

建立協議物件

連線郵件伺服器

登陸並傳送郵件

from email.header import header

from email.mime.base import mimebase

from email.mime.text import mimetext

import mimetypes

from email.mime.multipart import mimemultipart

import os

import smtplib

from email import encoders as email_encoders

class message(object):

def __init__(self, from_addr, to_addr, subject="", html="", text=none, cc_addr=, attachment=):

self.from_addr = from_addr

self.subject = subject

if to_addr:

if isinstance(to_addr, list):

self.to_addr = to_addr

else:

self.to_addr = [d for d in to_addr.split(',')]

else:

self.to_addr =

if cc_addr:

if isinstance(cc_addr, list):

self.cc程式設計客棧_addr = cc_addr

else:

self.cc_addr = [d for d in cc_addr.split(',')]

else:

self.cc_addr =

if html is not none:

self.body = html

self.body_type = "html"

else:

self.body = text

self.body_type = "plain"

self.parts =

if isinstance(attachment, list):

for file in attachment:

self.add_attachment(file)

def add_attachment(self, file_path, mimetype=none):

"""if *mimetype* is not specified an attempt towww.cppcns.com guess it is made. if nothing

is guessed then `application/octet-stream` is used.

"""if not mimetype:

mimetype, _ = mimetypes.guess_type(file_path)

if mimetype is none:

mimetype = 'application/octet-stream'

type_maj, type_min = mimetype.split('/')

with open(file_path, 'rb') as fh:

part_data = fh.read()

part = mimebase(type_maj, type_min)

part.set_payload(part_data)

email_encoders.encode_base64(part)

part_filename = os.path.basename(file_path)

part.add_header('content-disposition', 'attachment; filename="%s"'

% part_filename)

part.add_header('content-id', part_filename)

self.parts.append(part)

def __to_mime_message(self):

"""returns the message as

:py:class:`email.mime.multipart.mimemultipart`."""

## to get the message work in ios, you need use multipart/related, not the multipart/alterna程式設計客棧tive

msg = mimemultipart('related')

msg['subject'] = self.subject

msg['from'] = self.from程式設計客棧_addr

msg['to'] = ','.join(self.to_addr)

if len(self.cc_addr) > 0:

msg['cc'] = ','.join(self.cc_addr)

body = mimetext(self.body, self.body_type)

msg.attach(body)

# add attachment

for part in self.parts:

msg.attach(part)

return msg

def send(self, smtp_server='localhost'):

smtp = smtplib.smtp()

smtp.connect(smtp_server)

smtp.sendmail(from_addr=self.from_addr, to_addrs=self.to_addr + self.cc_addr, msg=self.__to_mime_message().as_string())

smtp.close()

對於實際傳送程式,要注意個引數的型別,比如from_addr是字串,to_addr和cc_addr以及attachment都是列表

####要特別注意這裡,正文插入的特殊格式!!!

here is the latest ***** link from the economist, you can click github.io/iooi/index.html" rel="external nofollow" >go for a full view!

best regards

any question please mail to lockey23.

sent at {} pst

""".format(time_now)

subject = '[halo] - ' + 'a new ***** published!'

msg = message(from_a程式設計客棧ddr=from_addr,

to_addr=[mail_to],

cc_addr=[mail_to],

subject=subject,

attachment=attach_files,

html=mail_msg

)msg.send()

if __name__ == '__main__':

send_go()

對於測試程式我們命名為sendgo.py,執行測試程式

~$ python sendgo.py

本文標題: python2.7實現郵件傳送功能

本文位址:

python2 7換行 Python2 7基礎語法

1.建立檔案xx.py usr bin python3 print hello,world 2.linux下執行 python hello.py 3.編碼 預設字串unicode 設定編碼 coding utf 8 4.識別符號 第乙個字元必須是字母表中字母或下劃線 識別符號的其他的部分有字母 數字...

python2 7是什麼 python2 7是什麼

pyton2.7是python在2010年發布的乙個版本。python 是乙個高層次的結合了解釋性 編譯性 互動性和物件導向的指令碼語言,具有很強的可讀性,相比其他語言經常使用英文關鍵字,其他語言的一些標點符號,它具有比其他語言更有特色語法結構。下面來解釋一下python這門語言 python是一種...

python2 7中文編碼 python2 7

我從外部api中獲得了乙個字串 u4ece u8d77 u70b9 u5411 u6b63 u5357 u65b9 u5411 u51fa u53d1,u884c u9a76170 u7c73,u76f4 u884c u8fdb u5165 u4e2d u5173 u6751 u4e1c u8def...