data.py
import os
user=
password =
"授權碼"
sender =
"傳送者"
receive =
["接收者1"
,"接收者2"
]content =
if __name__ ==
"__main__"
:print
(content[
"path"
])
send_email.py
import smtplib
from email.mime.text import mimetext
from email.mime.multipart import mimemultipart
from email.header import header
class
sendemail
(object):
def__init__
(self,user,password,sender,receive,kwd)
:"""
@user: 登入使用者名稱
@password: 密碼
@sender: 傳送人
@receive: 接收人的使用者名稱
@args: 傳送內容--為多個引數,標題和正文, 附件
"""self.user = user
self.password = password
self.sender = sender
self.receive = receive
self.sendcontent = kwd
"""傳送郵箱伺服器
"""self.smtpserver =
"smtp.163.com"
def__makeattachment
(self)
:"""構造附件"""
with
open
(self.sendcontent[
"path"],
"rb"
)as f:
att = f.read(
) content=mimetext(att,
"base64"
,"utf-8"
) content[
"conten-type"]=
content[
"content-disposition"]=
"attachment;filename='{}'"
.format
("excel.xls"
)return content
def__content
(self)
:"""
傳送郵箱正文
"""""" 判斷是否**
"""iflen
(self.receive)
>
0and
len(self.receive)
<=1:
""" 發給單個人 """
msg = mimemultipart(
) msg.attach(mimetext(self.sendcontent[
"content"],
"html"
,"utf-8"))
msg[
"subject"
]=header(self.sendcontent[
"title"],
"utf-8"
) msg[
"from"
]= self.sender
msg[
"to"
]= self.receive[0]
msg.attach(self.__makeattachment())
elif
len(self.receive)
>1:
""" 發給多人 """
msg = mimemultipart(
) msg.attach(mimetext(self.sendcontent[
"content"],
"html"
,"utf-8"))
msg[
"subject"
]=header(self.sendcontent[
"title"],
"utf-8"
) msg[
"from"
]= self.sender
msg[
"to"]=
",".join(self.receive)
msg.attach(self.__makeattachment())
else
:print
("沒有填寫傳送人"
)raise
return msg
def__emaillogin
(self)
:"""
1、指定 ssl 協議埠號 465
2、向伺服器標識使用者的身份
3、伺服器返回結果確認
4、登入郵箱伺服器使用者名稱和密碼
"""smtp = smtplib.smtp_ssl(self.smtpserver,
465)
smtp.helo(self.smtpserver)
smtp.ehlo(self.smtpserver)
smtp.login(self.user,self.password)
return smtp
defsendemail
(self)
:"""
傳送郵件
"""content = self.__content(
) login = self.__emaillogin(
) login.sendmail(self.sender, self.receive,content.as_string())
login.quit(
)if __name__ ==
"__main__"
:from data import
* mail = sendemail(user,password,sender,receive,content)
mail.sendemail(
)
html傳送郵件 Python傳送郵件(三十)
簡單郵件傳輸協議 smtp 是一種協議,用於在郵件伺服器之間傳送電子郵件和路由電子郵件。python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它...
python 傳送郵件
coding utf 8 import smtplib from email.mime.text import mimetext from email.header import header 檔案形式的郵件 def email file mail host smtp.qq.com 郵箱伺服器 ma...
python 傳送郵件
smtp mail transfer protocol 即簡單郵件傳輸協議,它是一組用於由源位址到目的位址傳送郵件的規則,由它來控制信件的中轉方式。python的smtplib提供了一種很方便的途徑傳送電子郵件。它對smtp協議進行了簡單的封裝。直接貼 coding utf 8 import smt...