Djangoアプリで、スクリプトを使用してメールを送信します(runscriptを使用)。 GoogleのSTMPを使用しようとしています。TypeError: 'EmailMultiAlternatives'タイプのオブジェクトにlen()がありません
ここでのrelaventスクリプトです:
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 = '[email protected]' # 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()
に私は新しめだ、と私はそれがダム何か知っている、と大幅に任意の助けをいただければ幸いです。
EDIT:to
(電子メール)の引数のトレースバック
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()
完全トレースを表示してください –
また、リンク先のチュートリアルには、そのSSHのものはありません。これは、Djangoがあなたのために世話をするからです。 –
@DanielRoseman Tracebackが追加されました。ありがとうございました –