在MailGun后端,我发现了一个"没有传送到之前被退回的地址"错误:
这可能是因为当时电子邮件地址尚未正确设置,但现在却是。因此,我想从之前退回的地址列表中删除地址kurt@hicleo.com
。
关注https://documentation.mailgun.com/en/latest/api-bounces.html#bounces后,我为发件人域get_bounces()
实施了mg.startwithlucy.com
方法:
In [1]: import requests
In [2]: def get_bounces():
...: return requests.get('https://api.mailgun.net/v3/mg.startwithlucy.com
...: /bounces', auth=('api', '<our_api_key>')
...: )
...:
In [3]: response = get_bounces()
In [4]: response.status_code
Out[4]: 200
In [6]: import json
In [7]: content = json.loads(response.content)
In [14]: next(item for item in content['items'] if item['address'] == 'kurt@hicleo.com')
Out[14]:
{'MessageHash': '0f35b8112739c23d996bece18755de105a8422ad',
'address': 'kurt@hicleo.com',
'code': '550',
'created_at': 'Thu, 07 Jun 2018 17:48:28 UTC',
'error': 'No Such User Here'}
接下来,我定义了一个delete_bounce(address)
函数并将其调用为kurt@hicleo.com
。但是,我仍然会在get_bounces()
:
In [21]: def delete_bounce(address):
...: return requests.delete(f'https://api.mailgun.net/v3/mg.startwithlucy.com/{address
...: }', auth=('api', '<our_api_key>'))
...:
In [22]: delete_response = delete_bounce('kurt@hicleo.com')
In [23]: delete_response.status_code
Out[23]: 200
In [24]: delete_response.content
Out[24]: b'Mailgun Magnificent API'
In [25]: response = get_bounces()
In [26]: response.status_code
Out[26]: 200
In [27]: content = json.loads(response.content)
In [28]: kurt_bounces = [item for item in content['items'] if item['address'] == 'kurt@hicleo.
...: com']
In [29]: kurt_bounces
Out[29]:
[{'MessageHash': '0f35b8112739c23d996bece18755de105a8422ad',
'address': 'kurt@hicleo.com',
'code': '550',
'created_at': 'Thu, 07 Jun 2018 17:48:28 UTC',
'error': 'No Such User Here'}]
这里出了什么问题?在我看来,这是API参考中指令的正确Python实现: