def send_mail(srv_name): '''发送邮件''' data = request.get_json() if srv_name not in config: logging.warning('!!!'+str(datetime.datetime.now())+'InvalidService!!!') return jsonify({"status":1, "msg":"InvalidService"}) else: srv_config = config[srv_name] if not verify_key(srv_name, data['key']): logging.warning('!!!'+str(datetime.datetime.now())+'InvalidKey!!!') return jsonify({"status":1, "msg":"InvalidKey"}) mail_msg = Message(data['mail_title'], fromaddr=srv_config['mail_from'], to=data['mail_to']) mail_msg.html = data['mail_html'] mail = Mail( srv_config['smtp'], port=srv_config['port'], username=srv_config['mail_from'], password=srv_config['smtppass'], use_tls=srv_config['tls'], use_ssl=srv_config['ssl'], debug_level=None ) try: logging.info('--------'+str(datetime.datetime.now())+'--------------') logging.info(data['mail_title']) logging.info(data['mail_to']) logging.info(data['mail_html']) logging.info('=======================================================') mail.send(mail_msg) return jsonify({"status":0, "msg":"Success"}) except Exception as e: logging.warning('!!!'+str(datetime.datetime.now())+'!!!') logging.warning(str(e)) return jsonify({"status":2, "msg":str(e)})
def send_email(): try: data = app.current_request.json_body # print(data) email = data["email"] password = data["password"] smtp_server = data["smtp_server"] smtp_port = data["smtp_port"] toAddress = data["toAddress"] fromAddress = data["fromAddress"] name = data["name"] subject = data["subject"] bodyPLAIN = data["bodyPLAIN"] bodyHTML = textile(bodyPLAIN) mail = Mail(host=smtp_server, port=smtp_port, username=email, password=password, use_ssl=True) msg = Message() msg.subject = subject msg.fromaddr = (name, fromAddress) msg.to = toAddress msg.body = bodyPLAIN msg.html = bodyHTML # msg.cc = "*****@*****.**" # msg.bcc = ["*****@*****.**", "*****@*****.**"] # msg.reply_to = "*****@*****.**" msg.date = int(round(time.time())) msg.charset = "utf-8" msg.extra_headers = {} msg.mail_options = [] msg.rcpt_options = [] # print(msg) # print(type(msg)) mail.send(msg) return Response(body={'sent': True}, status_code=200, headers=custom_headers) except Exception as error: # print("Send Emails Error") # print(error) return Response(body={'AppError': str(error)}, status_code=500, headers=custom_headers)
def __send_email_to(self, user, bangumi_map): info = { 'username': user.name, 'root_path': self.root_path, 'sys': False } bangumi_list = self.__bangumi_map_to_list(bangumi_map) msg = Message('Download Status Alert', fromaddr=('Alert System', self.mail_config['mail_default_sender'])) msg.to = user.email msg.html = self.mail_template.render(info=info, bangumi_list=bangumi_list) return msg
def __send_email_to_all(self, admin_list, bangumi_map): msg_list = [] for user in admin_list: info = { 'username': user.name, 'root_path': self.root_path, 'sys': True } bangumi_list = self.__bangumi_map_to_list(bangumi_map) msg = Message('Download Status Alert', fromaddr=('Alert System', self.mail_config['mail_default_sender'])) msg.to = user.email msg.html = self.mail_template.render(info=info, bangumi_list=bangumi_list) msg_list.append(msg) return msg_list
def send_message(subject, to, body): mail = Mail(MAIL_SMTP_ADDRESS, port=MAIL_PORT, username=FROM_EMAIL, password=MAIL_PASSWORD, use_tls=False, use_ssl=False, debug_level=None) msg = Message() msg = Message(subject) msg.fromaddr = (FROM_NAME, FROM_EMAIL) msg.to = to msg.bcc = [DEV_EMAIL] msg.html = body mail.send(msg) return True
def send(text, subject, toAdd, cc=None, attachments=None): """邮件发送方法 Arguments: text {str} -- 邮件内容,可以为字符串或者 HTML 字符串 subject {str} -- 邮件标题 toAdd {list} -- 收件人列表 Keyword Arguments: cc {list} -- 抄送人列表 (default: {None}) attachments {list} -- 附件列表,示例:[{'path': '/path/to/file', 'file_name': '附件文件名'}] (default: {None}) """ assert subject is not None assert toAdd is not None sslPort = os.getenv('MAIL_SERVER_PORT') server = os.getenv('MAIL_SMTP_SERVER') user = os.getenv('MAIL_USER') passwd = os.getenv('MAIL_PASSWORD') assert server is not None assert user is not None assert passwd is not None if not sslPort: sslPort = 465 mail = Mail(server, port=sslPort, username=user, password=passwd, use_tls=False, use_ssl=True, debug_level=None) msg = Message(subject) msg.fromaddr = (user, user) msg.to = toAdd if cc: msg.cc = cc if attachments: for att in attachments: with open(att['path'], 'rb') as f: mail_attachment = Attachment(att['file_name'], "application/octet-stream", f.read()) msg.attach(mail_attachment) msg.html = text msg.charset = "utf-8" mail.send(msg)
pwrd = os.environ['GMAIL_PASS'] if __name__ == "__main__": with Imbox(imap, username=user, password=pwrd, ssl=True, ssl_context=None, starttls=False) as imbox: drafts = imbox.messages(folder="[Gmail]/Drafts") todays_mail = [] for uid, msg in drafts: if 'schedmail' in msg.subject.lower(): date = msg.subject.lower().split(':')[1] today = pendulum.now().date().isoformat() subject_date = pendulum.parse(date).date().isoformat() if subject_date == today: todays_mail.append(msg) mail = Mail('smtp.gmail.com', port=587, username=user, password=pwrd, use_tls=True) for i in todays_mail: msg = Message(i.subject.split(':')[-1]) msg.fromaddr = (i.sent_from[0]['name'], i.sent_from[0]['email']) msg.to = [j['email'] for j in i.sent_to] msg.cc = [j['email'] for j in i.cc] msg.bcc = [j['email'] for j in i.bcc] msg.body = i.body['plain'][0] msg.html = i.body['html'][0] msg.charset = 'utf-8' mail.send(msg)
mail = Mail(host=SMTP_HOST, username=SMTP_USER, password=SMTP_PASS, fromaddr=SMTP_ADDRESS) msg01 = Message("Hello01", to="*****@*****.**", body="hello world") msg02 = Message("Hello02", to="*****@*****.**") msg02.fromaddr = ('no-reply', '*****@*****.**') msg02.body = "hello world!" msg03 = Message("Hello03", to="*****@*****.**") msg03.fromaddr = (u'请勿回复', '*****@*****.**') msg03.body = u"你好世界" # Chinese :) msg03.html = u"<b>你好世界</b>" msg04 = Message("Hello04", body="Hello world 04") msg04.to = "*****@*****.**" msg04.cc = ["*****@*****.**", "cc02@example"] msg04.bcc = ["*****@*****.**"] msg05 = Message("Hello05", to="*****@*****.**", body="Hello world 05") with open("../docs/_static/sender.png") as f: msg05.attach_attachment("sender.png", "image/png", f.read()) msg06 = Message("Hello06", to="*****@*****.**", body="Hello world 06") with open("test.txt") as f: attachment = Attachment("test.txt", "text/plain", f.read()) msg06.attach(attachment)
#!/usr/bin/env python3 # Before begin you need to change Gmail settings: # 1. Turn off 2-step verification # 2. Enable: Allow less secure apps from sender import Mail, Message mail = Mail("smtp.gmail.com", port=587, username="******", password="******", use_tls=True, use_ssl=False, debug_level=None) mail.fromaddr = ("Sender Name", "*****@*****.**") msg = Message("msg subject") msg.fromaddr = ("Sender Name", "*****@*****.**") msg.to = "*****@*****.**" msg.body = "this is a msg plain text body" msg.html = "<b>this is a msg text body</b>" msg.reply_to = "*****@*****.**" msg.charset = "utf-8" msg.extra_headers = {} msg.mail_options = [] msg.rcpt_options = [] # Send message mail.send(msg)
mail = Mail(host=SMTP_HOST, username=SMTP_USER, password=SMTP_PASS, fromaddr=SMTP_ADDRESS) msg01 = Message("Hello01", to="*****@*****.**", body="hello world") msg02 = Message("Hello02", to="*****@*****.**") msg02.fromaddr = ('no-reply', '*****@*****.**') msg02.body = "hello world!" msg03 = Message("Hello03", to="*****@*****.**") msg03.fromaddr = (u'请勿回复', '*****@*****.**') msg03.body = u"你好世界" # Chinese :) msg03.html = u"<b>你好世界</b>" msg04 = Message("Hello04", body="Hello world 04") msg04.to = "*****@*****.**" msg04.cc = ["*****@*****.**", "cc02@example"] msg04.bcc = ["*****@*****.**"] msg05 = Message("Hello05", to="*****@*****.**", body="Hello world 05") with open("../docs/_static/sender.png") as f: msg05.attach_attachment("sender.png", "image/png", f.read()) msg06 = Message("Hello06", to="*****@*****.**", body="Hello world 06") with open("test.txt") as f: