有个需求,需要给个别人开个发邮件的权限,但是又不希望他们知道账号密码。
这可咋搞?
我们可以用Python+Gooey+Zmail+Pyinstaller缝合起来下面的这个东西。 如果还不放心,还可以给这个exe程序加个壳混淆下。
最终效果如下:
如果什么参数不填,则会弹出报错提示
具体代码sb_send_email.py 内容如下:
# -*- coding: utf-8 -*-
# Version: Python3
# pip install Gooey==1.0.8.1
# pip install zmail==0.2.8
# 打包命令 pyinstaller.exe -F -w .\sb_send_email.py
import datetime
import zmail
from gooey import Gooey, GooeyParser
server = zmail.server(
username='xxxx@sina.com',
password='xxxxxxx',
smtp_host='smtp.sina.com',
smtp_port=465,
smtp_ssl=True,
smtp_tls=False,
timeout=120
)
def SendEmail(recipients, mail_content,cc=None):
server.send_mail(recipients=recipients, mail=mail_content,cc=cc)
@Gooey(encoding='utf-8', program_name="Email Sender v1.0", language='chinese')
def start():
parser = GooeyParser()
parser.add_argument("mail_attachment", metavar="mail_attachment", help="附件", widget="FileChooser")
parser.add_argument("mail_receiver", metavar="mail_receiver", help="收件人邮箱(只支持单个邮箱)")
parser.add_argument("mail_title", metavar="mail_title", help="邮件标题",default="XX需求")
parser.add_argument("mail_content", metavar="mail_content", help="邮件正文",widget="TextField",default="内容详见附件")
args = vars(parser.parse_args())
return args
if __name__ == "__main__":
args = start()
try:
SendEmail(
recipients = str(args["mail_receiver"]) ,
mail_content = {
'subject': str(args["mail_title"]),
'content_text': str(args["mail_content"]),
'attachments': [args["mail_attachment"]]
},
cc=None
)
except Exception as e:
print(str(e))
with open('C:\\sb_send_email.log' ,'a+') as f:
f.write(
"时间:" + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
+ ", 收件人:" + str(args["mail_receiver"])
+ ", 主题:" + str(args["mail_title"])
+ ", 正文:" + str(args["mail_content"])
+ ", 附件:" + str(args["mail_attachment"])
+ "\n"
)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。