2017-05-04 56 views
-3

我想在郵件正文中發送我的發佈數據。在郵件正文中發送POST數據 - Django

我想要做這樣的事情,但是body dict顯然不是正確的格式(這會返回錯誤'dict' object has no attribute 'splitlines')。完成此操作的最佳做​​法是什麼?

from django.core.mail import EmailMessage 

def send_mail(request): 
    body = { 
     'name': request.POST['name'], 
     'phone': request.POST['phone'], 
     'email': request.POST['email'], 
     'appointment prefs': request.POST['appt'], 
     'message': request.POST['message'], 
    } 
    email = EmailMessage(
     'Website Inquiry', 
     body, 
     to=['[email protected]'] 
     ) 
    email.send() 

這裏的錯誤:

Request Method: POST 
Request URL: http://localhost:8000/send_mail 

Django Version: 1.11 
Python Version: 2.7.10 
Installed Applications: 
['apps.cphh', 
'django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.sites', 
'smartfields'] 
Installed Middleware: 
['django.middleware.security.SecurityMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware'] 



Traceback: 
File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 
    41.    response = get_response(request) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/Users/kristaprokopczyk/Documents/pet_health_harmony/pet_health_harmony/apps/cphh/views.py" in send_mail 
    31.  email.send() 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in send 
    342.   return self.get_connection(fail_silently).send_messages([self]) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in send_messages 
    111.     sent = self._send(message) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in _send 
    125.   message = email_message.message() 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in message 
    301.   msg = SafeMIMEText(self.body, self.content_subtype, encoding) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in __init__ 
    214.   MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset) 

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/mime/text.py" in __init__ 
    30.   self.set_payload(_text, _charset) 

File "/Users/kristaprokopczyk/.virtualenvs/megan/lib/python2.7/site-packages/django/core/mail/message.py" in set_payload 
    224.     for l in payload.splitlines() 

Exception Type: AttributeError at /send_mail 
Exception Value: 'dict' object has no attribute 'splitlines' 
+1

你在哪兒叫'splitlines'?請徹底分享您的代碼,以便我們提供幫助。 – ozgur

+0

我不使用Django,但可以將該字典轉儲爲json並嘗試? –

+0

請顯示您的真實密碼。這甚至不會編譯;你有''''包裝你的'body'字典而不是'{}'。 –

回答

0

請嘗試request.POST.get('name')代替。

+0

仍然收到相同的錯誤。 –

0

只需使用json.parse將其轉換爲字符串,然後在郵件正文中發送此字符串即可。

import json 

def send_mail(request): 
    body = (........) 
    body = json.parse(body) 
    ........... 
1

該問題源於EmailMessage希望單個字符串的消息內容,它被給予一個字典。

這需要成爲一個字符串,這應該做到這一點。

# turn the dict into a set of strings 
content = {"%s: %s" % (key, value) for (key, value) in body.items()} 
# turn those strings into 1 block of text separated by newlines 
content = "\n".join(content) 
email = EmailMessage(
    'Website Inquiry', 
    content, 
    to=['[email protected]'] 
    ) 
+0

謝謝,這個工程。出於某種原因,我認爲會有不同的解決方案,而不是簡單地將其轉換爲字符串。 –