Skip to content

Commit

Permalink
Add send email util
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Sep 5, 2018
1 parent 1ebdd1d commit c2ef550
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from requests.adapters import HTTPAdapter
from flasgger import Swagger
from flask_flatpages import FlatPages
from flask_mail import Mail
from peewee import MySQLDatabase

import settings
Expand Down Expand Up @@ -97,3 +98,5 @@ def get_redis_client():


redis = get_redis_client()

mail = Mail(app)
13 changes: 13 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,16 @@
FLATPAGES_ROOT = os.path.join(PROJECT_ROOT_PATH, 'docs')
FLATPAGES_AUTO_RELOAD = DEBUG
FLATPAGES_EXTENSION = ['.md', '.html', '.htm', '.txt']

###########################
# settings for flask-mail #
###########################

MAIL_SERVER = config('MAIL_SERVER', default='localhost')
MAIL_PORT = config('MAIL_PORT', default=25, cast=int)
MAIL_USE_TLS = config('MAIL_USE_TLS', default=False, cast=bool)
MAIL_USE_SSL = config('MAIL_USE_SSL', default=False, cast=bool)
MAIL_DEBUG = config('MAIL_DEBUG', default=DEBUG, cast=bool)
MAIL_USERNAME = config('MAIL_USERNAME', default='')
MAIL_PASSWORD = config('MAIL_PASSWORD', default='')
MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', default='')
36 changes: 36 additions & 0 deletions app/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# http://docs.python-cerberus.org/en/stable/validation-rules.html

from utils.log import app_logger
import settings


def is_ipv4(ip):
Expand Down Expand Up @@ -115,3 +116,38 @@ def pagination(items_count, page_num, page_size):
'next_page_num': next_page_num,
'prev_page_num': prev_page_num,
}


def send_flask_mail(send_to,
title,
content,
html=True,
sender=settings.MAIL_DEFAULT_SENDER,
cc=None,
attachments=None):
'''使用邮件服务器发送邮件
:params send_to list: 收件人列表
:params title string: 邮件标题
:params content string: 邮件内容
:params html bool: 邮件内容是否为HTML格式,默认为True
:params sender string: 发件人
:params cc list: cc列表
:params attachments dict: 附件
eg: {"filename": "x.png", "content_type": "image/png", "data": img.read()}
'''
from services import mail, app
from flask_mail import Message

with app.app_context():
msg = Message()
msg.recipients = send_to
msg.cc = cc
msg.subject = title
msg.sender = sender
if html:
msg.html = content
else:
msg.body = content
if attachments:
msg.attach(**attachments)
mail.send(msg)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ requests
ujson
pyDes
redis-py-cluster
Flask-Mail

ipython
Flask-Script
Expand Down

0 comments on commit c2ef550

Please sign in to comment.