Home > Software design >  Django email is giving error ''list' object has no attribute 'splitlines'&#
Django email is giving error ''list' object has no attribute 'splitlines'&#

Time:02-19

This is my email sending code in my view:

def send_email():
    if dasa == 1:
        send_mail(
            'Test available',
            available_tests,
            'from_email',
            ['to_email'],
            fail_silently=False,
        )

When I run it on the website, though, I get this error:

AttributeError at /tests/
'list' object has no attribute 'splitlines'
Request Method: GET
Request URL:    https://myapp.herokuapp.com/tests/
Django Version: 4.0.1
Exception Type: AttributeError
Exception Value:    
'list' object has no attribute 'splitlines'
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 169, in set_payload
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.9.10
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python39.zip',
 '/app/.heroku/python/lib/python3.9',
 '/app/.heroku/python/lib/python3.9/lib-dynload',
 '/app/.heroku/python/lib/python3.9/site-packages']
Server time:    Fri, 18 Feb 2022 03:06:30  0000
Traceback Switch to copy-and-paste view
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py, line 47, in inner
                response = get_response(request) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
/app/Tests/views.py, line 35, in check
        send_mail('Test available', available_tests, '[email protected]', ['[email protected]'], fail_silently=False) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/__init__.py, line 61, in send_mail
    return mail.send() …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 284, in send
        return self.get_connection(fail_silently).send_messages([self]) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/backends/smtp.py, line 109, in send_messages
                sent = self._send(message) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/backends/smtp.py, line 123, in _send
        message = email_message.message() …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 246, in message
        msg = SafeMIMEText(self.body, self.content_subtype, encoding) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 159, in __init__
        MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset) …
Local vars
/app/.heroku/python/lib/python3.9/email/mime/text.py, line 42, in __init__
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 169, in set_payload
                for line in payload.splitlines()

How can I fix it? I took the instructions straight from a django tutorial, so I don't know why it isn't working. I also can't find anything on what this supposed 'splitlines' attribute is.

CodePudding user response:

Django's send_email expects a string as the message argument. So you need to apply e.g. join on the available_tests list variable first. Depending the structure of the variable, a simple "\n".join(available_tests) can be enough. Or <br> if you have HTML message.

  • Related