def send(title: str, content: str, sender_id: int, receiver_id: int): form = dict(title=title, content=content, sender_id=sender_id, receiver_id=receiver_id) Messages.new(form) receiver: User = User.one(id=receiver_id) # by default # send_mail( # subject=title, # author=admin_mail, # to=receiver.email, # content='站内信通知:\n {}'.format(content), # ) # by thread # import threading # form = dict( # subject=form['title'], # author=admin_mail, # to=receiver.email, # plain=form['content'], # ) # t = threading.Thread(target=send_mail, kwargs=form) # t.start() # by celery send_async.delay(subject=form['title'], author=admin_mail, to=receiver.email, plain=form['content'])
def send(title: str, content: str, sender_id: int, receiver_id: int): form = dict(title=title, content=content, sender_id=sender_id, receiver_id=receiver_id) Messages.new(form) receiver: User = User.one(id=receiver_id) send_async.delay(subject=form['title'], author=admin_mail, to=receiver.email, plain=form['content'])
def send(title: str, content: str, sender_id: int, receiver_id: int, type: str): form = dict( title=title, content=content, sender_id=sender_id, receiver_id=receiver_id, type=type, ) Messages.new(form) receiver: User = User.one(id=receiver_id) if form['type'] == 'private_letter': send_mail( subject=title, author=admin_mail, to=receiver.email, content='站内信通知:\n {}'.format(content), ) send_async.delay(subject=form['title'], author=admin_mail, to=receiver.email, plain=form['content']) import threading
from tasks import send_async from config import test_mail, admin_mail if __name__ == '__main__': subject = 'celery的测试' content = '如果收到,说明测试成功' send_async.delay(subject, author=admin_mail, to=test_mail, content=content)