首先:
1.需要在邮箱中开启smtp服务。
msg_from = '[email protected]' # 发送方邮箱
passwd = 'KD232323PHV'#邮箱识别码(需要在邮箱设置中开启。其自行保存,系统只出现一次,切记!!!!)
to = ['[email protected]'] # 接收方邮箱
2.电脑防火墙关闭
需求:
文件夹内最新保存的图片,需要推送邮箱。图片以附件的形式
刚开始的代码持续不停发图片给我。后来写了个集合,判断上一张图片是都发过,发过来就不发了,成功解决问题。
技术点:
1、新保存的已经发过的不再重复发邮件
2、图片邮箱附件的形式。
126邮箱推送代码
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os
import time
from PIL import Image
def send_email(image_path):
msg_from = '[email protected]' # 发送方邮箱
passwd = 'KD232323PHV'
to = ['[email protected]'] # 接收方邮箱
# 设置邮件内容
msg = MIMEMultipart()
content = "这个是字符串"
# 把内容加进去
# msg.attach(MIMEText(content, 'plain', 'utf-8'))
# 添加图片文件到邮件信息当中去
with open(image_path, 'rb') as image_file:
img_data = image_file.read()
msg.attach(MIMEImage(img_data, name=os.path.basename(image_path)))
# 设置邮件主题
msg['Subject'] = "这个是邮件主题"
# 发送方信息
msg['From'] = msg_from
# 开始发送
try:
# 使用SMTP_SSL连接网易126邮箱
with smtplib.SMTP_SSL("smtp.126.com", 465) as s:
# 登录邮箱
s.login(msg_from, passwd)
# 开始发送
s.sendmail(msg_from, to, msg.as_string())
print("邮件发送成功")
except Exception as e:
print(f"邮件发送失败:{e}")
def get_latest_screenshot(folder_path):
# 获取文件夹内最新的截图文件
screenshots = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
if not screenshots:
print("No screenshot found.")
return None
latest_screenshot = max(screenshots, key=lambda x: os.path.getmtime(os.path.join(folder_path, x)))
return os.path.join(folder_path, latest_screenshot)
def main():
folder_path = r'C:Users\' # 替换为存储截图的文件夹路径
sent_images = set() # 记录已发送的照片文件名
i = 1
while True:
i += 1
im = Image.new('RGB', (100, 100))
im.save('s' + str(i) + '.jpg')
# 获取最新截图文件路径
latest_screenshot = get_latest_screenshot(folder_path)
if latest_screenshot and latest_screenshot not in sent_images:
# 发送邮件
send_email(latest_screenshot)
sent_images.add(latest_screenshot)
time.sleep(10) # 修改发送邮件间隔时间
os.remove('s' + str(i) + '.jpg')
if __name__ == "__main__":
main()
163邮箱代码:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os
import time
from PIL import Image
def send_email(image_path):
msg_from = '[email protected]' # 发送方邮箱
passwd = 'KD232323PHV'
to = ['[email protected]'] # 接收方邮箱
# 设置邮件内容
msg = MIMEMultipart()
content = "这个是字符串"
# 把内容加进去
# msg.attach(MIMEText(content, 'plain', 'utf-8'))
# 添加图片文件到邮件信息当中去
with open(image_path, 'rb') as image_file:
img_data = image_file.read()
msg.attach(MIMEImage(img_data, name=os.path.basename(image_path)))
# 设置邮件主题
msg['Subject'] = "这个是邮件主题"
# 发送方信息
msg['From'] = msg_from
# 开始发送
try:
# 使用SMTP_SSL连接网易163邮箱
with smtplib.SMTP_SSL("smtp.163.com", 465) as s:
# 登录邮箱
s.login(msg_from, passwd)
# 开始发送
s.sendmail(msg_from, to, msg.as_string())
print("邮件发送成功")
except Exception as e:
print(f"邮件发送失败:{e}")
def get_latest_screenshot(folder_path):
# 获取文件夹内最新的截图文件
screenshots = [f for f in os.listdir(folder_path) if f.endswith('.jpg')]
if not screenshots:
print("No screenshot found.")
return None
latest_screenshot = max(screenshots, key=lambda x: os.path.getmtime(os.path.join(folder_path, x)))
return os.path.join(folder_path, latest_screenshot)
def main():
folder_path = r'C:Users\' # 替换为存储截图的文件夹路径
sent_images = set() # 记录已发送的照片文件名
i = 1
while True:
i += 1
im = Image.new('RGB', (100, 100))
im.save('s' + str(i) + '.jpg')
# 获取最新截图文件路径
latest_screenshot = get_latest_screenshot(folder_path)
if latest_screenshot and latest_screenshot not in sent_images:
# 发送邮件
send_email(latest_screenshot)
sent_images.add(latest_screenshot)
time.sleep(10) # 修改发送邮件间隔时间
os.remove('s' + str(i) + '.jpg')
if __name__ == "__main__":
main()