【问题标题】:Sending Email via SendGrid in Python在 Python 中通过 SendGrid 发送电子邮件
【发布时间】:2017-04-30 12:58:35
【问题描述】:

我正在尝试使用脚本中的上述函数通过 Sendgrid 发送电子邮件。

import MySQLdb
import sendgrid
import os
from sendgrid.helpers.mail import *

def sendalert(alertbody):
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SG.---....'#Not revealing my API key))
    from_email = Email("abcd@gmail.com")
    to_email = Email("abcd@gmail.com")
    subject = "Zomato Negative review"
    content = Content(alertbody)
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.body)
    print(response.headers)

调用类似于 sendalert("Yo") 的东西会给我以下错误消息:

Traceback (most recent call last):
File "zomato.py", line 91, in <module>
readreview(input_text)
File "zomato.py", line 64, in readreview
sendalert(review_text) # sending the email alert
File "zomato.py", line 16, in sendalert
response = sg.client.mail.send.post(request_body=mail.get())
File "/Library/Python/2.7/site-packages/python_http_client-2.2.1-     py2.7.egg/python_http_client/client.py", line 204, in http_request
return Response(self._make_request(opener, request))
File "/Library/Python/2.7/site-packages/python_http_client-2.2.1-py2.7.egg/python_http_client/client.py", line 138, in _make_request
return opener.open(request)
File  "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError:HTTP 错误 401:未经授权

怎么办?

【问题讨论】:

    标签: python python-2.7 sendgrid


    【解决方案1】:

    你可以试试

    import os
    import sendgrid
    
    
    def sendalert(alertbody):
        sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SG.---....'#Not     revealing my API key))
    
        message = sendgrid.Mail()
        message.add_to("abcd@gmail.com")
        message.set_from("abcd@gmail.com")
        message.set_subject("Zomato Negative review")
        message.set_text(alertbody)
    
        sg.send(message)
    

    您仍然可以按照您的方式创建电子邮件,但您发送电子邮件的方式肯定是错误的,请查看SendGrid documentation。

    顺便说一句,错误表明您传递的密钥在某种程度上是错误的。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,请确保 API 密钥是使用管理员 SendGrid 帐户或具有 Mail send 权限的 Teammates 帐户生成的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多