Python 我想通过Django中的API响应向用户发送电子邮件?
[data image][1]我想在调用此函数后向用户发送电子邮件,因此在第一个函数中,我将在razor pay上创建客户,在获取客户id后,我将传递给其他函数等。 因此,在这个函数get_virtual_account()中,我从razor pay提供的API中获取所有响应,我需要将该响应发送给创建该帐户的用户,因此我如何才能在电子邮件中发送此响应Python 我想通过Django中的API响应向用户发送电子邮件?,python,django,Python,Django,[data image][1]我想在调用此函数后向用户发送电子邮件,因此在第一个函数中,我将在razor pay上创建客户,在获取客户id后,我将传递给其他函数等。 因此,在这个函数get_virtual_account()中,我从razor pay提供的API中获取所有响应,我需要将该响应发送给创建该帐户的用户,因此我如何才能在电子邮件中发送此响应 def create_razor_customer(data): logger.info("Inside
def create_razor_customer(data):
logger.info("Inside create_razor_customer")
headers = {'Content-Type': 'application/json',}
data=json.dumps(data)
response = requests.post(settings.API_RAZORPAY+'/customers', headers=headers, data=data, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
logger.info(json.loads(response.content))
json_response = response.json()
customer_id = json_response['id']
logger.info(customer_id)
get_razor_customer(customer_id)
return response
def get_razor_customer(customer_id):
logger.info("Inside get_razor_customer")
headers = {'Content-Type': 'application/json',}
response = requests.get(settings.API_RAZORPAY+'/customers/'+customer_id, headers=headers, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
logger.info(json.loads(response.content))
create_razor_virtual_account(customer_id)
return response
def create_razor_virtual_account(customer_id):
logger.info("Inside create_razor_virtual_account")
headers = {'Content-Type': 'application/json',}
data = {"receivers": {"types": ["bank_account"]},"description": "razorpay","customer_id": customer_id,"close_by": 1761615838,"notes": {"reference_key": "reference_value"}}
data=json.dumps(data)
response = requests.post(settings.API_RAZORPAY+'/virtual_accounts', headers=headers, data=data, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
json_response = response.json()
virtual_id = json_response['id']
logger.info(virtual_id)
logger.info(json_response)
return response
def get_virtual_account(virtual_id):
logger.info("Inside get_virtual_account")
logger.info(virtual_id)
headers = {'Content-Type': 'application/json',}
response = requests.get(settings.API_RAZORPAY+'/virtual_accounts/'+virtual_id,headers=headers, auth=(settings.API_RAZORPAY_KEY, settings.API_RAZORPAY_SECRET))
json_response = response.json()
logger.info(json_response)
send_account_details()
return response
def send_account_details():
logger.info('Inside send_account_details')
send_mail('Account Details', 'Details for Razorpay account', settings.EMAIL_HOST_USER, ['abhishek@byond.travel',])
logger.info("sent")
return "sent"
[1]: https://i.stack.imgur.com/proIH.png
假设您的响应是JSON
resu data={“duration”:1201,“number”:6,“result”:“FAILURE”,“url”:”http://localhost:8080/job/git_checkout/6/“}
然后您需要将其传递到send\u account\u details函数中
def send_account_details(res_data):
import json
logger.info('Inside send_account_details')
body = "JSON Response is : " + json.dumps(res_data) + "\n\n" + "Details for Razorpay account"
send_mail('Account Details', body, settings.EMAIL_HOST_USER, ['abhishek@byond.travel',])
logger.info("sent")
return "sent"
使用
json.dumps(res_data)
您可以将您的json添加到电子邮件正文中。请参阅本文,谢谢您的帮助。但是我的问题是如何在send_account_details()函数中传递这个参数,因为响应来自json API这是我的主要问题。无法在电子邮件中发送数据,尽管电子邮件仅包含主题和消息行。您能否共享函数调用顺序以及发送电子邮件功能需要传递哪些数据?看,我已上载了一张图像。我现在获取的数据我希望这些数据通过send\u account\u details()此函数传递。我还更改了get\u virtual\u account()中的代码,我将调用send\u account\u details()