【问题标题】:AttributeError: 'SendGridAPIClient' object has no attribute 'send'AttributeError:“SendGridAPIClient”对象没有属性“发送”
【发布时间】:2019-11-19 13:36:33
【问题描述】:

我正在使用 sendgrid 通过 django 发送邮件,但是,它一直在说 AttributeError: 'SendGridAPIClient' object has no attribute 'send'

重现步骤
  1. 安装sendgrid pip install sendgrid
  2. 运行脚本
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

sg = SendGridAPIClient("the_api_key")

email_data = Mail(
    from_email="support@email.africa",
    to_email="example@email.com",
    subject="YOUR EVENT IS NOW LIVE ON SITE!",
)
email_data.dynamic_template_data = json.dumps({
    "username": "example username",
    "item_name": "example item name",
    "item_slug": "path to example item slug",
    "template_id": "transactional template id",
    "message_type": "EMAIL"
})

response = sg.send(request_body=email_data.get())

错误:

Traceback (most recent call last):
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/mixins.py", line 84, in partial_update
    return self.update(request, *args, **kwargs)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/mixins.py", line 70, in update
    self.perform_update(serializer)
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/mixins.py", line 80, in perform_update
    serializer.save()
  File "/Users/Retina15/project/virtual/lib/python3.6/site-packages/rest_framework/serializers.py", line 209, in save
    self.instance = self.update(self.instance, validated_data)
  File "/Users/Retina15/project/store/serializers/event.py", line 358, in update
    response = sg.send(request_body=email_data.get())
AttributeError: 'SendGridAPIClient' object has no attribute 'send'
技术细节:
  • sendgrid==3.6.5
  • sendgrid-django==4.2.0
  • Django==2.0.13
  • python==3.6

【问题讨论】:

  • 我无法迁移到sendgrid。我在尝试时收到错误 ERROR: sendgrid-django 4.2.0 has requirement sendgrid<4,>=3.5, but you'll have sendgrid 6.1.0 which is incompatible.
  • 您或许可以使用djangosendgrid-v5 而不是sendgrid-django
  • 试试sg.client.mail.send.post(request_body=data)

标签: django sendgrid


【解决方案1】:

我去,回答我自己。

问题出在 sendgrid 版本上。 sendgrid version 3.6.5中的SendGridAPIClient没有任何方法send()

这是不使用 mail 助手的解决方案:

sg = sendgrid.SendGridAPIClient(apikey="THE_SENDGRID_API_KEY")
data = {
    "personalizations": [
        {
            "to": [
                {
                    "email": "example@email.com"
                }
            ],
            "dynamic_template_data": {
                "subject": "YOUR EVENT IS NOW LIVE ON SITE!",
                "username": user_name,
                "item_name": item_name,
                "item_slug": item_slug,
            },

        }
    ],
    "from": {
        "email": "support@email.africa"
    },
    "template_id": "the_template_id",
}

response = sg.client.mail.send.post(request_body=data)

这是使用mail 助手的解决方案:

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Substitution

sg = SendGridAPIClient("the_api_key")

email_data = Mail(
    to_email="example@email.com",
)
email_data.add_personalization(personalization[0].Substitution({
    "username": "example username",
    "item_name": "example item name",
    "item_slug": "path to example item slug"
}))
email_data.set_from("support@email.africa")
email_data.set_subject("YOUR EVENT IS NOW LIVE ON SITE!")
email_data.set_template_id("the_template_id")

response = sg.client.mail.send.post(request_body=email_data.get())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-19
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多