Mailgun Error-Expecting value:line 1 column 1 (char 0)


#1


How to solve this error?


#2

@abhijithp180 Please post the entire code to diagnose the problem!


#3


#4

Hi @abhijithp180,

It seems that your are not getting the the json response from the send_email function because of some invalid details that you have passed in the the line

mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL) 

Remove the the below line from your code

response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))

and directly print the response without doing json.loads()

print ("Mailgun response is " + response.text)

Also print the credentials before passing to Email.

Check the below code snippet.

import email_conf, json, time
from boltiot import Email, Bolt

minimum_limit = 300 #the minimum threshold of light value
maximum_limit = 600 #the maximum threshold of light value


mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)

print(email_conf.MAILGUN_API_KEY)
print(email_conf.SANDBOX_URL)
print(email_conf.SENDER_EMAIL)
print(email_conf.RECIPIENT_EMAIL)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)


while True:
    print ("Reading sensor value")
    response = mybolt.analogRead('A0')
    data = json.loads(response)
    print ("Sensor value is: " + str(data['value']))
    try:
        sensor_value = int(data['value'])
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            print("Making request to Mailgun to send an email")
            response = mailer.send_email("Alert", "The Current temperature sensor value is " +str(sensor_value))
            print ("Mailgun response is " + response.text)
    except Exception as e:
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)

Do let me know in case you need any other information.


#5

Thanks. Worked well for me. But I want to know why
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text[‘message’]))
this line didn’t work. I used the same credentials for this one
print ("Mailgun response is " + response.text)
too.


#6

Hi @abhijithp180,

Because for the json.loads , response.text must be dict format. but I suppose you are not getting the data in dict format from Twilio.

Update the output for this lineprint ("Mailgun response is " + response.text)


#7