我試過讓這個工作,但必須有更好的方式,任何輸入是值得歡迎的。發送預定的電子郵件與pyramid_mailer和apscheduler
我試圖使用pyramid_mailer(存儲在.ini文件中的設置)在我的python金字塔應用程序中發送計劃的電子郵件,並且設置計劃的apscheduler。
我還使用SQLAlchemyJobStore,因此如果應用程序重新啓動,可以重新啓動作業。
jobstores = {
'default': SQLAlchemyJobStore(url='mysql://localhost/lgmim')
}
scheduler = BackgroundScheduler(jobstores=jobstores)
@view_config(route_name='start_email_schedule')
def start_email_schedule(request):
# add the job and start the scheduler
scheduler.add_job(send_scheduled_email, 'interval', [request], weeks=1)
scheduler.start()
return HTTPOk()
def send_scheduled_email(request):
# compile message and recipients
# send mail
send_mail(request, subject, recipients, message)
def send_mail(request, subject, recipients, body):
mailer = request.registry['mailer']
message = Message(subject=subject,
recipients=recipients,
body=body)
mailer.send_immediately(message, fail_silently=False)
這是就我所知,現在我得到一個錯誤,可能是因爲它不能pickle請求。
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
使用pyramid.threadlocal.get_current_registry().settings
獲得郵件工作的第一次,但此後我得到一個錯誤。我建議不要在任何情況下使用它。
我還能做什麼?
謝謝你,這是真的很有幫助。你的鏈接似乎被打破了。 – Niel
修復了鏈接。被瀏覽器自動完成咬傷。 –