Get sender email from gmail-api

China☆狼群 提交于 2019-11-27 16:11:25

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.

Know this question is from a while back, but I ran into this problem recently and couldn't really find any solutions online - so thought I'd share my findings.

Basically wrapping it in the htmlentities() would do the trick - but you'd have to access the part directly; as such:-

$part = $message->payload['modelData']['headers'][0]['value']; 
echo htmlentities($part);

It seems that the API is removing the values because when you do a var_dump on the payload, the string value is actually visible.

 ["name"]=> string(11) "Return-Path" ["value"]=> string(21) ""

This works on the FROM part too. Just thought this would a much easier way to demonstrate it. :)

I did a few experiments. the header from API Users.messages: get

header of "From"

{
    "name": "From",
    "value": "Ray Lin \u003cray@xxx.com\u003e"
 }

Experiments:

  foreach($headers as $header){ if($header->name==="From")  $from = $header->value;}

// experiment 1:
    preg_match('/\\\u003c(.*?)\\\u003e/', $from, $match);
    echo $match[1]; // empty


// experiment 2:(use a string instead)
    $str = 'Ray Lin \u003cray@xxx.com\u003e';
    preg_match('/\\\u003c(.*?)\\\u003e/', $str, $match);
    echo $match[1]; // ray@xxx.com

// experiment 3:
    echo $from // Ray Lin

// experiment 4:
    echo htmlentities($from) // Ray Lin <ray@xxx.com>

// experiment 5:
    preg_match('/<(.*?)>/', $from, $match);
    $from =$match[1];
    echo $from  // ray@xxx.com

// experiment 6:
    $from = htmlentities($from);
    preg_match('/<(.*?)>/', $from, $match);
    print_r($match); // empty Array()

experiment 5 is ideal to my need, but you can see from experiment 3, $from was just two words, I don't even know where the email was from.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!