Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using the gmail-api and having trouble getting the email of the sender. I am talking about the "full" format of the email. I consider the email of the sender to be the one that is actually written in the "From" field using the web interface of gmail. The headers of this full format usually include stuff like "X-Original-Authentication-Results" from which normally I can retrieve the smtp.mail value which is the sender's email but there are other emails where this header cannot be found.

This is my code so far:

if ("X-Original-Authentication-Results" == $header["name"]) {
        $value = $header["value"];
        preg_match("/smtp.mail=(.*)/", $value, $emailFound);
        $parsedEmail = $emailFound[1];
}

and here is a typical format of some headers:

[headers] => Array
                    (
                        [0] => Array
                            (
                                [name] => Delivered-To
                                [value] => [email protected]
                            )

                        [1] => Array
                            (
                                [name] => Received
                                [value] => ................
                            )

                        [2] => Array
                            (
                                [name] => X-Received
                                [value] => ................
                            )

                        [3] => Array
                            (
                                [name] => Return-Path
                                [value] => 
                            )

                        [4] => Array
                            (
                                [name] => Received
                                [value] => ................
                            )

                        [5] => Array
                            (
                                [name] => Received-SPF
                                [value] => ................
                            )

                        [6] => Array
                            (
                                [name] => Authentication-Results
                                [value] => ................
                            )
........

So is there a solid way to get the correct email of the sender? Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
769 views
Welcome To Ask or Share your Answers For Others

1 Answer

Loop through the headers[] array and look for the one with the 'name' = "From" (or whatever the header name is you're interested in). Note there may be multiple headers with that name. There are some standard headers that will usually exist (To, From, Subject) but I don't believe that's mandated by the RFC.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...