# PyCall and email get\_payload

**URL:** <https://discourse.julialang.org/t/pycall-and-email-get-payload/3005>\
**Category:** General Usage\
**Created:** [April 1, 2017, 11:24pm UTC](https://discourse.julialang.org/t/pycall-and-email-get-payload/3005 "2017-04-01T23:24:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 1, 2017, 11:24pm UTC](https://discourse.julialang.org/t/pycall-and-email-get-payload/3005/1 "2017-04-01T23:24:48Z")

</div>

I am trying to decode email body using PyCall and email Python library. This works in Python:

```python
>>> import email
>>> email_message = email.message_from_string(data[0][1])
>>> email_message.get_payload(decode=True)

```

The problem in Julia is that email.message\_from\_string returns a Dict, so calling get\_payload is not possible (none of the elements contain the body):

```julia
julia> email_message = email.message_from_string(data[1][2])
Dict{Any,Any} with 25 entries:
  "Subject" => "Rec...

```

So, is there a way to call get\_payload(decode=True) from Julia?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 2, 2017, 4:07am UTC](https://discourse.julialang.org/t/pycall-and-email-get-payload/3005/2 "2017-04-02T04:07:49Z")

</div>

`PyCall.jl` tries to automatically convert Python return types back to native Julia types. Since that’s not always what you want (as in this case) you can use the lower-level `pycall` function, which lets you specify the expected return type. Specifying a return type of `PyObject` avoids any automatic conversion:

```julia
using PyCall
@pyimport email
pymsg = pycall(email.message_from_string, PyObject, data[1][2])
pymsg[:get_payload](decode=true)

```

See: [https://github.com/JuliaPy/PyCall.jl#calling-python](https://github.com/JuliaPy/PyCall.jl#calling-python)

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 2, 2017, 9:24am UTC](https://discourse.julialang.org/t/pycall-and-email-get-payload/3005/3 "2017-04-02T09:24:33Z")

</div>

Because `@pyimport` works so well in general, it seems that I have overlooked `pycall()`, many thanks!
