I'm using php imap function imap_header to extract full header and imap_fetch_overview to extract raw header. Both the functions gives me stdClass object and arrays respectively.
I would like to always clean my from and to before further processing. Sometimes a FROM or TO can contain some thing like this,
Test user <test#test.com>
Also currently in my FROM I only see Test User and no email address until I use firebug.
How do I just get the test#test.com from these objects, arrays?
This is the result I get from imap_fetch_overview
Array
(
[0] => stdClass Object
(
[subject] => Testing
[from] => Test User
[to] => testuser2#test.com
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[message_id] => <abcdef1244.93784jgsfk#test.com>
[size] => 3443
[uid] => 1234
[msgno] => 123
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1366204439
)
)
There is a hidden <test#test.com> next to Test User. How do I extract that email address?
Similarly this is what I get from imap_header
stdClass Object
(
[date] => Wed, 17 Apr 2013 18:43:46 +0530
[Date] => Wed, 17 Apr 2013 18:43:46 +0530
[subject] => Test
[Subject] => Test
[message_id] => <abcdef1244.93784jgsfk#test.com>
[toaddress] => testuser#test.com
[to] => Array
(
[0] => stdClass Object
(
[mailbox] => testuser
[host] => test.com
)
)
[fromaddress] => Test User
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[reply_toaddress] => Test User
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[senderaddress] => Test User
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Test User
[mailbox] => test
[host] => test.com
)
)
[Recent] =>
[Unseen] => U
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 123
[MailDate] => 17-Apr-2013 13:13:59 +0000
[Size] => 3443
[udate] => 1366204439
)
A preg_match would be the obvious answer but just cant seem to figure out how to perform it on from bit where email address is not seen in the browser but is present when inspected with firebug. Any help is appreciated.
Thanks.
You can go one step further with imap_rfc822_parse_adrlist()Docs:
$toAddresses = imap_rfc822_parse_adrlist('Test user <test#test.com>', 'localhost');
print_r($toAddresses);
Array
(
[0] => stdClass Object
(
[mailbox] => test
[host] => test.com
[personal] => Test user
)
)
If name of array of std object is $arr
$arr[0]->to
returns what you want.
in second case
$stdObject <- object that is returned by second function
foreach($stdObject->to as $to)
{
echo $to->mailbox;
echo $to->host;
}
I've just discovered this thread and whilst it's old I hope this comment might help. I believe I've answered the query. As it happens, the format of the sender email is what's causing the issue.
From Name <from#name.com>
Because of the <> tag, the browser is making it into an HTML tag, I guess something akin to a span. By using str_replace to change "<" into " " (or whatever) you remove the issue before it gets to HTML
Related
I'm accessing a mailbox using ddeboer/imap. Connecting to the Server and retrieving Messages ist not a problem. But $message->getHeaders() returns the following (shortened Version):
Ddeboer\Imap\Message\Headers Object
(
[storage:ArrayIterator:private] => Array
(
[date] => Fri, 5 Jul 2019 07:00:47 +0200
[subject] => Test Mail
[message_id] => <108a4850-284e-170a-2c7d-b6f9g5218202#test.de>
[fromaddress] => "Test" <test#test.de>
[from] => Array
(
[0] => stdClass Object
(
[personal] => Test
[mailbox] => test
[host] => test.de
)
)
[deleted] =>
[draft] =>
[msgno] => 1
[maildate] => 5-Jul-2019 07:00:49 +0200
[size] => 223715
[udate] => 1562302849
)
)
How am I supposed to access the Information e.g. udate or from->mailbox?
You can use the get method and add the path
$message->getHeaders()->get('udate'):
$message->getHeaders()->get('from')[0]->mailbox
Okay.. I did a lot of research but I kept getting errors so I decided to ask the question directly..
I always converted objects to arrays, but I now want to try using an object directly to save website speed.. I got this object:
stdClass Object
(
[date] => Wed, 8 Feb 2017 15:03:44 +0000
[Date] => Wed, 8 Feb 2017 15:03:44 +0000
[subject] => asdasdasd
[Subject] => asdasdasd
[message_id] =>
[toaddress] => "test#hotmail.com"
[to] => Array
(
[0] => stdClass Object
(
[personal] => test#hotmail.com
[mailbox] => test
[host] => hotmail.com
)
)
[fromaddress] => Jason K
[from] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[reply_toaddress] => Jason K
[reply_to] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[senderaddress] => Jason K
[sender] => Array
(
[0] => stdClass Object
(
[personal] => Jason K
[mailbox] => JasonK
[host] => hotmail.com
)
)
[Recent] =>
[Unseen] =>
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 1
[MailDate] => 8-Feb-2017 16:03:25 +0100
[Size] => 7887
[udate] => 1486566205
)
I am trying to get the first date (wed, 8 feb 2017), the from->mailbox and the Size. I managed to get the from->mailbox with this code:
foreach($EmailHeaders->from as $from ){
echo $from->mailbox;
}
But I just cant find out how to obtain the other values aswell.. If i try:
foreach($EmailHeaders as $headers){
echo $headers->date;
}
then it doesnt work... Can anyone explain this to me? Sorry if this is already asked a thousand times before, I just cant figure it out..
check this http://php.net/manual/en/function.get-object-vars.php
var_dump(get_object_vars($EmailHeaders ));
It's simple, you don't have to iterate over the direct properties, just use
$EmailHeaders->date
To get the "date", you just need to use $EmailHeaders->date
You don't need a foreach. It's working for "from" because it contains an array of objects.
You can simply do
$EmailHeaders->date
OR
$EmailHeaders->Date
to get the date from your object.
While iterating through loop, your pointer is already on the date item, so you can't point to date index. It will point to the date index under date item.
Im in the meddle of creating a web app that downloads all the emails using imap library in php but im troubled on which id should i used, This is a sample email header retrieve from a gmail account.
Array
(
[0] => stdClass Object
(
[subject] => test subject
[from] => someone
[to] => enquire#xxxx.com
[date] => 17 Jun 2016 10:33:13 +0900
[message_id] => <08a32A30-C6n7-4A4A-AE91-C35429AF48BA#x2mail.xxxx.com>
[size] => 8460
[uid] => 8645
[msgno] => 8522
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1466127256
)
)
as you can see above there is a "uid" and "msgno" which should i use?
http://php.net/manual/en/function.imap-uid.php
Here I read: An UID is a unique identifier that will not change over time while a message sequence number may change whenever the content of the mailbox changes.
So, "uid" would probably be better.
so heres my predicament.
Currently using Yahoo API with PHP in order to use a 'Login with Yahoo' Function on my site.
I have managed to retrieve my entire profile. Trouble is, I can't directly display the primary email associated with the account.
If I use print_r($profile) I get everything dumped from my profile. It looks like this (personal data removed):
stdClass Object ( [uri] => http://social.yahooapis.com/v1/user/*********/profile [guid] => ***** [birthdate] => / [created] => 2010-04-22T20:47:48Z [emails] => Array ( [0] => stdClass Object ( [handle] => **#gmail.com [id] => 1 [type] => HOME ) [1] => stdClass Object ( [handle] => ****#yahoo.com [id] => 2 [primary] => 1 [type] => HOME ) ) [familyName] => Surname [gender] => M [givenName] => Ricki [image] => stdClass Object ( [height] => 192 [imageUrl] => http://l.yimg.com/dh/ap/social/profile/profile_b192.png [size] => 192x192 [width] => 192 ) [lang] => en-US [memberSince] => 2010-04-08T11:22:56Z [nickname] => Ricki [profileUrl] => http://profile.yahoo.com/*********** [updated] => 2013-03-03T18:12:22Z [isConnected] => )
Using $profile->emails->handle yields nothing however. Any ideas on what to do? I can use $profile->emails which returns an Array, displayed simply as 'Array' but if i try to select from the array i get an error:
"Catchable fatal error: Object of class stdClass could not be converted to string"
Anyone?
$profile->emails[0]->handle will give you the first handle in the system.
You can also do foreach($profile->emails as $email) { print $email->handle; }
For the life of me I can not figure out how to access the values of this array. Every example the stdClass Object has some type of value. If I try for example $obj->0->0->city; I get an error.
Can someone show me a example how to access [city] => toronto or even [date_created] => 2011-05-03 14:33:58?
I also tried this with no luck.
$object = $buy[1];
$title = $object->title[0];
echo "$title";
Thanks
This is what the api gives me.
stdClass Object
(
[id] => 1
[name] => toronto
[date_modified] => 2011-03-08 13:07:10
[tax_rate_provincial] =>
)
<br/>
Array
(
[0] => stdClass Object
(
[0] => stdClass Object
(
[id] => 28131844
[full_date] => 20110506
[end_date] => 20110511
[city] => toronto
[saved] => 1651
[discount_percentage] => 52
[deal_option] => Array
(
[0] => stdClass Object
(
[id] => 2600
[title] =>
[date_modified] => 0000-00-00 00:00:00
[date_created] => 2011-05-03 14:33:58
[value] => 3150
[price] => 1499
[deal_id] => 28131844
[is_default] => 0
)
)
[options] =>
[option_quantity] =>
[option_remaining] =>
[purchase_limit] => 1
[gift_limit] => 0
There is a special evil syntax to bypass numeric object attributes:
print $obj->{'0'}->{'0'}->city;
Is the correct syntax, and equivalent to the path you already determined.
Your second example is an array however, so it's probably:
print $array[0]->{'0'}->city;
The alternative is always to just foreach over a specific level - that works for objects and arrays likewise.