django发送邮件问题

发布于 2022-09-04 15:49:00 字数 4348 浏览 19 评论 0

django+celery+redis+send_mail来发送邮件

当直接console启动django项目和celery时候,既如下情况时候可以正常邮件发送:

启动django

[root@vultrvpn vpnWeb]# python run.py 
Performing system checks...

System check identified no issues (0 silenced).
March 17, 2017 - 11:09:42
Django version 1.10.6, using settings 'vpnWeb.settings'
Starting development server at http://0.0.0.0:9000/
Quit the server with CONTROL-C.
[17/Mar/2017 11:11:51] "GET /admin/ HTTP/1.0" 200 6274
[17/Mar/2017 11:11:54] "GET /admin/app/userpay/ HTTP/1.0" 200 7052
[17/Mar/2017 11:11:55] "GET /admin/jsi18n/ HTTP/1.0" 200 3217
[17/Mar/2017 11:12:00] "POST /admin/app/userpay/ HTTP/1.0" 200 3168

启动celery

[root@vultrvpn vpnWeb]# celery -A vpnWeb worker -l info
/usr/lib/python2.7/site-packages/celery/platforms.py:793: RuntimeWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!

Please specify a different user using the -u option.

User information: uid=0 euid=0 gid=0 egid=0

  uid=uid, euid=euid, gid=gid, egid=egid,
 
 -------------- celery@vultrvpn v4.0.2 (latentcall)
---- **** ----- 
--- * ***  * -- Linux-4.10.2-1.el7.elrepo.x86_64-x86_64-with-centos-7.3.1611-Core 2017-03-17 11:09:32
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         vpnWeb:0x234a450
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     redis://localhost:6379/0
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . app.emailTask.sendEmal
  . vpnWeb.celery.debug_task

[2017-03-17 11:09:32,548: INFO/MainProcess] Connected to redis://localhost:6379/0
[2017-03-17 11:09:32,557: INFO/MainProcess] mingle: searching for neighbors
[2017-03-17 11:09:33,571: INFO/MainProcess] mingle: all alone
[2017-03-17 11:09:33,578: INFO/MainProcess] celery@vultrvpn ready.
[2017-03-17 11:14:55,514: INFO/MainProcess] Received task: app.emailTask.sendEmal[618eb32c-0c34-4cf7-9c0f-9020f6d72242]  
[2017-03-17 11:14:58,385: INFO/PoolWorker-1] Task app.emailTask.sendEmal[618eb32c-0c34-4cf7-9c0f-9020f6d72242] succeeded in 2.868786066s: None

当项目调用到send_mail时候可以正常进行邮件发送
发送邮件代码

from django.core.mail import send_mail
import time
from time import sleep

@celery_app.task
def sendEmal(tomail):
    send_mail(
        u'账号信息',
        u'发送目标邮箱',
        'sedegse@gmail.com',
        [tomail],
        fail_silently=False,
    )

但当用supervisor进行守护进程部署django
以及celery也部署了守护进程(按以下方式部署守护进程)

参考资料:
http://docs.celeryproject.org...
拷贝这个文件(https://github.com/celery/cel...)内容到/etc/init.d/celeryd

邮件就无法发送了。

守护进程时候celery调用发送邮件task的时候打印日志内容:

[2017-03-17 10:58:30,190: INFO/MainProcess] mingle: all alone
[2017-03-17 10:58:30,197: INFO/MainProcess] djangoCelery1@vultrvpn ready.
[2017-03-17 10:58:49,185: INFO/MainProcess] Received task: app.emailTask.sendEmal[1c9ff6d0-2962-42ce-ba35-151c73980ffb]  
[2017-03-17 11:06:51,331: INFO/MainProcess] Received task: app.emailTask.sendEmal[7b71fa6c-0554-44c3-a030-20f14885f001]  
[2017-03-17 11:07:44,339: INFO/MainProcess] Connected to redis://localhost:6379/0
[2017-03-17 11:07:44,347: INFO/MainProcess] mingle: searching for neighbors
[2017-03-17 11:07:45,361: INFO/MainProcess] mingle: all alone
[2017-03-17 11:07:45,367: INFO/MainProcess] djangoCelery1@vultrvpn ready.
[2017-03-17 11:30:44,493: INFO/MainProcess] Connected to redis://localhost:6379/0
[2017-03-17 11:30:44,499: INFO/MainProcess] mingle: searching for neighbors
[2017-03-17 11:30:45,526: INFO/MainProcess] mingle: all alone
[2017-03-17 11:30:45,532: INFO/MainProcess] djangoCelery1@vultrvpn ready.
[2017-03-17 11:31:04,372: INFO/MainProcess] Received task: app.emailTask.sendEmal[680f3ae0-a85f-4b7c-a9d7-9c64d1788aa2]  

后有经过测试,django部署守护进程,celery用console启动,就可以正常发送邮件。

那么现在要怎么调整才能在celery使用守护进程部署的时候也能进行邮件发送的?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2022-09-11 15:49:00

import logging

在 task 里, 加 logging.error() 看一下

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文