From 627d470f491d8b03a3a337733555cb27797c6493 Mon Sep 17 00:00:00 2001 From: 123GitHub0123 <2405606352@qq.com> Date: Wed, 3 May 2023 09:35:52 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0outlook=E9=82=AE=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/123GitHub0123/mail_sent.md | 5 ++ contributors/123GitHub0123/mail_sent.py | 82 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 contributors/123GitHub0123/mail_sent.md create mode 100644 contributors/123GitHub0123/mail_sent.py diff --git a/contributors/123GitHub0123/mail_sent.md b/contributors/123GitHub0123/mail_sent.md new file mode 100644 index 0000000..cb3e038 --- /dev/null +++ b/contributors/123GitHub0123/mail_sent.md @@ -0,0 +1,5 @@ +# mail_sent模块 + +邮件发送模块,目前仅支持outlook邮箱,后续添加跟多邮箱 + +编程菜鸟,如有不足请指正 \ No newline at end of file diff --git a/contributors/123GitHub0123/mail_sent.py b/contributors/123GitHub0123/mail_sent.py new file mode 100644 index 0000000..454fbbc --- /dev/null +++ b/contributors/123GitHub0123/mail_sent.py @@ -0,0 +1,82 @@ +# !/usr/bin/env python +# -*- coding:utf-8 -*- +# @FileName : mail_sent.py +# @Time : 2023/2/10 13:11 + + +# my_sender = 'onetop99@outlook.com' # 发件人邮箱账号 +# my_pass = 'Wcl201314' # 发件人邮箱密码 +# my_user = '18142510250@163.com' # 收件人邮箱账号,我这边发送给自己 + +import smtplib +import winsound + +# 发送文字 +from email.mime.text import MIMEText + +# 发送文件 +from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication + + + +def send_mail(_sender, _passwd, receiver: list, files: list = None, content=None, subject=None, types='plain' or 'html', + log_options=False, host='smtp.office365.com', port=587): + """ + + :param _sender: 必填,字符串,服务邮件账号 + :param _passwd: 必填,字符串,服务邮件密码 + :param receiver: 必填,列表,发送邮件目的地 + :param content: 默认空,字符串,发送文本 + :param subject: 默认空,字符串,邮件主题 + :param files: 可选,列表,发送附件时候使用,支持多文件 + :param types: 默认’plain‘,字符串,邮件发送类型,常见类型:'plain'常用文本, 'html'超文本 + :param log_options: 默认False,布尔值 + :param host: 邮件服务器地址 + :return: + """ + ret = True + try: + # 重要的4行代码 + server = smtplib.SMTP(host, port) # 这里容易遗漏host,切记 + # server.connect(host, 587) + server.set_debuglevel(log_options) # 打印登录日志 + server.ehlo() + server.starttls() + server.ehlo() + server.login(_sender, _passwd) # 如果程序需要先花很多时间去获取各种信息然后把信息发出来,那么千万别在程序开启的时候就login,要不然断开连接了,要发信的时候再login比较好 + + msg = MIMEMultipart() + msg["Accept-Charset"] = "ISO-8859-1,utf-8" + msg["Accept-Language"] = "zh-CN" + msg['Subject'] = subject + msg['From'] = _sender + msg['To'] = ','.join(receiver) # 这里必须要把多个邮箱按照逗号拼接为字符串 + + # 文本 + + message = MIMEText(content, types, 'utf-8') + msg.attach(message) + + # 多个文件,for循环attach + try: + for fileName in files: + with open(fileName, 'rb') as f: + part = MIMEApplication(f.read()) + part.add_header('Content-Disposition', 'attachment', filename=fileName) + msg.attach(part) + except: + pass + + server.sendmail(_sender, receiver, msg.as_string()) + + server.quit() + print('email sent') + winsound.MessageBeep() + except: + ret = False + + if ret: + print('sent to') + else: + print('erron') From 2aca4f5cc7a433212cd8677660a584dfa42d3c5b Mon Sep 17 00:00:00 2001 From: 123GitHub0123 <2405606352@qq.com> Date: Wed, 3 May 2023 10:13:23 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0outlook=E9=82=AE=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/123GitHub0123/mail_sent.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/contributors/123GitHub0123/mail_sent.py b/contributors/123GitHub0123/mail_sent.py index 454fbbc..b7fec97 100644 --- a/contributors/123GitHub0123/mail_sent.py +++ b/contributors/123GitHub0123/mail_sent.py @@ -4,9 +4,6 @@ # @Time : 2023/2/10 13:11 -# my_sender = 'onetop99@outlook.com' # 发件人邮箱账号 -# my_pass = 'Wcl201314' # 发件人邮箱密码 -# my_user = '18142510250@163.com' # 收件人邮箱账号,我这边发送给自己 import smtplib import winsound From 442acdc765cba4d79abd1d831581122d7769df10 Mon Sep 17 00:00:00 2001 From: 123GitHub0123 <2405606352@qq.com> Date: Wed, 3 May 2023 10:35:27 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0outlook=E9=82=AE=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/123GitHub0123/mail_sent.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contributors/123GitHub0123/mail_sent.py b/contributors/123GitHub0123/mail_sent.py index b7fec97..74063a3 100644 --- a/contributors/123GitHub0123/mail_sent.py +++ b/contributors/123GitHub0123/mail_sent.py @@ -5,6 +5,7 @@ + import smtplib import winsound From 0e03472143873750ae1eca36b547d1e5bfa30fbe Mon Sep 17 00:00:00 2001 From: 123GitHub0123 <89198225+123GitHub0123@users.noreply.github.com> Date: Sat, 6 May 2023 08:42:37 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A8=A1=E5=9D=97dome?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/123GitHub0123/mail_sent.md | 32 +++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/contributors/123GitHub0123/mail_sent.md b/contributors/123GitHub0123/mail_sent.md index cb3e038..2928531 100644 --- a/contributors/123GitHub0123/mail_sent.md +++ b/contributors/123GitHub0123/mail_sent.md @@ -1,5 +1,33 @@ # mail_sent模块 -邮件发送模块,目前仅支持outlook邮箱,后续添加跟多邮箱 +参数如下 -编程菜鸟,如有不足请指正 \ No newline at end of file + :param _sender: 必填,字符串,服务邮件账号 + :param _passwd: 必填,字符串,服务邮件密码 + :param receiver: 必填,列表,发送邮件目的地 + :param content: 默认空,字符串,发送文本 + :param subject: 默认空,字符串,邮件主题 + :param files: 可选,列表,发送附件时候使用,支持多文件 + :param types: 默认’plain‘,字符串,邮件发送类型,常见类型:'plain'常用文本, 'html'超文本 + :param log_options: 默认False,布尔值 + :param host: 邮件服务器地址 + :return: + +**main.py** +```python +import mail_sent + +_sender = '*******@outlook.com' # 邮箱账号 +_passwd = '******' # 邮箱账号密码 +receiver = ['******'] # 多个收件人放在一个list里面 # 接收邮箱 +text = ''' +文本内容(可根据send_mail的types参数发送内容为html或者正常文本内容) +''' + +mail_sent.send_mail(_sender, _passwd, receiver, content=text, subject='青年大学习完成名单情况(测试)', types='plain') +``` + +> 邮件发送模块 +> 由于outlook邮箱发送和其他国内邮箱有些不一样,目前仅支持outlook邮箱,后续添加跟多邮箱 + +编程菜鸟,如有不足请指正