要想實現(xiàn)一個能夠發(fā)送帶有文本、圖片、附件的python程序,首先要熟悉兩大模塊:
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的灌陽網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!email以及smtplib
然后對于MIME(郵件擴展)要有一定認知,因為有了擴展才能發(fā)送附件以及圖片這些媒體或者非文本信息
最后一個比較細節(jié)的方法就是MIMEMultipart,要理解其用法以及對應(yīng)參數(shù)所實現(xiàn)的功能區(qū)別
發(fā)送郵件三部曲:
創(chuàng)建協(xié)議對象
連接郵件服務(wù)器
登陸并發(fā)送郵件
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 to 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/alternative 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()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。