python直聯(適合於企業內部的內網郵箱)發送郵件(帶附件)、發送給多人、抄送給多人的示例

# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# 發送純文本格式的郵件
message = MIMEMultipart('related')
message.attach(MIMEText('there is some error or warning in your project', 'plain', 'utf-8'))
att1 = MIMEText(open('lintResult.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 這裏的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字
att1["Content-Disposition"] = 'attachment; filename="aaaa"'
message.attach(att1)
#發送郵箱地址
sender = '[email protected]'
#登陸密碼
password = 'eeee'
#收件箱地址
receiver = '[email protected], [email protected]'
#抄送
acc = '[email protected], [email protected]

#smtp服務器
smtp_server = 'ffff'
#發送郵箱地址
message['From'] = sender
#收件箱地址
message['To'] = receiver
message['Cc'] = acc
#主題
message['Subject'] = 'this is test email for gggg project'
server = smtplib.SMTP(smtp_server,25)

server.login(sender,password)
server.sendmail(sender,receiver,message.as_string())
server.quit()
 

很多企業內網或者外網郵箱都是通過郵箱服務器來實現收發郵件並不提供ssh接口,因此需要直聯25端口來發送郵件,其中aaaa,bbbb,cccc等需要讀者自行替換爲所需的東東,其中有些換行或者空格是我後來加入的,可能會出錯,需要讀者重新替換下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章