How to access From field of Gmail email message

I’m creating a workflow to send an SMS when an email coms into a mailbox from certain domains and email. This is working well, except that grabbing the From field of the email is a little tricky. The only way I can see to get this information is to grab it from the headers array by number, but that number can be a moving target. Most of the time {{steps.trigger.event.payload.headers[18].value}} is it, but sometimes it’s 17 or 19. Is there a way to consistent grab the From field? Zapier seems to do this easily, but I like Pipedream much better.

Hi @admin-cf5726

First off, welcome to the Pipedream community. Happy to have you!

Can you share an example of the data of steps.trigger.event.payload.headers?

You may have you use the .filter() method available on Javascript Arrays to detect which header contains the value you’re looking for, but without an example of the data and which specific text/object you’re looking for it’s very difficult to follow along.

Thanks!

1 Like

I believe the below is what you’re requesting. Your suggestion about the filter() command sounds like what I need to do. Do you have a link to an example or document about implementing that?

Here’s my header data. I couldn’t paste it into here. The site thought I was pasting in a bunch of links.

Hi @admin-cf5726

Perfect, that’s exactly what I was asking for.

Just a heads up, this forum supports Markdown. So to make a code block you can start any new line with three backticks (```) then paste your code and add another three backticks on a new line.

Or, alternatively just click this button in the post editor to create a code block:

I believe you’re looking for the To field in the email address. In that case you can write a quick filter like this:

{{ steps.trigger.event.payload.headers.filter(header => header.name === "To")[0] }}

This example hasn’t been tested by me, but it should grab the first header of the name "To" in your headers.

It won’t support multiple headers, but if you do need all of them, remove the [0] characters.

Hope this helps

1 Like

Exactly what I was looking for. Thanks for the response. I will let you know how it works!

So, I’ve switched my code to the following, but the SMS I receive isn’t quite right. I’m not quite sure how to troubleshooting this without being able to enumerate what’s in the Objects.

NEW EMAIL: From: {{ steps.trigger.event.payload.headers.filter(header => header.name === "From")[0] }} | Subject: {{ steps.trigger.event.payload.headers.filter(header => header.name === "Subject")[0] }}

NEW EMAIL: From: [object Object] | Subject: [object Object]

I tried this

NEW EMAIL: From: {{ steps.trigger.event.payload.headers.filter(header => header.name === "From")[0][0] }} | Subject: {{ steps.trigger.event.payload.headers.filter(header => header.name === "Subject")[0][0] }}

but that resulted in this

NEW EMAIL: From: undefined | Subject: undefined

This leads me to believe that the Objects are empty. Is that a correct assumption?

Hi @admin-cf5726

You’re close, but not quite.

In Javascript, you can access Arrays with that bracket notation. So headers[0] means you’re retrieving the the first item in the array of headers.

However, your steps.trigger.event.payload.headers is an array of objects. So in your case {{ steps.trigger.event.payload.headers.filter(header => header.name === "From")[0] }} is returning this object:

{
    "name": "From",
    "value": "Sending User <fromemail@sendingdomain.com>"
}

When any object is converted to a string in Javascript, it will convert that object into the string object Object

To access the value property on this Object you’ll need to use a different notation:

{{ steps.trigger.event.payload.headers.filter(header => header.name === "From")[0].value }}

Or describe in laymans terms, of the headers, filter out any headers that don’t equal "From", of that filtered list, grab the first item and retrieve the value property on that object.

Hope this helps.

1 Like

Ah, right! .value
That worked great! Thanks!

1 Like