日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python发邮件详解_python实现发送邮件详解

發(fā)布時間:2023/12/9 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python发邮件详解_python实现发送邮件详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

[Python]代碼

#_*_encoding:utf-8_*_

#script for python3.2

#-------------------------------------------------------------------------------

# Name: 發(fā)送郵件

# Purpose:

#

# Author: QiuChangJie

#

# Created: 10/09/2012

# Copyright: (c) cj.qiu 2012

# Licence:

#-------------------------------------------------------------------------------

import os

import smtplib

import mimetypes

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

from email.mime.text import MIMEText

from email.mime.audio import MIMEAudio

from email.mime.image import MIMEImage

from email.encoders import encode_base64

MAIL_163_USER = "[email?protected]"

MAIL_163_PWD = "test"

MAIL_YEAH_USER = "[email?protected]"

MAIL_YEAH_PWD = "test"

MAIL_GOOGLE_HOST = "smtp.gmail.com"

MAIL_163_HOST = "smtp.163.com"

MAIL_YEAH_HOST = "smtp.yeah.com"

RECIPIENT = ["[email?protected]"]

ATTACHMENTS = []

class QMail():

def __init__(self, user, pwd, host):

self.mail_user = user

self.mail_pwd = pwd

self.mail_server = smtplib.SMTP()

self.mail_server.connect(host)

self.mail_server.ehlo()

self.mail_server.starttls()

self.mail_server.ehlo()

self.mail_server.login(self.mail_user, self.mail_pwd)

def __del__(self):

self.mail_server.close()

def send_mail(self, recipient, subject, text, att_files=[]):

msg = MIMEMultipart()

msg["From"] = self.mail_user

msg["Subject"] = subject

msg["To"] = ",".join(recipient)

msg.attach(MIMEText(text))

if len(att_files) > 0:

for file_name in att_files:

msg.attach(self.get_attachment(file_name))

self.mail_server.sendmail(self.mail_user, recipient, msg.as_string())

def get_attachment(self, file_name):

content_type, encoding = mimetypes.guess_type(file_name)

if content_type is None or encoding is not None:

content_type = "application/octet-stream"

main_type, sub_type = content_type.split('/', 1)

file = open(file_name, "rb")

if main_type == "text":

attachment = MIMEText(file.read())

elif main_type == 'message':

attachment = email.message_from_file(file)

elif main_type == 'image':

attachment = MIMEImage(file.read(), _subType=sub_type)

elif main_type == 'audio':

attachment = MIMEAudio(file.read(), _subType=sub_type)

else:

attachment = MIMEBase(main_type, sub_type)

attachment.set_payload(file.read())

encode_base64(attachment)

file.close()

attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_name))

return attachment

def test():

mail = QMail("[email?protected]", "test", MAIL_163_HOST)

mail.send_mail(["[email?protected]"], "sub_test", "text_test", r"G:\WorkSpace\Doing\CMMI文檔模板.dot")

if __name__ == '__main__':

test()

總結(jié)

以上是生活随笔為你收集整理的python发邮件详解_python实现发送邮件详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。