公司有一个工作邮箱,每次需要给这个邮箱发送附件,将邮件主题编辑成指定的名称,才能够发送成功。
这个操作说麻烦吧,也不麻烦,但是每次都要进行一系列步骤:打开网页浏览器、打开邮箱、登录邮箱、新建邮件、选择收件人、编辑邮件主题、上传附件、执行发送。
最近不是ChatGPT写代码很火吗,试试看它能不能完成。
代码简单改一下就能用了,就不写注释了:
import os
import sys
import smtplib
import traceback
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
paths = sys.argv[1:]
send_addr = '[email protected]'
pass_word = 'password'
recv_addr = '[email protected]'
smtp_server = 'smtp.126.com'
subject = 'Title'
body = f'This email contain {len(paths)} attachments:\n' + ''.join(f' "{p}"\n' for p in paths)
print(body)
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = send_addr
msg['To'] = recv_addr
msg.attach(MIMEText(body, 'plain', 'utf-8'))
for path in paths:
if os.path.exists(path):
with open(path, 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
msg.attach(attachment)
try:
server = smtplib.SMTP_SSL(smtp_server, 465)
server.login(send_addr, pass_word)
server.sendmail(send_addr, recv_addr, msg.as_string())
server.quit()
except Exception as e:
traceback.print_exc(1)
print()
else:
print('Send success!\n')
input('Press enter to exit: ')
然后运行 shell:sendto
打开 发送到
文件夹,将脚本保存到这里之后,就可以在右键中使用发送到菜单,将选中的多个文件通过脚本自动发送了。