1

I'm new to Python, so I'm having trouble figuring out how to get at a specific element in a Mail object. I'm creating the Mail object in a function and returning it:

data = getMessage()

When I do print (data) it gives me the following:

{
  'from': {
    'email': 'my@email.com'
  },
  'subject': 'My Subject Line',
  'personalizations': [{
    'to': [{
      'email': 'another@email.com'
    }]
  }],
  'content': [{
    'type': 'text/plain',
    'value': 'http://localhost/some-url?t=ab99ceccf4ab4f97b1d014bb5e89707c'
  }]
}

Now, I can do something like print (data.subject) and it gives me My Subject Line, but I can't figure out how to get to the value property of content. I just want to print the URL to the console for testing so I don't have to wait for the email to come through.

Now I'm just printing it from inside the function itself because that's the easy way, but I'd still like to know how to get it from the Mail object.

2 Answers 2

1

The content thingy is a list with only 1 entry.

You can reach it by doing:

data["content"][0]["value"]

or if you aren't sure if there is only 1 entry:

for content in data["content"]:
    content["value"]
1
  • Thanks Walter. I tried things similar to that, but even with your code I end up getting TypeError: 'Mail' object is not subscriptable. I get the same error with both options.
    – user5886152
    Sep 28, 2020 at 13:38
0
for content in data["content"]:
    content["value"] 
1
  • 3
    A little explanation would make this code-only answer better.
    – рüффп
    Sep 28, 2020 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy