Django应用程序,仅在工作日使用脚本(使用脚本)发送电子邮件。试图使用谷歌的STMP。
以下是相关脚本:
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "labschedule.settings")
django.setup()
import datetime
from datetime import date
import smtplib
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
from labschedule import settings
def run():
today = date.today()
subject = "Daily Report for %s" % today
to = [settings.EMAIL_ADMIN]
from_email = 'blahblah@gmail.com' # My email
reservations = Event.objects.filter(day=today).order_by('cart')
last = Event.objects.latest('day')
if today >= today + datetime.timedelta(days=5):
countdown = last - datetime.timedelta(today)
warning = "Hey! You run out of open slots in %s days" % countdown
else:
warning = None
ctx = ({
'reservations' : reservations, 'warning' : warning, 'cart_choice' : cart_choice
})
html_content = render_to_string('schedule/email.html', ctx)
text_content = render_to_string('schedule/email.html', ctx)
msg = EmailMultiAlternatives(subject, text_content, to=to, from_email=from_email)
msg.attach_alternative(html_content, "text/html")
weekend = set([5, 6]) # Python week starts on Monday as 0
if today.weekday() not in weekend and settings.DAILY_EMAIL == True:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(settings.gmail_user, settings.gmail_password)
server.sendmail(from_email, to, msg)
server.close()
print ('Email sent!')
else:
pass
我主要阅读了教程here。
我收到的错误:
TypeError: object of type 'EmailMultiAlternatives' has no len()
我是新的,我知道这是愚蠢的,并且非常感谢任何帮助。
编辑:追溯
Traceback (most recent call last):
File "/home/mrsaltz/labschedule/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_comm
and_line
utility.execute()
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django_extensions/management/email_notifications.py", line 63, in r
un_from_argv
super(EmailNotificationCommand, self).run_from_argv(argv)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django_extensions/management/email_notifications.py", line 75, in e
xecute
super(EmailNotificationCommand, self).execute(*args, **options)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django_extensions/management/utils.py", line 58, in inner
ret = func(self, *args, **kwargs)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django_extensions/management/commands/runscript.py", line 165, in h
andle
run_script(mod, *script_args)
File "/home/mrsaltz/.virtualenvs/scheduler/lib/python3.5/site-packages/django_extensions/management/commands/runscript.py", line 77, in ru
n_script
mod.run(*script_args)
File "/home/mrsaltz/labschedule/scripts/daily_email.py", line 40, in run
server.sendmail(from_email, to, msg)
File "/usr/lib/python3.5/smtplib.py", line 853, in sendmail
esmtp_opts.append("size=%d" % len(msg))
TypeError: object of type 'EmailMultiAlternatives' has no len()
答案 0 :(得分:0)
SMTP_SSL.send()
期望字节串不是EmailMultiAlternatives的实例
你不应该在这里混合东西。 EmailMultiAlternatives是Django的一个类,但smtplib来自Python标准库。 EmailMultiAlternatives应该与Django邮件库一起使用 - 此外,它还负责所有SMTP连接和登录逻辑。
所有这些都包含在您已链接到的文档页面中:您应该遵循这一点。
答案 1 :(得分:-1)
对于to
(电子邮件)的参数未作为单个电子邮件填写,但它用于电子邮件列表。你应该改变:
msg = EmailMultiAlternatives(subject, text_content, to=to, from_email=from_email)
到
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
有关详情,请参阅this docs。