> 使用邮箱监控网站后台
>
> 网站地址:[ascii-art.lcx-blog.top](http://ascii-art.lcx-blog.top/)
>
> 代码仓库地址:https://github.com/ChenXu-Li/AsciiArtWebsite
## 准备
1. 注册163邮箱
2. 开启smtp服务,获得授权码
![image-20220925164235608](http://cdn.lcx-blog.top/img/image-20220925164235608.png)
3. 查阅163邮箱提供的smtp服务器地址和端口号`smtp.163.com,994`
4. 导入python自带的包
```python
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
```
## 发送文本
以下为自己发送给自己纯文本邮件的demo:
``` python
def send_txt_email(a1,receiver="asciiart@163.com"):
subject = "滴滴答答"#邮件标题
sender = "asciiart@163.com"#发送方
content = "hello world {}!".format(a1)
recver = receiver#接收方
password = "****************"#授权码
message = MIMEText(content,"plain","utf-8")
#content 发送内容 "plain"文本格式 utf-8 编码格式
message['Subject'] = subject #邮件标题
message['To'] = recver #收件人
message['From'] = sender #发件人
smtp = smtplib.SMTP_SSL("smtp.163.com",994) #实例化smtp服务器
smtp.login(sender,password)#发件人登录
smtp.sendmail(sender,[recver],message.as_string()) #as_string 对 message 的消息进行了封装
smtp.close()
send_txt_email("李辰旭")
```
![image-20220925181828873](http://cdn.lcx-blog.top/img/image-20220925181828873.png)
## 发送图片
可以以html格式来发送文本,附加图片
```python
def send_img_email(a1,a2,img_path='D:\\justplay\\asciiweb\\www\\tmp\\88.jpg',receiver="asciiart@163.com"):
TextContent = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>来自ascii-art的日志信息</h1>
<p>{}</p>
<p>{}</p>
<img src="cid:dns_config" alt="some_text">
</body>
</html>
'''.format(a1,a2)
sender = 'asciiart@163.com'
receiver = 'asciiart@163.com'
subject = 'ascii-art web log'
smtpserver = 'smtp.163.com'
password = '****************'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgText = MIMEText(
TextContent, 'html', 'utf-8')
msgRoot.attach(msgText)
fp = open(img_path, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', 'dns_config')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
send_img_email('lcx','nb',"C:\\Users\\31646\\Pictures\\Saved Pictures\\lena.jpg")
```
![image-20220925184517138](http://cdn.lcx-blog.top/img/image-20220925184517138.png)
## 发送附件
```python
def send_appendix_email(a1,a2,file_path='D:\\justplay\\asciiweb\\www\\tmp\\88.jpg',receiver="asciiart@163.com"):
TextContent = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>来自ascii-art的日志信息</h1>
<p>{}</p>
<p>{}</p>
</body>
</html>
'''.format(a1,a2)
sender = 'asciiart@163.com'
receiver = 'asciiart@163.com'
subject = 'ascii-art web log'
smtpserver = 'smtp.163.com'
#username = '***'
password = '****************'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgText = MIMEText(
TextContent, 'html', 'utf-8')
msgRoot.attach(msgText)
#构造附件txt附件1
fp = open(file_path, 'rb')
msgFile = MIMEApplication(fp.read())
fp.close()
_,filename = os.path.split(file_path)
#msgFile["content_Type"]='application/octet-stream'
msgFile["Content-Disposition"] = 'attachment; filename="{}"' .format(filename)
msgRoot.attach(msgFile)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
send_appendix_email('lcx','nb',"C:\\Users\\31646\\Pictures\\Saved Pictures\\cat.ico")
```
![](http://cdn.lcx-blog.top/img/image-20220925191759391.png)
## 部署在云服务器上
会出现超时的错误,这是因为25端口被ban了https://blog.csdn.net/codeHaoHao/article/details/82940865
只好用SSL协议发送邮件,服务器会使用465端口来发送。
改为如下格式https://blog.csdn.net/weixin_30687811/article/details/95815435
```python
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import os
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
mail_info = {
"from": "asciiart@163.com",
"to": "asciiart@163.com",
"hostname": "smtp.163.com",
"username": "asciiart@163.com",
"password": "****************",
"mail_subject": "asciiWeb log ",
"mail_text": "haaaaello, this is a test email, sended by py",
"mail_encoding": "utf-8"
}
def send_txt_email(flag_subject,a1):
msg = MIMEText(mail_info["mail_text"]+a1, "plain", mail_info["mail_encoding"])
msg["Subject"] = Header(mail_info["mail_subject"]+flag_subject, mail_info["mail_encoding"])
msg["from"] = mail_info["from"]
msg["to"] = mail_info["to"]
#---------------------------------------------
#这里使用SMTP_SSL就是默认使用465端口
smtp = SMTP_SSL(mail_info["hostname"])
#smtp.set_debuglevel(1)
smtp.ehlo(mail_info["hostname"])
smtp.login(mail_info["username"], mail_info["password"])
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
smtp.quit()
def send_img_email(flag_subject,a1,a2,img_path='D:\\justplay\\asciiweb\\www\\tmp\\88.jpg'):
TextContent = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>来自ascii-art的日志信息</h1>
<p>{}</p>
<p>{}</p>
<img src="cid:dns_config" alt="some_text">
</body>
</html>
'''.format(a1,a2)
#----------------------------------------
msg = MIMEMultipart('related')
#文本
msgText = MIMEText(
TextContent, 'html', 'utf-8')
msg.attach(msgText)
#图片
fp = open(img_path, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', 'dns_config')
msg.attach(msgImage)
msg["Subject"] = Header(mail_info["mail_subject"]+flag_subject, mail_info["mail_encoding"])
msg["from"] = mail_info["from"]
msg["to"] = mail_info["to"]
#------------------------------------------------------
#这里使用SMTP_SSL就是默认使用465端口
smtp = SMTP_SSL(mail_info["hostname"])
#smtp.set_debuglevel(1)
smtp.ehlo(mail_info["hostname"])
smtp.login(mail_info["username"], mail_info["password"])
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
smtp.quit()
def send_appendix_email(flag_subject,a1,a2,file_path='D:\\justplay\\asciiweb\\www\\tmp\\88.jpg'):
TextContent = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>来自ascii-art的日志信息</h1>
<p>{}</p>
<p>{}</p>
<img src="cid:dns_config" alt="some_text">
</body>
</html>
'''.format(a1,a2)
#----------------------------------------
msg = MIMEMultipart('related')
#文本
msgText = MIMEText(TextContent, 'html', 'utf-8')
msg.attach(msgText)
#图片
fp = open(file_path, 'rb')
msgFile = MIMEApplication(fp.read())
fp.close()
_,filename = os.path.split(file_path)
msgFile["Content-Disposition"] = 'attachment; filename="{}"' .format(filename)
msg.attach(msgFile)
msg["Subject"] = Header(mail_info["mail_subject"]+flag_subject, mail_info["mail_encoding"])
msg["from"] = mail_info["from"]
msg["to"] = mail_info["to"]
#------------------------------------------------------
#这里使用SMTP_SSL就是默认使用465端口
smtp = SMTP_SSL(mail_info["hostname"])
#smtp.set_debuglevel(1)
smtp.ehlo(mail_info["hostname"])
smtp.login(mail_info["username"], mail_info["password"])
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())
smtp.quit()
if __name__ == '__main__':
#send_txt_email('okok','lcx')
#send_img_email('not fail','1','2','D:\\justplay\\asciiweb\\www\\tmp\\88.jpg')
#send_appendix_email('not fail','1','2','D:\\justplay\\asciiweb\\www\\tmp\\88.jpg')
#send_img_email('linux not fail','1','2','/root/asciiWeb/www/tmp/88.jpg')
send_appendix_email('ubuntu not fail','1','2','/root/asciiWeb/www/tmp/88.jpg')
```
简单小网站(四)