django 测试用例期间发送电子邮件出错?

发布于 2024-12-05 09:01:21 字数 1609 浏览 2 评论 0原文

我有一个简单的 django 应用程序,它允许用户添加新用户。每当我们创建新用户时,都会向该相应的电子邮件 ID(我们在创建新用户时提供的电子邮件 ID)发送一封邮件。如果我添加了任何新用户对于电子邮件,请说 [电子邮件受保护],邮件将成功发送至 [电子邮件受保护] 这里没有问题。

但是当我尝试编写一个添加新用户的测试用例时,我的代码中的电子邮件发送行出现错误...即 TypeError: __init__() 最多需要 2 个非关键字参数(给定 6 个). 代码如下,

#from my views.py,

Email().send_email(settings.FORGOT_SUBJECT, emailmessage, [username], 
   settings.CONTENT_TYPE)`

请记住,我在从网页创建用户时在这一行没有问题。邮件发送成功。仅在测试用例期间此行出现错误。

# 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])

请给出任何解决方案以避免此错误。

TypeError: __init__() takes at most 2 non-keyword arguments (6 given)

提前致谢。

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 [email protected],a mail will send succesfully to [email protected] 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 error 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-12-12 09:01:21

您似乎正在编写一些不是 Python 的东西。

该 __init__ 方法什么也不做。将其删除。

事实上,完全删除该类。这不是 Java:如果您不存储状态或封装功能,就没有理由拥有类。只需单独使用 send_email 函数即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文