我刚刚安装了django_crontab
而不是post
,但不知怎的,我正在尝试发送电子邮件作为通知,但它无法正常工作。
这仅用于测试目的,因此我将其设置为1分钟。
代码之前有点复杂,但它没有用,所以我用最简单的方式发送电子邮件,以确保它不起作用。甚至还使用它作为class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 1 # every minute
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'Email_Test' # a unique code
def do(self):
print('######################################')
send_mail(
'Subject here from cron',
'Here is the message.',
'from@email.com',
['to@emailcom'],
fail_silently=False,
)
方法进行测试,并且如果post方法称为跟随代码
python manage.py runcrons
我尝试运行python manage.py runcrons --force
和print
并等待,(没有错误,我还添加了#################
因为我想看看代码是否运行良好,我看到{ {1}}打印出来了)
有人可以给我一个建议吗?
提前致谢
答案 0 :(得分:0)
尝试使用自定义功能发送邮件。我尝试了send_mail
功能,它对我不起作用,我没有找到关于这个主题的任何错误或解释。以下是使用smtplib.
import smtplib
def send_email(email, subject, message):
text = subject
html = message
msg = MIMEMultipart('alternative')
html_part = MIMEText(html, 'html')
text_part = MIMEText(text, 'plain')
msg.attach(text_part)
msg.attach(html_part)
msg['Subject'] = subject
msg['From'] = SENDER_EMAIL
msg['To'] = email
s = smtplib.SMTP(settings.GMAIL_SMTP)
s.ehlo()
s.starttls()
s.login(SENDER_EMAIL, SENDER_PASSWORD)
s.sendmail(SENDER_EMAIL, email, msg.as_string())
s.quit()
答案 1 :(得分:0)
请确保首先必须从django.core.mail导入库,才能使用send_mail。我知道为时已晚,但是您可以尝试一下。
from django_cron import CronJobBase, Schedule
from django.core.mail import send_mail
class my_scheduled_job(CronJobBase):
RUN_EVERY_MINS = 1
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'user_dashboard.autoemail.my_scheduled_job'
def do(self):
subject= 'Send Email With Automatic Schedule'
message= 'Test send email :'
email_to= ['xxxxxxxx@gmail.com']
email_user(subject_test, message_test, email_to_test)
print ("done")
def email_user(subject, message, email_to):
email_from = 'noreply@xxxx.id'
send = send_mail(subject, message, email_from, email_to)
return send