I have a simple django application which allows users to add a new user.Whenever we created a new user,a mail will send to that respective email-id (what we gave while creating a new user).If i have added any new user with email say xxxx@xxxx.com,a mail will send succesfully to xxxx@xxxx.com.I have no problem here.
But when I try to write a testcase for adding a new user, I got error in email sending line in my code...ie TypeError: __init__() takes at most 2 non-keyword arguments (6 given)
.
The code is given below,
#from my views.py,
Email().send_email(settings.FORGOT_SUBJECT, emailmessage, [username],
settings.CONTENT_TYPE)`
pls remember i have no problem in this line while created a user from webpage & the mail send sucesfully.only getting erro开发者_JAVA技巧r in this line during testcase.
# code for SMTP connection
class Email:
def __init__(self):
return;
def send_email(self, subject, message, recipients, contenttype):
"""
Send email using smtp connection.
"""
try:
from django.core.mail import EmailMessage, SMTPConnection, send_mail
from settings import EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_PORT, EMAIL_USE_TLS
finally:
connection = SMTPConnection(EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_USE_TLS)
emailMessage = EmailMessage(subject, message, settings.EMAIL_HOST_USER, recipients)
emailMessage.content_subtype = contenttype
connection.send_messages([emailMessage])
Pls give any solution to avoid this error.
TypeError: __init__() takes at most 2 non-keyword arguments (6 given)
Thanks in advance.
You appear to be writing something that isn't Python.
That __init__
method does nothing. Remove it.
In fact, remove the class altogether. This isn't Java: if you're not storing state or encapsulating functionality, there's no reason to have a class. Just use the send_email
function on its own.
精彩评论